更新值时出错

时间:2017-08-13 09:18:18

标签: javascript php html mysql

我正在创建一个小项目,向我的学生解释如何使用PHP代码更新SQL数据库中的值。我在MySQL中创建了表,所有字段都是VARCHAR。我写了以下代码,引发以下错误: 更新值时出错您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,使用接近' Mbps WHERE dsl =' 25610669''在第1行,其中25610669是数据库中的现有记录。这是代码:

<?php
    if((isset($_POST['B2'])))
    {
     $server = 'localhost' ;
      $un = 'root' ;
      $pass = 'icsk' ;
      $db = 'yusuf' ;
     $conn = mysqli_connect($server, $un, $pass, $db);
    $update = "UPDATE homereg SET Fname = {$_POST['First']}, Lname = {$_POST['Last']}, cid = {$_POST['cid']}, pack = {$_POST['choice']} WHERE dsl = {$_POST['dsl']}" ;
    $result = mysqli_query($conn, $update);
    if($result == 1)
      { 
         echo "Successfully Updated" ; 
      }
    else
      {
         echo "Error in Updting value" . mysqli_error($conn)  ;
      }
    }

    ?>
    <html>
    <head>
    <title>Update User Information</title>
    </head>
    <body background="HomePageMap.gif">
    <CENTER><B><FONT COLOR = 'RED'>SEARCH & UPDATE THE EXISTING RECORD HERE </FONT></B></CENTER><P>
    <form method="POST" action="modify.php" name = "frm">   
        <div align="center">
            <table border="1" width="314">
                <tr>
                    <td width="130"><b>DSL Number</b></td>
                    <td width="168"><input type="text" name="dsl" size="20"></td>
                </tr>
                <tr>
                    <td width="130"><b>First Name</b></td>
                    <td width="168"><input type="text" name="First" size="20"></td>
                </tr>
                <tr>
                    <td width="130"><b>Last Name</b></td>
                    <td width="168"><input type="text" name="Last" size="20"></td>
                </tr>
                <tr>
                    <td width="130"><b>Civil ID</b></td>
                    <td width="168"><input type="text" name="cid" size="20"></td>
                </tr>
                <tr>
                    <td width="130"><b>Net Pack</b></td>
                    <td width="168"><select size="1" name="choice">
                    <option value = "2 Mbps">2 Mbps</option>
                    <option value = "5 Mbps">5 Mbps</option>
                    <option value = "10 Mbps">10 Mbps</option>
                    <option value = "15 Mbps">15 Mbps</option>
                    </select></td>
                </tr>
            </table>
        </div>
        <p align="center"><input type="submit" value="Search" name="B1">&nbsp;&nbsp;
        <p align="center"><input type="submit" value="Modify" name="B2">&nbsp;&nbsp;
        <input type="reset" value="Reset" name="B2"></p>
    </form>
    <p align="center">&nbsp;</p>
    </body>

    </html>

2 个答案:

答案 0 :(得分:1)

你必须在这样的字符串周围加上引号:

$update = "UPDATE homereg SET Fname = '{$_POST['First']}', Lname = '{$_POST['Last']}', cid = '{$_POST['cid']}', pack = '{$_POST['choice']}' WHERE dsl = '{$_POST['dsl']}'" ;

此外,不确定您是否将其删除,因为它只是一个示例,但您将要转义POST值以防止SQL注入:

$first = mysqli_real_escape_string($conn, $_POST['First']);

答案 1 :(得分:1)

如果我下面的回答不起作用,请试试这个:

 $update = "UPDATE homereg SET Fname = `$_POST['First']}`, Lname = `$_POST['Last']`, cid = `$_POST['cid']`, pack = `$_POST['choice']` WHERE dsl = `$_POST['dsl']`" ;