无法更新数据库

时间:2013-10-29 08:30:56

标签: php sql-server

我想从数据库更新我的数据,但不幸的是我无法更新。 有人可以帮助我。

我将不胜感激。

问题是我的代码出错时无法显示错误。 抱歉我的英语不好。

这是我的代码

<?
include "new.php";
$response = array("updated data", 1);
if (isset($_POST['ID_Person']) && isset($_POST['FirstName']) && isset($_POST['MiddleName']) && isset($_POST['LastName']) && isset($_POST['AliasName']) 
&& isset($_POST['Gender']) && isset($_POST['CityBirth']) && isset($_POST['DateBirth']) && isset($_POST['MonthBirth']) && isset($_POST['YearBirth'])) 
{
    $id = $_POST['ID_Person'];
    $name =  $_POST['FirstName'];
    $middle =  $_POST['MiddleName'];
    $last =  $_POST['LastName'];
    $alias =  $_POST['AliasName'];
    $gender =  $_POST['Gender'];
    $citybirth =  $_POST['CityBirth'];
    $datebirth =  $_POST['DateBirth'];
    $monthbirth =  $_POST['MonthBirth'];
    $yearbirth =  $_POST['YearBirth'];
    $hasil = sqlsrv_query($conn,"UPDATE T_Person SET
        First_Name_Person = '$name' ,
        Middle_Name_Person = '$middle' ,
        Last_Name_Person = '$last' ,
        Alias_Person = '$alias',
        Gender_Person = '$gender', 
        City_Birth_Person = '$citybirth', 
        Date_Birth_Person = '$datebirth', 
        Month_Birth_Person = '$monthbirth',
        Year_Birth_Person = '$yearbirth',
        WHERE ID_Person = '$id'"
    );
    $rows_affected = sqlsrv_rows_affected($hasil);
    if ($rows_affected === false) 
    {
        die( print_r( sqlsrv_errors(), true));
    }       
    if ($hasil)
    {
        $response["success"] = 1;
        $response["message"] = "Product successfully edit.";
        // echoing JSON response
        echo json_encode($response);
    }
}
else {
    echo 'Data fail update';
}
?>

3 个答案:

答案 0 :(得分:0)

您在WHERE

之前使用逗号juste查询时出错
$hasil = sqlsrv_query($conn,"UPDATE T_Person SET First_Name_Person = '$name' , 
                                                 Middle_Name_Person = '$middle' , 
                                                 Last_Name_Person = '$last' ,
                                                 Alias_Person = '$alias',
                                                 Gender_Person = '$gender', 
                                                 City_Birth_Person = '$citybirth', 
                                                 Date_Birth_Person = '$datebirth', 
                                                 Month_Birth_Person = '$monthbirth', 
                                                 Year_Birth_Person = '$yearbirth',
                                                                 no comma here   ^
                                              WHERE ID_Person = '$id'");

尝试在PHP中echo查询并在phpMyAdmin中复制粘贴此查询。

答案 1 :(得分:0)

请在此之后删除逗号,

Year_Birth_Person = '$yearbirth'

答案 2 :(得分:0)

我猜你的表中没有带有ID_Person == $ id的T_Person所以没有更新