PHP BLOG - somo la 12: Jinsi ya kutumia prepared statement kwenye kusoma post za blog


image



Katika somo hili tutakwenda kutumia prepared ststement kwenye ku fetch data kutoka kwenye database.



Somo hili ni muendelezo wa masomo mawili yaliotangulia nyuma. Hvyo basi hapa nitakwenda kukuletea orodha ya mabadliko ya mafaili yote yaliobadilishwa baada ya kutumia prepared statement.

Pia tutakwenda kutatuwa tatizo la kufuta pica. Hapa tutakwenda kutatua liletatizo unapo edit icha le ya zamani inabakia. 

Ili kuelewa vyema somo hili tafadhali rejea mazomo mawili yaliotangulia kwa njia ya video. Tembelea channel yetu ya youtube inayopatikana kwa link hii youtube.com/@tehama-tz

 

dashboard.php

<html>
<head>
<title>Dshboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table, th, td {
border:1px solid black;
}
.dol{
margin: 0 auto;
max-width: 70%;
}
</style>
</head>
<body>
<h1><a href="post.php">Ongeza post</a> </h1>
<table>
<tbody>
<th>id</th>
<th>Image</th>
<th>Title</th>
<th>Publshed</th>
<th>Updated</th>
<th>Publsher</th>
<th>Soma</th>
<th>edit</th>
<th>Delete</th>
<?php
include "config.php";
$sql= $conn->prepare("SELECT * FROM posts");
$sql->execute();
$result = $sql->get_result();
while ($post = $result->fetch_assoc()){?>
<tr>
<td><?php echo $post['id']?></td>
<td><img src="upload/<?php echo $post['image']?>" height="5%" width="5%"></td>
<td><?php echo $post['title']?></td>
<td><?php echo $post['post_time']?></td>
<td><?php echo $post['updated_time']?></td>
<td><?php echo $post['publisher']?></td>
<td><a href="view.php?id=<?php echo $post['id']?>">Soma</a> </td>
<td><a href="edit.php?id=<?php echo $post['id']?>">edit</a> </td>
<td><a href="delete.php?id=<?php echo $post['id']?>">Futa</a> </td>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>

 

 

delete.php

 

$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);

include "config.php";

$sql= $conn->prepare("SELECT image FROM posts where id =?");

$sql->bind_param("i", $id);

$sql->execute();

$result = $sql->get_result();

while ($post = $result->fetch_assoc()){

$image = $post['image'];

unlink("upload/".$image);

$sql= $conn->prepare("DELETE FROM posts where id =?");

$sql->bind_param("i", $id);

$sql->execute();

header("location:dashboard.php");



}

 

 

 

 

 

 

post.php

<html>
<head>
<title>create your post</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.ckeditor.com/4.16.2/standard/ckeditor.js"></script>
<script src="ckeditor.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label for="title">Title</label><br>
<input type="text" name="title" placeholder="write your post title" id="title"><br>

<label for="summary">Summary</label><br>
<input type="text" name="summary" placeholder="write your post summary" id="summary"><br>

<label for="content">content</label><br>
<textarea name="content" id="content" placeholder="wrte your post content"></textarea>
<script>CKEDITOR.replace( 'content' );
</script>
<label for="publisher">publisher</label><br>
<input type="text" name="publisher" placeholder="write your post publisher" id="publisher"><br>

<label for="date">Date</label><br>
<input type="date" name="date" placeholder="write your post date" id="date"><br><br>

<label for="image">upload your image</label>
<input type="file" name="file" accept="image/*" id="image"><br><br>

<input type="submit" name="submit" value="submit">
</form>
<br><br>
</body>
</html>
<?php
include "config.php";
if (isset($_POST['submit'])) {
//start code
// form variables
$title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
$summary = filter_var($_POST['summary'], FILTER_SANITIZE_STRING);
$content = $_POST['content'];
$publisher = filter_var($_POST['publisher'], FILTER_SANITIZE_STRING);
$date = filter_var($_POST['date'], FILTER_SANITIZE_STRING);
$image = filter_var($_FILES['file']['name'], FILTER_SANITIZE_STRING);
$folder = "upload/";
$location = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size']; //1024 = 1kb, 1024 kb = 1mb, 1024 = 1 gb 5*1024*1024 = 5242880
$format = ["webp", "jpg", "jpeg", "png"];
$extension = pathinfo($image, PATHINFO_EXTENSION);
if (!in_array($extension, $format)){
echo "we dont allow such file format";
}else {
if ($size > 5242880) {
echo "your file is too large";
} else {
//$sql = "INSERT INTO posts(title, summary, content, publisher, post_time, image) VALUES ('$title', '$summary', '$content', '$publisher', '$date', '$image' )";
$sql = $conn->prepare("INSERT INTO posts(title, summary, content, publisher, post_time, image) VALUES (?,?,?,?,?,? )");
$sql->bind_param("ssssss",$title, $summary, $content, $publisher, $date, $image );
if ($sql->execute()) {
move_uploaded_file($location, $folder . $image);
header("location:dashboard.php");
} else {
echo "try again";
}
}
}
}else{
echo "we dont accept empty form";
}

 

