以下是php代码:
<?php
if (isset($_POST['update-post'])) {
$post_title_update= $_POST['post-title'];
$post_category_update= $_POST['post-category-id'];
$post_author_update=$_POST['post-author'];
$post_content_update= $_POST['post-content'];
$post_tags_update= $_POST['post-tags'];
$post_comment_update= $_POST['post-comment-count'];
$post_status_update= $_POST['post-status'];
$post_image_update= $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];
move_uploaded_file($post_image_temp, "images/$post_image_update");
$update_query="UPDATE post set ";
$update_query.="post_category_id= '{$post_category_update}', ";
$update_query.="post_title= '{$post_title_update}', ";
$update_query.="post_author= '{$post_author_update}', ";
$update_query.="post_date= now(), ";
$update_query.= "post_image ='{$post_image_update}', ";
$update_query.="post_content= '{$post_content_update}', ";
$update_query.="post_tags= '{$post_tags_update}', ";
$update_query.="post_comment= '{$post_comment_update}', ";
$update_query.="post_status= '{$post_status_update}' ";
$update_query.="WHERE post_id={$id} ";
//echo $update_query;
$update_query_result= mysqli_query($connnect, $update_query);
if ($update_query_result) {
echo 'Record Updated !';
}
else{
echo mysqli_error($connect);
}
}
?>
Html表格:
[![<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="post-title">Post Title</label>
<input type="text" name="post-title" class="form-control" value="<?php echo $post_title;?>">
</div>
<div class="form-group">
<label for="post-category-id">Post Category </label>
<select class="form-control" name="post-category-id">
<?php
$querycategory= "SELECT * FROM category";
$select_category= mysqli_query($connect, $querycategory);
while ($categories = mysqli_fetch_assoc($select_category)) {
echo "<option>{$categories\['cat_title'\]}</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="post-author">Post Author</label>
<input type="text" name="post-author" class="form-control" value="<?php echo $post_author;?>">
</div>
<div class="form-group">
<label for="post-image">Upload image </label>
<input type="file" name="image">
</div>
<div class="form-group">
<label for="post-content">Post Content</label>
<textarea class="form-control" rows="4" name="post-content" cols="30" ><?php echo $post_content;?> </textarea>
</div>
<div class="form-group">
<label for="post-tags">Post Tags</label>
<input type="text" name="post-tags" class="form-control" value="<?php echo $post_tags;?>">
</div>
<div class="form-group">
<label for="post-comment-count">Comment Count</label>
<input type="text" name="post-comment-count" class="form-control" value="<?php echo $post_comment;?>">
</div>
<div class="form-group">
<label for="post-status"></label>
<input type="text" name="post-status" class="form-control" value="<?php echo $post_status;?>">
</div>
<input type="submit" name="update-post" value="Update Post" class="btn btn-primary">
</form>
现在,当我点击更新时,它既没有出现任何错误也没有更新数据库,请帮助它
答案 0 :(得分:1)
$update_query_result= mysqli_query($connnect, $update_query);
^^^^^^^^^ TYPO (should be $connect)
if ($update_query_result) {
echo 'Record Updated !';
}
else{
echo mysqli_error($connect);
^^^^^^^^ Correct form of the word is used here.
}
您在链接的变量名称中输入了拼写错误。除此之外,您很容易受到SQL注入攻击。请阅读并考虑使用准备好的查询。