1. kwanza tengeneza faili liite edit.php kisha weka code zifuatazo

<?php
$id = $_GET['id'];
include "config.php";
$Soma = mysqli_query($conn, "SELECT * FROM posts where id =$id");
while ($post = mysqli_fetch_array($Soma)){?>
<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="post_script.php" method="post" enctype="multipart/form-data">
<label for="title">Title</label><br>
<input type="text" name="title" placeholder="write your post title" id="title" value="<?php echo $post['title'];?>"><br>

<label for="summary">Summary</label><br>
<input type="text" name="summary" placeholder="write your post summary" id="summary" value="<?php echo $post['summary'];?>"><br>

<label for="content">content</label><br>
<textarea name="content" id="content" placeholder="wrte your post content"> <?php echo $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" value="<?php echo $post['publisher'];?>"><br>

<label for="date">Date</label><br>
<input type="date" name="date" placeholder="write your post date" id="date" VALUE="<?php echo $post['post_time'];?>"><br><br>

<label for="update">Update time</label><br>
<input type="date" name="update" placeholder="write your post update" id="update" VALUE="<?php echo $post['updated_time'];?>"><br><br>

<p><img src="upload/<?php echo $post['image'];?>"></p>
<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>


   

2. Kisha tengeneza fal lingine liite posts_script.php kisha pest code zifuatazo

<?php
include "config.php";
if (isset($_POST['submit'])) {
//start code
// form variables
$id = $_POST['id'];
$title = $_POST['title'];
$summary = $_POST['summary'];
$content = $_POST['content'];
$publisher = $_POST['publisher'];
$date = $_POST['date'];
$update = $_POST['update'];
$image = $_FILES['file']['name'];
$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 ="UPDATE posts set title = '$title', summary = '$summary', content = '$content', publisher = '$publisher', post_time = '$date', updated_time = '$update' where id = $id";
if (mysqli_query($conn, $sql)) {
if ($image ==""){echo "no image";}else{
$sql2 = "UPDATE posts set image = '$image' where id =$id";
if (mysqli_query($conn,$sql2)){
move_uploaded_file($location, $folder . $image);
}
}
header("location:dashboard.php");
} else {
echo "try again";
}
}
}
}else{
echo "we dont accept empty form";
}

  

 

Mwisho:

Katka somo linalofuata tutakwenda kujifunza jinsi ya kuwea securit kwenye blog yetu