使用PHP $stmt格式更新mysql数据库


update mysql database with form php $stmt

我有更新我的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错误,但是没有更新数据库

您的语句中有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中,因此更改所有这些变量。