在PHP更新到MySQL的WHERE子句中需要帮助

时间:2013-10-01 04:28:19

标签: php mysql sql manual

当我尝试更新到数据库时,我收到错误......错误说....

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();
    }
}
?>

请帮助谢谢。

3 个答案:

答案 0 :(得分:2)

company_password='$company_password'

之后删除该逗号

答案 1 :(得分: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());  

答案 2 :(得分:0)

你在where子句之前放了逗号(,)。

company_password='$company_password'

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