由于某种原因,此脚本无法根据查询正确更新数据库。有人知道为什么脚本不更新吗?请告诉我!
<?php
session_start();
include '../connect.php';
if(!isset($_SESSION['id'])){
header("Location: ../index.php");
}
if(isset($_POST['submit'])){
$id=$_POST['id'];
$postid=$_POST['postid'];
$content=$_POST['content'];
$title=$_POST['title'];
echo "<pre>";
print_r($_POST);
if(!empty($content)){
$content = mysql_real_escape_string($content);
} else {
echo 'You need to write something in your comment!';
}
$upd=mysql_query("UPDATE replies SET reply_content='$content' WHERE reply_id='$postid'");
if(!$upd){
echo 'Error: '.mysql_error();
}
} else {
if (isset($_GET['id'])){
$postid = $_GET['id'];
$id=$_SESSION['id'];
$q = mysql_query("SELECT * FROM `replies` where `reply_id`='$postid'");
if(!$q){
echo 'Error: '.mysql_error();
}
$res = mysql_fetch_assoc($q);
$q2 = mysql_query("SELECT topic_subject FROM `topics` where `topic_id`='$postid'");
$res2 = mysql_fetch_assoc($q2);
if(!q2){
echo 'Error: '.mysql_error();
}
if ($res['reply_by'] == $id){
} else {
header("Location: ../pagenotfound.html");
}
}
?>
<form action="edit.php">
<input type="text" name="title" value="<?php echo $res2['topic_subject'] ?>" disabled="disabled" />
<br />
<textarea rows="20" name="content" cols="50"><?php echo $res['reply_content']?></textarea>
<input type="hidden" name="postid" value="<?php echo $postid ?>" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
如果您需要更多信息,请告诉我!
更新:问题是,当我单击“提交”时,它会将我发送到仍列出表单的页面。当我尝试 print_r($_POST) 时,我注意到了这个问题,因为它实际上没有打印 $_POST,我相信表单或检查 isset 提交的位置有问题。
最佳答案
尝试:
if(!empty($content)){
$content = mysql_real_escape_string($content);
} else {
echo 'You need to write something in your comment!';
// if $content is mandatory, you should put a die("error") here
}
你应该用一个简单的方法检查你的 $_POST 数组
echo "<pre>";
print_r($_POST);
并确保 POST 包含您要查找的变量(首先:提交)
编辑:在使用 print_r($_POST) 之前先放置它:
if(isset($_POST['submit'])){
错误:您忘记设置表单方法类型。尝试使用:
<form action="edit.php" method="post">
否则,表单将以 $_GET 形式发送参数。
这是一个简单的 php 表单教程。 http://php.net/manual/en/tutorial.forms.php
关于php - 查询未更新,不确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350965/