post_script.php

 

include "config.php";

if (isset($_POST['submit'])) {

//start code

// form variables

$id = filter_var($_POST['id'], FILTER_SANITIZE_NUMBER_INT);

$title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);

$summary = filter_var($_POST['summary'], FILTER_SANITIZE_STRING);

$content = $_POST['content'];

$publisher = filter_var($_POST['publisher'], FILTER_SANITIZE_STRING);

$date = filter_var($_POST['date'], FILTER_SANITIZE_STRING);

$image = filter_var($_FILES['file']['name'], FILTER_SANITIZE_STRING);

$folder = "upload/";

$old_image = filter_var($_POST['old_image'], FILTER_SANITIZE_STRING);

$location = $_FILES['file']['tmp_name'];

$size = $_FILES['file']['size']; //1024 = 1kb, 1024 kb = 1mb, 1024 = 1 gb 5*1024*1024 = 5242880

$format = ["webp", "jpg", "jpeg", "png"];

$extension = pathinfo($image, PATHINFO_EXTENSION);

if (!in_array($extension, $format)){

echo "we dont allow such file format";

}else {

if ($size > 5242880) {

echo "your file is too large";

} else {

//$sql ="UPDATE posts set title = '$title', summary = '$summary', content = '$content', publisher = '$publisher', post_time = '$date', updated_time = '$update' where id = $id";

$sql = $conn->prepare("UPDATE posts set title = ?, summary = ?, content = ?, publisher =?, post_time =?, updated_time= ? where id = ?");

$sql->bind_param("ssssssi", $title, $summary, $content,$publisher,$date, $update, $id );

if ($sql->execute()) {

if ($image ==""){echo "no image";}else{

//$sql2 = "UPDATE posts set image = '$image' where id = $id";

$sql = $conn->prepare("UPDATE posts set image =? where id=?");

$sql->bind_param("si", $image,$id);

if ($sql->execute()){

if (unlink("upload/".$old_image)){

move_uploaded_file($location, $folder . $image);

}else{

echo "sorry we couldn't delete old image";

}



}

}

header("location:dashboard.php");

} else {

echo "try again";

}

}

}

}else{

echo "we dont accept empty form";

}

 

 

 

 

 

view.php

 

<?php
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
include "config.php";
$sql= $conn->prepare("SELECT * FROM posts where id =?");
$sql->bind_param("i", $id);
$sql->execute();
$result = $sql->get_result();
while ($post = $result->fetch_assoc()){
?>
<html lang="swa">
<head>
<title><?php echo $post['title']?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p{
margin: 0 auto;
text-align: left;
max-width: 70%;
font-size: 100%;
}
h1{
font-size: 100%;
text-align: center;
max-width: 98%;
}
a {text-decoration:none;}
@media screen and (max-width: 600px) {
img { width: 100%; }
}
</style>
</head>
<body>
<h1><?php echo $post['title']?></h1>
<p><img src="upload/<?php echo $post['image']?>"></p>
<p><?php echo $post['summary']?></p>
<p><?php echo $post['content']?></p>
<p>IImeandika na <?php echo $post['publisher']?> Tarehehe <?php echo $post['post_time']?> </p>
<?php }?>
</body>
</html>

 

Mwisho

Katika somo linalofuata utajifunza jisni ya kuweka friendly url kw akutumia htaccess file 



Je! umeipenda hii post?
Ndio            Hapana            Save post

Imeandikwa na Rajabu Terehe 2023-10-20 Download PDF Share on facebook WhatsApp

RELATED POSTS

picha

Katika somo hili utajifunza jinsi ya kuandika while loop kwenye
picha

katika post hii utajifunza jinsi ya kutengeneza ukurasa wa dashboard
picha

Katika somo hili utakwenda kujifunza kuhusu function kwenye PHP na
picha

katika somo hili utajfunza kukusanya taarifa za mafaili. kisha ku
picha

Katika somo hili utajifnza jinsi ya ku upload file kwenye
picha

Katika somo hili utakwenda kujifunza jinsi ya kutumia PHP kutengeneza
picha

Katika somo hili utajifunza jinsi ya kufuta table ya databse
picha

Katika somo hili utakwenda kujifunza jinsi ya kuandika function yako
picha

Katika somo hili utakwenda kujifunza concept ya interface na concept
picha

Katika post hii utajifunza jinsi ya kupata taarifa muhimu za
picha

Katika somo hili uatakwenda kujifunza jinsi ya kutumia htaccess ili
picha

Katika somo hili utajifunza jinsi ambavyo utaweza kutengeneza mfumo wa
picha

Katika somo hili tutakwenda kujifunza kuhusu array kwenye php, na
picha

katika post hii utajifunza jinsi ya kufuta post kwenye database.
picha

Katika somo hili utakwendakujifunza concept ya namespaces na jinsi inavyosaidi
picha

Katika somo hili utakwenda kujifunza kuhusu static method na inavtotumika