更新查询不更新记录

时间:2018-03-17 10:18:57

标签: php

我完全不知道为什么我的更新查询没有更新记录。检查器控制台中没有错误。如果我在phpmyadmin中运行查询,用实际值替换变量,它就可以正常工作。

我已尝试在查询中对这些vars进行编码:'".$name."',也就像我现在拥有它一样。所有字段名称都是正确的,所有值都正确传递给php。如果有人能指出我的错误,我会很感激,因为它让我疯了。非常感谢

<?php

$conn = mysqli_connect("localhost","root","","domain");

if($conn === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}

  $id = mysqli_real_escape_string($conn, $_POST['idcon']);
  $company = mysqli_real_escape_string($conn, $_POST['companycon']);
  $name = mysqli_real_escape_string($conn, $_POST['namecon']);
  $email = mysqli_real_escape_string($conn, $_POST['emailcon']);
  $phone = mysqli_real_escape_string($conn, _POST['phonecon']);
  $fax = mysqli_real_escape_string($conn, $_POST['faxcon']);
  $mobile = mysqli_real_escape_string($conn, $_POST['mobilecon']);

  $sql = mysqli_query($conn, "UPDATE contact_con SET idcode_con = '$company', name_con = '$name', email_con = '$email', phone_con = '$phone', fax_con = '$fax', mobile_con = '$mobile' WHERE id_con='$id'");

  mysqli_close($conn);

?>

1 个答案:

答案 0 :(得分:2)

您应该使用prepared queries,也有拼写错误_POST['phonecon']

<?php
$conn = mysqli_connect("localhost", "root", "", "domain");

// check connection
if (mysqli_connect_errno()) {
    exit("Connect failed: ". mysqli_connect_error());
}

// create a prepared statement
$stmt = $conn->prepare("
    UPDATE contact_con 
    SET idcode_con = ?,
        name_con = ?, 
        email_con = ?, 
        phone_con = ?, 
        fax_con = ?, 
        mobile_con = ?
    WHERE id_con= ?
");

if ($stmt) {
    // bind parameters for markers
    $stmt->bind_param(
        "ssssssi", 
        $_POST['companycon'],
        $_POST['namecon'], 
        $_POST['emailcon'], 
        $_POST['phonecon'], 
        $_POST['faxcon'], 
        $_POST['mobilecon'],
        $_POST['idcon']
    );

    // execute query
    $stmt->execute();

    // close statement
    $stmt->close();
}

// close connection
$conn->close();
?>