2016-04-08 2 views
-1

私は学校のプロジェクトを行っていますが、データベースの特定の行の特定の項目を更新しようとしています私は更新しようとすると、最初のエントリだけが意図した変更を行います。ここ更新しようとしているエントリに関係なく、最初のデータベースエントリを更新するだけです

if (isset($_POST['update']) && isset($_POST['serviceNum'])) 
{ 
    $progress = get_post($conn, 'progress'); 
    $serviceNum = get_post($conn, 'serviceNum'); 
    $query = "UPDATE classics SET progress='$progress' WHERE serviceNum='$serviceNum'"; 
    $result = $conn->query($query); 
    if (!$result) echo "UPDATE failed: $query<br>" . 
     $conn->error . "<br><br>"; 
} 

そして、私の更新フォームです:

<form action="admin.php" method="post"> 
        <br> 

    <select name="progress"> 
     <option value=" "> </option> 
     <option value="In Progress">In Progress</option> 
     <option value="Completed">Completed</option> 
    </select> 

    <input type="hidden" name="update" value="progress"> 
    <input type="hidden" name="serviceNum" value="$row[0]"> 

    <br><br> 
    <input type="submit" value="UPDATE"> 
    <br><br> 
</form> 

$row[0]がserviceNumある主キーを保持している行である

は、ここに私の更新機能です。私はテーブルにネストされたフォームを持っています。ローカルホスト上でそれを実行し、ソースを見ると、各エントリがその行のデータに対して適切なserviceNumを持っていることがわかります。ただし、更新ボタンを使用するエントリに関係なく、データベーステーブルの最初のエントリのみが更新されます。

EDIT:

for ($j = 0; $j < $rows; ++$j) 
{ 
    $result->data_seek($j); 
    $row = $result->fetch_array(MYSQLI_NUM); 

    echo "<tr>"; 

    echo <<<_END 
    <td>$row[0]</td> 
    <td>$row[1]</td> 
    <td>$row[2]</td> 
    <td>$row[3]</td> 
    <td>$row[4]</td> 
    <td> 

     <!-- Trigger Update Modal --> 
     <center> 
     <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal"> <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> </button> 
     </center> 

     <!-- Update Modal --> 

     <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
      <div class="modal-dialog"role="document"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
          <span aria-hidden="true">&times;</span> 
         </button> 
         <h4 class="modal-title" id="myModalLabel">Update Record</h4> 
        </div> 
        <div class="modal-boday"> 
         <center> 
         <form action="admin.php" method="post"> 
          <br> 

          <select name="progress"> 
           <option value=" "> </option> 
           <option value="In Progress">In Progress</option> 
           <option value="Completed">Completed</option> 
          </select> 

          <input type="hidden" name="update" value="yes"> 
          <input type="hidden" name="serviceNum" value="$row[0]"> 

          <br><br> 
          <input type="submit" value="UPDATE"> 

EDIT 2:MODAL

      <center> 
           <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal"> 
            <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> 
           </button> 
          </center> 

          <!-- Update Modal --> 

          <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
           <div class="modal-dialog" role="document"> 
            <div class="modal-content"> 
             <div class="modal-header"> 
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
               <span aria-hidden="true">&times;</span> 
              </button> 
              <h4 class="modal-title" id="myModalLabel">Update Record</h4> 
             </div> 
             <div class="modal-boday"> 
             <center> 
              <form action="admin.php" method="post"> 
               <br> 
               <select name="progress"> 
                <option value=" "> </option> 
                <option value="In Progress">In Progress</option> 
                <option value="Completed">Completed</option> 
               </select> 

               <input type="hidden" name="update" value="status"> 
               <input type="hidden" name="ticketnum" value="$row[0]"> 
               <br><br> 
               <input type="submit" value="UPDATE"> 
               <br><br> 
              </form> 
             </center> 
            </div> 
           </div> 
          </div> 

          <!-- End Update Modal --> 
+1

興味があるだけ、なぜライン '$ serviceNum = get_post($ CONN、 'serviceNum');'代わりに、単純に '$ serviceNum = $ _POST [ 'serviceNum']の; '? – larsAnders

+0

この 'get_post()'はPHPの組み込みではありません。これを読む。 http://stackoverflow.com/questions/4068855/php-having-a-problem-with-get-post –

+0

あなたは以下の回答を持っています。彼らと一緒に持ち帰ります –

答えて

1

問題は、テーブルのすべての行で同じid="update-modal"を使用していることです。 IDは一意でなければならず、IDを参照すると常に最初のIDが返されます。だからdata-target="#update-modal"は、あなたがクリックしたものにかかわらず、常に最初の行のモーダルを対象にしています。

ID番号update-modalに商品IDを追加します。

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal{$row[0]}"> 

<div class="modal fade" id="UpdateModal{$row[0]}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
+0

ありがとうございました。これは完璧に動作します。 – cneil18

関連する問題