2016-11-16 178 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