MySQL UPDATE不更新任何行

时间:2017-03-09 03:46:07

标签: php mysql

以下语法出了什么问题:

    if( isset($_POST['save_changes']) ) {
    // Get current id of customer
    $currentID = $_GET['id'];
    // Get Input Values
    $newfirstName        = validateInputData($_POST['first_name']);
    $newlastName         = validateInputData($_POST['last_name']);
    $newemail            = validateInputData($_POST['email']);
    $newphone            = validateInputData($_POST['phone_number']);
    $newaddressOne       = validateInputData($_POST['address_one']);
    $newaddressTwo       = validateInputData($_POST['address_two']);
    $newcounty           = validateInputData($_POST['county']);
    $newcity             = validateInputData($_POST['city']);
    $newzipCode          = validateInputData($_POST['zip_code']);
    $newprovince         = validateInputData($_POST['province']);
    $newstate            = validateInputData($_POST['state']);

    // Queries
    $query = "UPDATE customers 
              SET 
              first_name='$newfirstName', 
              last_name='$newlastName', 
              email='$newemail', 
              phone='$newphone'
              WHERE id='$currentID'
              ";
    $conn->query($query) or die($conn->error.__LINE__);

    $query = "UPDATE addresses
              SET 
              address_one='$newaddressOne', 
              address_two='$newaddressTwo', 
              county='$newcounty', 
              city='$newcity', 
              province='$newprovince', 
              zip_code='$newzipCode', 
              state='$newstate'
              WHERE customer_id='$currentID'
              ";
    $conn->query($query) or die($conn->error.__LINE__);

    // Bring user back to index
    header("Location: index.php?alert=savechanges");

    // Close connection to database
    $conn->close();
  }

上面的查询运行正常,但该行未更新。所有字段名称都是合适的。在phpMyAdmin中尝试查询时,行已更新。

请帮助,谢谢。

1 个答案:

答案 0 :(得分:-1)

您的validateInputData()函数未进行任何验证。希望它正在进行一些转义,这意味着您正在为数据库连接对象假设全局范围。您没有告诉我们这是什么类型的数据库对象。您的错误检查很差。重定向后,您不会显式退出。

除此之外,sql看起来还不错。