2017-07-29 19 views
0

これは間違っていませんが、エラーはありませんが、更新クエリについては機能しません。私はその最新のコードまたは古いコードがわからない。どうすればいいのか教えてください。ありがとう、ここにコードです。最初のコードはserver.phpというPHPでUPDATE QUERYにエラーはありませんが動作しません。

はここ構造ですされています

<?php 

     session_start(); 

     $username = ""; 
     $password = ""; 
     $lastname = ""; 
     $firstname = ""; 
     $id = 0; 
     $edit_state = false; 


     //connect to the database 
     $db = mysqli_connect('localhost', 'root', '', 'login'); 

     // button is clicked 
     if (isset($_POST['save'])) { 
      $username = $_POST['text_username']; 
      $password = $_POST['text_password']; 
      $lastname = $_POST['text_lastname']; 
      $firstname = $_POST['text_firstname']; 
      //adding data in to database 
      $query = "INSERT INTO users (username, password, lastname, firstname) values ('$username', '$password', '$lastname', '$firstname')"; 
      mysqli_query($db, $query); 
      $_SESSION['msg'] = "Account Saved!"; 
      header('location: acc-settings.php'); 
     } 


     //update records in the database 
     if (isset($_POST['update'])) { 
      $username = mysqli_real_escape_string($_POST['text_username']); 
      $password = mysqli_real_escape_string($_POST['password']); 
      $lastname = mysqli_real_escape_string($_POST['lastname']); 
      $firstname = mysqli_real_escape_string($_POST['firstname']); 
      $id = mysqli_real_escape_string($_POST['id']); 

      mysqli_query($db, "UPDATE users SET username = '$username', password = '$password', lastname = '$lastname', firstname = '$firstname' where id='$id'"); 
      $_SESSION['msg'] = "Account Updated!"; 
      header('location: acc-settings.php'); 
     } 
     //retrieve records 
     $results = mysqli_query($db, "SELECT * FROM users"); ?> 

これは、ACC-のsettings.php

<?php include 'server.php'; 

     //fetching the record 
     if (isset($_GET['edit'])) { 
      $id = $_GET['edit']; 

      $rec = mysqli_query($db, "SELECT * FROM users where id=$id"); 
      $record = mysqli_fetch_array($rec); 
      $username = $record['username']; 
      $password = $record['password']; 
      $lastname = $record['lastname']; 
      $firstname = $record['firstname']; 
      $id = $record['id']; 
     } 

      ?> 

    <!DOCTYPE html> 
    <html> 
    <head> 
     <title>Account Settings</title> 
     <link rel="stylesheet" type="text/css" href="css/acc-style.css"> 
    </head> 
    <body> 

      <?php if (isset($_SESSION['msg'])): ?> 
      <div class="msg"> 
       <?php 
       echo $_SESSION['msg']; 
       unset($_SESSION['msg']); 
       ?> 
      </div> 

      <?php endif ?> 
    <table> 
     <thead> 
      <tr> 
       <th>Username</th> 
       <th>Password</th> 
       <th>Lastname</th> 
       <th>Firstname</th> 

       <th colspan="2">Action</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php while ($row = mysqli_fetch_array($results)) { ?> 
        <tr> 
        <td><?php echo $row['username']; ?></td> 
        <td><?php echo $row['password']; ?></td> 
        <td><?php echo $row['lastname']; ?></td> 
        <td><?php echo $row['firstname']; ?></td> 
        <td> 
         <a href="acc-settings.php?edit=<?php echo $row['id']; ?>">Edit</a> 
        </td> 
        <td> 
         <a href="#">Delete</a> 
        </td> 
        </tr> 
      <?php } ?> 



    </tbody> 
    </table> 
    <form method="post" action="#"> 
    <input type="hidden" name="text_id" value="<?php echo $id; ?>"> 
    <div class="input-group"> 
     <label>Username</label> 
     <input type="text" name="text_username" value="<?php echo 
    $username; ?>"> 
    </div> 
    <div class="input-group"> 
     <label>Password</label> 
     <input type="text" name="text_password" value="<?php echo 
    $password; ?>"> 
    </div> 
    <div class="input-group"> 
     <label>Lastname</label> 
     <input type="text" name="text_lastname" value="<?php echo 
    $lastname; ?>">`enter code here` 
    </div> 
    <div class="input-group"> 
     <label>Firstname</label> 
     <input type="text" name="text_firstname" value="<?php echo 
    $firstname; ?>"> 
    </div> 
    <div class="input-group"> 
    <?php if ($edit_state == true): ?> 
     <button type="submit" name="save" class="btn">Save</button> 
    <?php else: ?> 
     <button type="submit" name="update" class="btn">Update</button> 
    <?php endif ?> 
    </div> 
</form> 

</body> 
</html> 

で、このコードの間違っているものを私に教えてください:(

+0

あなたのスクリプトは[SQLインジェクション攻撃](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)の危険性があります [あなたがエスケープしている場合入力、その安全ではない!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) [準備されたパラメータ化されたステートメント](http:// php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly

+0

更新文のチェックを追加します。エラーがあります – Akintunde007

+0

@ RiggsFolly私のコードでそのコードをどうやって適用できますか? ;( – Dave

答えて

1

フォーム入力の名前と$ _POSTキーに不一致があります(例:

)。問題が理解できない場合はコメントしてください。 = "text_id"
<input type="hidden" name="text_id" value="<?php echo $id; ?>"> 

名が、$ _POST [ 'ID']使用して、ない$ _POST [ 'text_id']

他の多くのフィールドが同じ問題を抱えています。

+0

イム申し訳ありません[「text_id」] – Dave

+0

、それについての正しい構文は何ですか?私のデータベースのtextbotまたは行の名前ですか?ありがとうございます:) – Dave

関連する問題