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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应