2014-02-16 111 views
-1

我想覆蓋MySQL中的當前數據以便能夠更新所有內容。 我是新來這個我沒有看到下面的代碼的任何錯誤:用PHP更新MySQL查詢

PHP代碼:

<?php 
// see if the form has been completed 
if (isset($_POST['submit'])){ 
    $firstname = $_POST['firstname']; 
    $surname = $_POST['surname']; 

    if($firstname && $surname){ 
     // connect to the server 
     include_once("php_includes/db_conx.php"); 

     // check if that user exist 
     $exists = mysql_query ("SELECT * FROM users WHERE firstname='$firstname'") or die ("the query could not be connected"); 
     if (mysql_num_rows ($exists) != 0) { 
      // update the description in the database 
      mysql_query("UPDATE firstname SET surname='$surname' WHERE firstname='$firstname'") or die ("update could not be applied"); 
      echo "successful"; 
     } else echo "the name does not edist"; 
    } else echo "you need to enter both of the fields try again:"; 
} 
?> 

我得到的錯誤是

查詢無法連接

但我試過查詢,它很好。

HTML:

<html> 
<head> 
<title>update MySql form</title> 
</head> 
<body> 

<div id="pageMiddle"> 

    <form action="user1.php" method="POST"> 
     <div> 
     <p>First Name: <input type="text" name="firstname" id="firstname" ></p> 
     <p>Surname: <input type="text" name="surname" id="surname"></p> 
     <p><input type="submit" name="submit" id="submit" value="Update Description"></p> 
     </div> 
    </form> 

</body> 
</html> 
+0

你應該真的讀這個http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Fabio

+0

更新查詢錯誤我討厭人們只是看看對我說我是新手 – user3311898

+1

而不是'死(「查詢無法連接」)'嘗試'死(mysql_error())' –

回答

0

你的表被稱爲 '用戶',但你對姓運行UPDATE。將其更改爲:

UPDATE users SET surname... 

然後做

$exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $firstname . "'") 

以隔離是否有該變量的值。我會建議打印查詢字符串用於測試目的。

+0

我試過,我仍然得到相同的結果 – user3311898

+0

我仍然得到相同的結果有人可能會顯示更好的方式我不介意從頭開始 – user3311898