duanguilin2007 2015-11-27 09:39
浏览 16
已采纳

数据库在PHP / MySQL中无法正确更新[重复]

This question already has an answer here:

I've got a database with a Users table which I'm trying to update.

Currently I have customers.php, which displays form fields with the user information so it can be updated.

This form points to edit_customer_processor.php , which takes the new values, puts them into a MYSQL query... and then despite the query working correctly when I query the DB via the PHPMyAdmin command line, the record doesn't update.

customers.php

<?php
  session_start();
  if(!$_SESSION["logged_in"]){
    header("location:home.php");
    die;
  }
?>
<?php include 'header.html'; ?>
<div id='maincontent'>

<?php

  if (isset($_GET["id"])){
    $customer_id = $_GET["id"];

    require_once('config.php');

    $customer_query = "SELECT * FROM customer WHERE customer_id = $customer_id";
    $customer_results = mysql_query($customer_query, $conn);
    if (!$customer_results) {
      die ("Error selecting car data: " .mysql_error());
    }
    else {
      while ($row = mysql_fetch_array($customer_results)) {
        echo "<h3>Edit Customer</h3>";
        echo "<FORM method='post' action='edit_customer_processor.php'>";
          echo '<p> Name: <input type="text" name="name" size = "40" value=' . $row[name] . '></p>';
          echo '<p> Address: <input type="text" name ="address" size="40" value=' . $row[address] . '></p>';
          echo '<p> Email: <input type="text" name="email" value=' . $row[email] . '></p>';
          echo '<p> Phone: <input type ="text" name="phone" size="20" value=' . $row[phone] . '></p>';
          echo '<input type ="hidden" name="customer_id" value="' . $row[customer_id] . '">';
          echo '<input type ="hidden" name="formtype" value="edit_customer">';
          echo '<input type="submit" name="submit" value= "Update">';
        echo '</form>';
      }
    }
  } else {
    // If there isn't an ID, display the New Customer form and all customers below, with links
    // to their edit pages.

    echo "<h3>Enter new customer information and submit.</h3>";

    echo "<FORM method='post' action='new_customer_processor.php'>";

      echo '<p> Name: <input type="text" name="name" size = "40"></p>';
      echo '<p> Address: <input type="text" name ="address" size="40"></p>';
      echo '<p> Email: <input type="text" name="email"></p>';
      echo '<p> Phone: <input type ="text" name="phone" size="20"></p>';
      echo '<input type ="hidden" name="formtype" value="new_customer">';
      echo '<input type="submit" name="submit" value= "Submit">';
      echo '<input type ="reset" name="reset" value ="Reset">';

    echo '</form>';

    require_once('config.php');
    echo "<h3>Current Customers</h3>";
    $query = "SELECT * FROM customer";
    $results = mysql_query($query, $conn);
    if (!$results) {
      die ("Error selecting customer data: " .mysql_error());
  }
  else {
    // In the absence of an ID, all customers will be displayed down
    // the bottom of the page
    while ($row = mysql_fetch_array($results)) {
      echo "<a href=customers.php?id=";
      echo $row[customer_id];
      echo "><p> $row[name] </p></a>";
      echo "<p> $row[address] </p>";
      echo "<p> $row[phone] </p>";
      echo "<p> $row[email] </p>";
    }
  }
      }
?>
<a href="customers.php">Back to Customers Page</a>

</div>
<?php include 'footer.html' ?>

edit_customer_processor.php

<?php include 'header.html' ?>
  <div id="maincontent">
<?php
  // Pulling in hidden customer ID from post value
  $mysqli = new mysqli( 'localhost', 'root', 'root', 'w_c_a' );

    // Check our connection
    if ( $mysqli->connect_error ) {
      die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
    }
  // Insert our data
  $sql = mysql_query("UPDATE customer
     SET name = '".mysql_real_escape_string($_POST['name'])."',
     address = '".mysql_real_escape_string($_POST['address'])."',
     phone = '".mysql_real_escape_string($_POST['phone'])."',
     email = '".mysql_real_escape_string($_POST['email'])."'
     WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");
  $update = $mysqli->query($sql);

  echo "Customer updated: ";
  echo "<a href=customers.php?id=" . $_POST['customer_id'] . ">";
  echo "Back to Edit Customer</a>";
  ?>
</div>
<?php include 'footer.html' ?>

And when I echo the MYSQL query, I get:

UPDATE customer SET name = 'Kellyassdsa', address = 'ads', phone = '0260123123', email = 'asdasd' WHERE customer_id='1'

Which works when I put it in PHPMyAdmin.

I know it'll be some boneheaded little mistake, but I've been trying to get this work for ages now. Any ideas?

</div>
  • 写回答

3条回答 默认 最新

  • dongmin1166 2015-11-27 10:21
    关注

    your problem is...

    $sql = mysql_query(..);
    
    $update = $mysqli->query($sql);
    

    it should be

    $sql = 'UPDATE ...';
    
    $update = $mysqli->query($sql);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题