2017-08-13 164 views
0

我正在創建一個小項目來解釋我的學生如何使用PHP代碼更新SQL數據庫中的值。我在MySQL中創建了所有字段爲VARCHAR的表。我寫了以下代碼,其中引發以下錯誤: 更新值時出錯您的SQL語法中有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的'Mbps WHERE dsl ='25610669'附近使用正確的語法,其中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> 

回答

0

如果我下面的回答不行,試試這個:

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

非常感謝你Luki Centuri。這工作。我忘了用單引號括住字符串值。再次感謝! – YusufSM

+0

不客氣:) –

0

你必須把字串,報價是這樣的:

$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']);