2016-11-16 179 views
-2

我在使用表单输入更新我的MySQL数据库时遇到问题。我相信这是一个关于身份证或其他事情的问题,但我不确定。

以下是完整的文档:

<?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错误,但它不更新数据库。

+0

*我有问题* ...有什么问题?你是否收到任何错误讯息?什么是期望的行为,到底发生了什么? – Phiter

+0

没有语法或显示的php错误,但是当表单被提交时,它不会用输入的信息更新数据库。 – Scary

+0

尝试倾销你的超级全局变量。您正在混淆GET和POST,并使用值构成占位符。 – Progrock

回答

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,因此请更改所有这些变量。

+0

我已更正此错误,但窗体仍不更新数据库。我是否在if语句中做错了什么? – Scary

+0

您的表单具有'method =“POST”',但是您使用'$ _GET'作为所有输入。将其更改为'$ _POST'。 – Barmar

+0

我收到通知:Undefined index:id in C:\ xampp \ htdocs \ School \ Week 4 \ Contact Manager \ updateclients.php on line 77 Warning:PDOStatement :: execute():SQLSTATE [HY093]:Invalid参数号:参数没有在第93行的C:\ xampp \ htdocs \ School \ Week 4 \ Contact Manager \ updateclients.php中定义 – Scary