douqingji3026 2014-11-10 21:57
浏览 37
已采纳

MySQL UPDATE查询网页中的错误

My UPDATE query is failing although the syntax looks fine to me (I have another update query that works fine on the same page).

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";          

    }

How the HTML review forms are rendered..

// 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>";
}

Why am I getting a SQL syntax error whenever a comment is submitted?

  • 写回答

2条回答 默认 最新

  • doucheng2053 2014-11-11 03:02
    关注

    I fixed it to this:

    // 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";
    
            }
    

    Also fixed the hidden input value:

    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 />";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?