更新/编辑php mysqli中的用户

时间:2017-09-13 11:03:48

标签: php mysqli

使用php mysqli更新/编辑用户的问题

我已成功创建了管理信息中心,但我的更新/编辑功能无效。

请检查我的以下代码&让我知道我哪里错了。

请在下面找到我的代码:

// Php Code就在这里

<?php
require_once('includes\database.php');
?>

<?php

$id= $_GET['id'];

$query= "SELECT * FROM customer
        INNER JOIN customer_address
        ON customer.id=customer_address.customer
        WHERE customer.id=$id";
$mysqli->query($query);

if($result= $mysqli->query($query)){
    while($row=$result->fetch_assoc()){

        $name= $row['name'];
        $email= $row['email'];
        $phone= $row['phone'];
        $password= $row['password'];
        $image= $row['image'];
        $address= $row['address'];
        $Country= $row['Country'];
        $City= $row['City'];
        $Zip_code= $row['Zip_code'];
    }

    $result->close();

}


?>

<?php


    if ($_POST) {


$id= $_GET['id'];


    $name= mysqli_real_escape_string($_POST['name']);
    $email= mysqli_real_escape_string($_POST['email']);
    $phone= mysqli_real_escape_string($_POST['phone']);
    $password= md5(mysqli_real_escape_string($_POST['password']));
    $image= mysqli_real_escape_string($_POST['image']);
    $address= mysqli_real_escape_string($_POST['address']);
    $Country= mysqli_real_escape_string($_POST['Country']);
    $City= mysqli_real_escape_string($_POST['City']);
    $Zip_code= mysqli_real_escape_string($_POST['Zip_code']);

    $sql= "UPDATE customer SET 
    name='$name',
    email='$email',
    phone='$phone',
    password='$password',
    image='$image'
    WHERE id=$id;

    $res= $mysqli->query($sql)";

    $sql= "UPDATE customer_address SET 
    address='$address',
    Country='$Country',
    City='$City',
    Zip_code='$Zip_code',
    WHERE customer=$id";

    $mysqli->query($sql);

    exit;
}


?>

// HTML

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CManager | Edit Customer</title>
    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="css/jumbotron-narrow.css" rel="stylesheet">
  </head>
  <body>
    <div class="container">
      <div class="header">
        <ul class="nav nav-pills pull-right">
          <li class="active"><a href="index.php">Home</a></li>
          <li><a href="add_customer.php">Add Customer</a></li>
        </ul>
        <h3 class="text-muted">Store CManager</h3>
      </div>

      <div class="row marketing">
        <div class="col-lg-12">
         <h2>Edit Info</h2>
         <table class="table table-striped">

        <form method="POST" action="add_customer.php">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" required="true" value="<?php echo $name; ?>"></td>
</tr>

<tr>
<td>Email:</td>
<td><input type="email" name="email" required="true" value="<?php echo $email; ?>"></td>
</tr>

<tr>
<td>Phone:</td>
<td><input type="phone" name="phone" required="true" value="<?php echo $phone; ?>"></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="password" name="password" required="true" value="<?php echo $password; ?>"></td>
</tr>

<tr>
<td>Image:</td>
<td><input type="text" name="image" required="true" value="<?php echo $image; ?>"></td>
</tr>

<tr><td><h2>Other Details:</h2><td></tr>

<tr>
<td>Address:</td>
<td><input type="text" name="address" value="<?php echo $address; ?>"></td>
</tr>

<tr>
<td>Country:</td>
<td><input type="text" name="Country" value="<?php echo $Country; ?>"></td>

 </select> 
</tr>

<tr>
<td>City:</td>
<td><input type="text" name="City" value="<?php echo $City; ?>"></td>
</select>
</tr>

<tr>
<td>Zip Code:</td>
<td><input type="text" name="Zip_code" value="<?php echo $Zip_code; ?>"></td>
</tr>

<tr><td><input type="submit" value="update" name="submit"></td></tr>

</table>
</form>



        </table>
        </div>


      </div>

      <div class="footer">
        <p>&copy; Company 2014</p>
      </div>

    </div> 
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

有两个问题。

<强>更新

要更新您必须获取ID的字段。你不是通过GET的POST发送它(表单动作有nog querystring)。

您尝试获取此ID:

$id = $_GET['id'];

但要完成这项工作,你必须在FORM动作中添加?id = X. 或者向FORM添加一个HIDDEN FIELD并使用

$id = $_POST['id'];

<强>插入

您无法使用UPDATE来插入新客户。所以你必须为此做一个INSERT QUERY:

INSERT INTO `table` (`column1`, `columns2`) VALUES ('value1', 'value2');