【问题标题】:MySQL UPDATE query Error in Web Page网页中的 MySQL UPDATE 查询错误
【发布时间】:2014-11-10 21:57:35
【问题描述】:

尽管语法对我来说很好,但我的 UPDATE 查询失败(我有另一个更新查询在同一页面上运行良好)。

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("sitename") or die(mysql_error());

$id = $_GET['id'];

if (isset($_POST['submit'])){                           

        $b = mysql_real_escape_string(trim($_POST['body']));

        //**You have an error in your SQL syntax;** --> ?
        mysql_query ("UPDATE body SET body= $b WHERE id = $id") or die (mysql_error() );

        // $b is fine 
        echo "$b";          

    }

HTML 评论表单的呈现方式..

// Puts SQL Data into an array
$q = mysql_query("SELECT * FROM vote") or die (mysql_error());

// Now we loop through the database
echo "<br />";
while ($ratings = mysql_fetch_array($q))
{
    //This outputs the doctors's name
    echo "Doctor's name:" . $ratings['doctor_name'] ."<br />";

        //This outputs a textarea for the user to submit comments
        echo "<b>Your Experience: </b>";
        echo "<form method='post' action='review_doctors.php'> 

                <textarea name='body'></textarea>
                <input type='submit' name='submit' value='Send' id='submit'/>
             </form>
             "; 
        echo "<br />";

echo "<p> </p>";
}

为什么每次提交评论时都会出现 SQL 语法错误?

【问题讨论】:

标签: php mysql sql database forms


【解决方案1】:

所以,您从$_GET 数组中设置$id,这可能不会在通过邮寄提交表单时设置。

您正在运行的更新查询在 POST 检查中(检查是否设置了 $_POST['submit'])。

您可能希望在帖子正文中发送$id 的值并将其从帖子数组中拉出。

【讨论】:

  • 我根据您的提示将其修复为:
【解决方案2】:

我把它改成这样:

// If submitted 
if (isset($_POST['id'])){       

            //Capture what was typed in textarea
            $b = mysql_real_escape_string(trim($_POST['body']));

            $id = $_POST['id'];
            mysql_query ("UPDATE vote SET body = '$b' WHERE id = $id") or die (mysql_error() );

            // $b and $id are still fine 
            echo "$b";  
            echo "$id";

        }

还修复了隐藏的输入值:

while ($ratings = mysql_fetch_array($q))
{
    //This outputs the doctors's name
    echo "Doctor's name:" . $ratings['doctor_name'] ."<br />";

        $id = $_POST['id'];    

        //This outputs a textarea for the user to submit comments
        echo "<b>Your Experience: </b>";
        echo "<form method='post' action='review_doctors.php'> 

                <textarea name='body'></textarea>
                <input type='submit' name='submit' value='Send'/>


                <input type='hidden' name='id' value='$ratings[id]' />

             </form>
             ";

        echo "<br />";

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 2012-02-21
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多