我在更新数据库方面遇到了问题。我想在编辑帖子后更新数据库。我回显了输入中的数据,您可以在单击编辑后看到这些数据,因此不需要填充字段但需要修改。它可以在我剪切数据并再次通过它时工作,但是当我修改数据时,例如标题然后它显示填写所有字段的消息。我顺便使用PDO方法。
以下是代码:
<div id="box" class="info-box">
<h1 class="text-title">Insert Post Here!</h1>
<form action="" method="post" enctype="multipart/form-data">
<p>Your post will not be published if all fields are not filled!</p>
<p>
<label>Post Title *</label>
<input type="text" name="post_title" size="60" value="<?php echo $p_title; ?>">
</p>
<p>
<label>Post Category *</label>
<select name="cat">
<?php
$g_cats = "SELECT * FROM categoryTable WHERE cat_id='$p_cat'";
$run_g_cats = $conn->query($g_cats);
while ($cats_row = $run_g_cats->fetch()) {
$c_id = $cats_row['c_id'];
$c_title = $cats_row['c_title'];
echo "<option value='$c_id'>$c_title</option>";
// Get more Categories..
$get_m_cats = "SELECT * FROM categoryTable";
$run_m_cats = $conn->query($get_m_cats);
while ($more_cats = $run_m_cats->fetch()) {
$c_id = $more_cats['c_id'];
$c_title = $more_cats['c_title'];
echo "<option value='$c_id'>$c_title</option>";
}// while for more categories
}
?>
</select>
</p>
<p>
<label>Post Author *</label>
<input type="text" name="post_author" size="60" value="<?php echo $p_author; ?>">
</p>
<p>
<label>Post Keywords *</label>
<input type="text" name="post_keywords" size="60" value="<?php echo $p_keywords; ?>">
</p>
<p id="upfile">
<label>Post Image *</label>
<label class="upfile" for="file">Choose a file</label><img src="upimg/<?php echo $p_image; ?>" style="max-width: 135px; height: auto; margin-top: 15px;">
<input type="file" name="post_image" size="50" id="file" class="inputfile" data-multiple-caption="{count} files selected" multiple>
</p>
<p>
<label>Post Content *</label>
<textarea name="post_content" rows="20" cols="45"><?php echo $p_content; ?></textarea>
</p>
<p class="form-submit">
<input type="submit" name="update" value="Update The Post!">
</p>
</form>
if ( isset($_POST['update']) ) {
$update_id = $p_id;
$p_title = $_POST['p_title'];
$p_date = date('d-m-y');
$p_cat_up = $_POST['cat'];
$p_author = $_POST['p_author'];
$p_keywords = $_POST['p_keywords'];
$p_image = $_FILES['p_image']['name'];
$p_image_tmp = $_FILES['p_image']['tmp_name'];
$p_content = $_POST['p_content'];
if ( $p_title == '' OR
$p_cat == '' OR
$p_author == '' OR
$p_keywords == '' OR
$p_image == '' OR
$p_content == '' ) {
echo "Please fill in all the fields!";
exit();
} else {
move_uploaded_file($p_image_tmp, "upimg/$p_image");
$update_posts = "UPDATE table SET cat_id = :p_cat, p_title = :p_title, p_date = :p_date [...] WHERE p_id = :p_id";
$update_q = $conn->prepare($update_posts);
$update_q->execute( array(':p_cat'=>$p_cat_up,
':p_title' =>$p_title,
':p_date' =>$p_date,
':p_author' =>$p_author,
':p_keywords'=>$p_keywords,
':p_image' =>$p_image,
':p_content' =>$p_content,
':p_id' =>$update_id
) );
echo "Post has been Published!";
} // Validation
} // Main if-statement
$conn = null;
&GT?;