2013-10-01 66 views
0

我得到錯誤,當我嘗試更新到數據庫......錯誤說....需要幫助WHERE用PHP UPDATE子句中到MySQL

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE company_id='2'' at line 6

沒有任何一個知道第6行涉及到什麼以及如何解決這個問題!

<?php 
// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
?> 

<?php 
// Parse the form data and update company information to the system 
if (isset($_POST['company_name'])) { 

    $pid = mysql_real_escape_string($_POST['thisID']); 
    $company_name = mysql_real_escape_string($_POST['company_name']); 
    $company_url = mysql_real_escape_string($_POST['company_url']); 
    $company_username = mysql_real_escape_string($_POST['company_username']); 
    $company_password = mysql_real_escape_string($_POST['company_password']); 

    // See if that company name is an identical match to another company in the system 
    $sql = mysql_query("UPDATE company SET 
    company_name='$company_name', 
    company_url='$company_url', 
    company_username='$company_username', 
    company_password='$company_password', 
    WHERE company_id='$pid'") or die(mysql_error()); 

    header("location: company.php"); 


    exit(); 
} 
?> 


<?php 
// Gather these companies full information for inserting automatically into the edit form below on page 
if (isset($_GET['pid'])) { 
    $targetID = $_GET['pid']; 
    $sql = mysql_query("SELECT * FROM company WHERE company_id='$targetID' LIMIT 1"); 
    $productCount = mysql_num_rows($sql); // count the output amount 
    if ($productCount > 0) { 
     while($row = mysql_fetch_array($sql)){ 
      $company_id = $row["company_id"]; 
      $company_name = $row["company_name"]; 
      $company_url = $row["company_url"]; 
      $company_username = $row["company_username"]; 
      $company_password = $row["company_password"]; 

     } 
    } else { 
     echo "Sorry dude that crap dont exist."; 
     exit(); 
    } 
} 
?> 

請幫忙謝謝。

+0

*旁註:*停止使用不推薦使用的'mysql_ *'函數。嘗試PDO或MySQLi。 – Raptor

回答

2

接招逗號出後company_password='$company_password'

0

WHERE之前額外的逗號這是一個錯誤。 所以,刪除它。

$sql = mysql_query("UPDATE company SET 
    company_name='$company_name', 
    company_url='$company_url', 
    company_username='$company_username', 
    company_password='$company_password' // REMOVE COMMA FROM HERE 
    WHERE company_id='$pid'") or die(mysql_error()); 
+0

非常感謝,您的權利與逗號有關。事情已經解決了。 – andy

+0

如果這是您的解決方案,您能否將我的答案標記爲解決方案? – Pupil

0

您已將逗號(,)放在where子句之前。

company_password='$company_password' 

**,** 
WHERE company_id='$pid'"