php - 查询未更新,不确定原因

标签 php mysql

由于某种原因,此脚本无法根据查询正确更新数据库。有人知道为什么脚本不更新吗?请告诉我!

<?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/

相关文章:

php - 有没有可靠的方法来避免 PHP 超时?

php - 为什么 PayPal 的 IPN 模拟器不能使用 https 地址?

mysql - 将 MySQL 变量与 Sequelize 结合使用

MySQL REGEXP 没有空格没有数字

mysql - 切换数据库中的记录

php - 使用 PHPmyAdmin 加密/解密 MySQL 数据库

php - 无法从新订单 Hook 函数的 order_id 获取产品详细信息

javascript - 如何从 javascript 中的 php 获取值?

mysql - 在 MySQL 中增加一列

mysql - 序列化和/或运算符