使用表单php $ stmt更新mysql数据库

时间:2016-11-16 23:11:35

标签: php mysql

我在使用表单输入更新MySQL数据库时遇到问题。我认为这是ID或其他问题,但我不确定。

以下是完整文件:

<?php

session_start();

$_SESSION["message"] = '<p class="message">Client updated successfully!</div>';

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ssl";
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Contact Manager - Client Directory</title>
<link type="text/css" rel="stylesheet" href="assets/custom/css/main.css">
<link type="image/ico" rel="icon" href="assets/custom/images/favicon.ico">
</head>
<body>
  <div id="container">
     <div class="header">
       <div class="logo">
    <h1>Contact Manager</h1>
  </div>
  <nav>
    <ul>
      <a href="index.php"><li>Home</li></a>
      <a href="clientdirectory.php"><li>Client Directory</li></a>
      <a href="admin.php"><li>Admin</li></a>
    </ul>
  </nav>
</div> <!-- header div -->
<div class="clear"></div>
<div class="content">
  <div class="inner-container">
    <div class="inner-header">
      <h2>Control Panel > Update Clients</h2>
      <?php
        $stmt = $dbh->prepare('select id, firstname, lastname, username, password, email, phone from users');
        $stmt->execute();
        $result = $stmt->fetchall(PDO::FETCH_ASSOC);
        foreach ($result as $row) {
          echo '<div class="employee-inner">';
          echo '<div class="employee">';
          echo '<h3>ID</h3>' . '<p>' . $row['id'] . '</p>';
          echo '<div class="clear"></div>';
          echo '</div>';
          echo '<form enctype="multipart/form-data" action="updateclients.php" method="POST">';
          echo '<input class="update" type="text" name="firstname" placeholder=' . $row['firstname'] . ' required />';
          echo "<br />";
          echo '<input class="update" type="text" name="lastname" placeholder=' . $row['lastname'] . ' required />';
          echo "<br />";
          echo '<input class="update" type="text" name="username" placeholder=' . $row['username'] . ' required />';
          echo "<br />";
          echo "<input class='update' type='password' name='password' placeholder='Password' required />";
          echo "<br />";
          echo '<input class="update" type="text" name="email" placeholder=' . $row['email'] . ' required />';
          echo "<br />";
          echo '<input class="update" type="text" name="phone" placeholder=' . $row['phone'] . ' required />';
          echo "<br />";
          echo '<input class="update-submit" type="submit" name="update" value="Update Client" />';
          echo '</form>';
          echo '<a href="deleteclients.php?id='.$row['id'].'"><button>Delete Client</button></a>';
          echo '</div>';
        }
        if (isset($_GET['update'])) {
          $employeeid = $_GET['id'];
          $firstname = $_GET['firstname'];
          $lastname = $_GET['lastname'];
          $username = $_GET['username'];
          $password = $_GET['password'];
          $email = $_GET['email'];
          $phone = $_GET['phone'];
          $encrypted = md5("encrypted".$password);
          $stmt = $dbh->prepare("update users set firstname='" . $firstname . "', lastname='" . $lastname . "', username='" . $username . "'. password='" . $password . "', email='" . $email . "', phone='" . $phone . "' values (:firstname, :lastname, :username, :encrypted, :email, :phone);");
          $stmt->bindParam(':firstname', $firstname);
          $stmt->bindParam(':lastname', $lastname);
          $stmt->bindParam(':username', $username);
          $stmt->bindParam(':encrypted', $encrypted);
          $stmt->bindParam(':email', $email);
          $stmt->bindParam(':phone', $phone);
          $stmt->execute();
          echo '<p class="message">Client updated successfully!</p>';
        }
      ?>
    </div>
  </div>
  <div class="clear"></div>
  <footer>
    <p>Copyright &copy 2016 Content Manager. All rights reserved.</p>
  </footer
</div> <!-- content div -->
</div> <!-- container div -->
</body>
</html>

任何信息都会有所帮助。谢谢,我很感激。没有PHP错误,但它没有更新数据库。

1 个答案:

答案 0 :(得分:0)

您的语句中包含UPDATEINSERT语法的位。它应该只是:

$stmt = $dbh->prepare("update users set firstname=:firstname, lastname=:lastname, username=:username, password=:password, email=:email, phone=:phone
                        where id = :id");

您需要绑定其他参数:

$stmt->bindParam(':id', $id);

另一个问题是您的表单使用method="POST"。这意味着所有参数都在$_POST,而不是$_GET,因此请更改所有这些变量。