如何解决更新查询错误

时间:2015-11-20 09:54:16

标签: php mysql

我的错误是

  

更新时出错:您的SQL语法出错;检查   手册,对应右边的MySQL服务器版本   在第1行“1”附近使用的语法

请记下在没有错误的情况下没有错误在帖子中我检查了每一位..更新查询导致该错误。我尝试了很多次但没有解决。提前致谢。

<?php

include('db.php');
session_start();
if(isset($_POST['update']))
    {
        $fname = mysql_real_escape_string($_POST['fname'], $con1);
        $lname = mysql_real_escape_string($_POST['lname'], $con1);
        $phone = mysql_real_escape_string($_POST['phone'], $con1);
        $zip = mysql_real_escape_string($_POST['zip'], $con1);
        $city = mysql_real_escape_string($_POST['city'], $con1);
        $country = mysql_real_escape_string($_POST['country'], $con1);
        $userId = $_SESSION['userID'];

        $strSQL = mysql_query("SELECT * FROM sarah_cloudRecord.user_info WHERE userId = '$userId'" , $con1);

        //$Results = mysql_fetch_array($strSQL);
        $numrows = mysql_num_rows($strSQL);

    if($numrows != 0)
    {

        while ($row = mysql_fetch_assoc($strSQL))
        {

        $dblname = $row['lastName'];
        $dbfname = $row['firstName'];
        $dbphone = $row['phone'];
            $dbzip = $row['zipCode'];
            $dbcity = $row['city'];
            $dbcountry = $row['country'];



        if ($fname != $dbfname || $fname == $dbfname) 
        {
            $newfName = $fname;
        }

        if ($lname != $dblname || $lname == $dblname) 
        {
            $newlName = $lname;
        }

        if ($phone != $dbphone || $phone == $dbphone) 
        {
            $newPhone = $phone;
        }

        if ($zip != $dbzip || $zip == $dbzip) 
        {
            $newZip = $zip;
        }

        if ($city != $dbcity || $city == $dbcity) 
        {
            $newCity = $city;
        }

        if ($country != $dbcountry || $country == $dbcountry) 
        {
            $newCountry = $country;
        }

        }

    $updateSQL = mysql_query("UPDATE sarah_cloudRecord.user_info SET firstName = '$newfName', lastName = '$newlName', phone = '$newPhone', city = '$newCity', zipCode = '$newZip', country = '$newCountry' WHERE userId = '$userId'", $con1);

        if (mysql_query($updateSQL, $con1)) 
        {
                echo "Update Successfully!";
        }
            else {
            echo "Error while updating: " . mysql_error($con1);
        }

    }

    else {

    echo "no records found";

    }

    }


        else
        {
            ?>

        <script type="text/javascript">
        window.location = "http://dev.metawarez.com/cloud/login.php";
        </script>
    <?php
        }        

    ?>

2 个答案:

答案 0 :(得分:1)

两次使用mysql_query。您将更新查询更改为

$updateSQL = "UPDATE sarah_cloudRecord.user_info SET firstName = '$newfName', lastName = '$newlName', phone = '$newPhone', city = '$newCity', zipCode = '$newZip', country = '$newCountry' WHERE userId = '$userId'";

if (mysql_query($updateSQL, $con1))
  

注意: - 不推荐使用mysql而是使用mysqli或pdo

答案 1 :(得分:1)

您正在查询UPDATE的回复,​​即1来自哪里。

http://php.net/manual/de/function.mysql-query.php

$updateSQL = mysql_query("UPDATE sarah_cloudRecord.user_info SET firstName = '$newfName', lastName = '$newlName', phone = '$newPhone', city = '$newCity', zipCode = '$newZip', country = '$newCountry' WHERE userId = '$userId'", $con1);

if ($updateSQL !== false) 
{
    echo "Update Successfully!";
} else {
    echo "Error while updating: " . mysql_error($con1);
}