2016-11-16 5 views
-2

フォーム入力で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エラーはありませんが、データベースは更新されません。

+0

*問題がありますか?...何が問題ですか?エラーメッセージが表示されますか?何が望ましい行動であり、何が起こっているのですか? – Phiter

+0

構文や表示されるPHPエラーはありませんが、フォームが送信されると、入力された情報でデータベースが更新されることはありません。 – Scary

+0

スーパーグローバルをダンプしてみてください。あなたはGETとPOSTを混同し、プレースホルダに値を設定します。 – Progrock

答えて

0

あなたの明細には、UPDATEINSERTの両方のビットがあります。それはちょうど次のようになります。

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

もう一つの問題は、あなたのフォームがmethod="POST"を使用していることである:

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

あなたは追加のパラメーターをバインドする必要があります。つまり、すべてのパラメータがで、$_GETではなく、すべての変数を変更します。

+0

私はこれを修正しましたが、フォームはまだデータベースを更新しません。 if文で何か問題がありますか? – Scary

+0

あなたのフォームは 'method =" POST "'を持っていますが、すべての入力に '$ _GET'を使っています。それを '$ _POST'に変更してください。 – Barmar

+0

通知:未定義のインデックス:ID:C:\ xampp \ htdocs \ School \ Week行4のContact Manager \ updateclients.php 警告:PDOStatement :: execute():SQLSTATE [HY093]:無効パラメータ番号:パラメータがC:\ xampp \ htdocs \ School \ Weekに定義されていません4 \ Contact Manager \ updateclients.php 93行目 – Scary

関連する問題