我正在尝试更新数据库中的记录。该代码旨在允许用户更新网站上的条目,同时还可以选择编辑图像。当我最初测试这段代码时,它没有任何问题。当他们选择图像时,它会更新图像,当他们没有选择图像时,它不会在更新中包含图像。当我将此代码移动到它需要的页面时,它不再工作。它总是在读取它,就好像用户没有选择要上传的图像一样。测试代码和这段代码之间唯一不同的是数据库中的名称,以及为变量 $title 和 $description 添加了 mysql_real_escape_string()。
这是对我不起作用的 PHP 代码:
<?php
require_once ("connect.php");
if (isset($_POST['description'])) {
$id = $_GET['id'];
$title = $_POST['title'];
$description = $_POST['description'];
$title = mysql_real_escape_string($title);
$description = mysql_real_escape_string($description);
$target = "../images/contests/";
$target = $target.basename( $_FILES['image']['name']);
$ok=1;
if($_FILES['image']['name'] == "") {
$query = "UPDATE tbl_contests SET contests_title='$title', contests_description='$description' WHERE contests_id='$id'";
$result = mysql_query ($query);
if ($result) {
header ("Location: contests.php?=noimage");
exit ();
} else {
header ("Location: contests.php?=error");
exit ();
}
} else {
if ($ok==0){
header("Location: contests.php?=error");
} else {
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
echo "<p>Your upload was sucessful.</p>";
$query = "UPDATE tbl_contests SET contests_title='$title', contests_description='$description', contests_image='$target' WHERE contests_id='$id'";
$result = mysql_query ($query);
if ($result) {
header ("Location: contests.php?=image");
exit ();
} else {
header ("Location: contests.php?=error");
exit ();
}
}
}
}
}
?>
这是与上述代码有关的表格:
<?php
$postnum = $_GET['id'];
$query = "SELECT * FROM tbl_contests WHERE contests_id=".$postnum;
$result= mysql_query($query);
$row = mysql_fetch_array($result);
$path = "../images/contests/";
?>
<form action="update-past.php?id=<?php print $row[contests_id]; ?>" method="post" id="updatepast">
<br /><label>Title:</label> <p><input type="text" name="title" id="title" class="input" value="<?php print $row[contests_title]; ?>" /></p>
<?php if ($row['contests_image'] == !null) { ?>
<p><img src="<?php print $path.$row['contests_image']; ?>" width="425" height="500" /></p>
<br /><label>Edit Image: (Optional)</label> <p><input name="image" type="file" id="image" class="file" size="50" /></p>
<?php } else { ?>
<br /><br /><br /><br /><label>Add Image: (Optional)</label> <p><input name="image" type="file" id="image" class="file" size="50" /></p>
<?php } ?>
<br /><br /><br /><br /><br /><label>Description:</label><p><textarea name="description" cols="85" id="description" class="contentinput" rows="10"><?php print $row[contests_description]; ?></textarea></p>
<p><input type="submit" name="submit" id="button" value="Edit" /></p>
</form>