简体   繁体   中英

Update 1 column of all rows in the database table

Hopefully someone can help me with this " easy " question.

I have data in a table that has been gathered from a mysql database. I want the user to be able to edit the "PRICE" column to any and all rows, then press the update button which will send the data to the table and update all the rows in the "PRICE" column.

For the life of me I can't get the database to update. I know it has to be something that I'm missing. And it has to be something so easy it's laughable.

Please help?

if (isset($_POST['update']))
  {
  echo '<pre>';
  print_r($_POST);
  echo '</pre>';
  if (is_array($ID))
    {
    foreach($_POST['hidden'] AS $ID)
      {
      echo "ID is: " . $ID . "</br>";
      echo "Price is: " . $pricing . "</br>";
      $ID = mysqli_real_escape_string($conn, $_POST['hidden'][$ID]);
      $pricing = mysqli_real_escape_string($conn, $_POST['price'][$ID]);
      $updateQuery = 'UPDATE `bathroom_price` SET `price` ="' . $pricing . '" WHERE `ID`=' . $ID;
      mysqli_query($conn, $updateQuery) or die(mysql_error());
      }
    }
  }

?>
</head>
<?php
mysqli_select_db($conn, "table_name");
?>


<div class="row center-xs">
      <div style="margin-top:100px;" class="col-xs-12 col-sm-12 col-md-12 col-lg-8">
          <div class="box">
              <form method=POST>
                  <h1>Price List for Bathroom Form</h1>
                  <table>
                      <?
              $secondSQL = "SELECT question, question_ID, ID, form_ID, form_name FROM bathroom_price GROUP BY question_ID, form_name ORDER BY ID "; 
              $result1 = mysqli_query($conn, $secondSQL);
              while ($row = mysqli_fetch_assoc($result1))        
               { 
                  $question_ID = $row['question_ID'];
                  $question = $row['question'];
                  $formID = $row['form_ID'];
                  $form_name = $row['form_name'];
            ?>
                          <input type=hidden value="<? echo $question_ID ?>">
                          <tr class='questionHeading'>
                              <td colspan='3'>
                                  <h2><? echo $question ?></h2>
                                  <h3>Questions for the <? echo $form_name ?> Form</h3></td>
                          </tr>
                          <tr>
                              <th>Options:</th>
                              <th>Price:</th>
                              <th>Update:</th>
                          </tr>
                          <?  
                            $thirdSQL = "SELECT question_ID, options, price, ID FROM bathroom_price WHERE question_ID = $question_ID";
                            $replies = mysqli_query($conn, $thirdSQL);
                            while ($rows = mysqli_fetch_assoc($replies))
                             {

                                  $price = $rows['price'];
                                  $options = $rows['options'];
                                  $ID = $rows['ID'];

                              ?>
                              <input type=hidden name="hidden[]<?echo $ID ?>" value="<?echo $ID ?>" />
                              <input type=hidden value="<? echo $question_ID ?> " />
                              <tr>
                                  <td style='width:60%;'>
                                      <input readonly type=text value="<? echo $options ?>">
                                  </td>
                                  <td style='width:10%;'>
                                      <input type=text name="price[]<?echo $ID ?>" value="<?echo $price ?>">
                                  </td>
                              </tr>
                              <?}?>
                                  <?}?>
                  </table>
                  <div class="start-xs" style="margin: 0 0 50px 0;">
                      <button type=submit name="update" class="admin-style" value="Update Price Form">
                          <i class="fa fa-save"></i> Update Price Form
                      </button>
                  </div>
              </form>
          </div>
      </div>
  </div>
  </body>

  </html>
  <?php mysqli_close($conn);?>

Maybe this is a problem:

<input type=hidden name="hidden[]<?echo $ID ?>" value="<?echo $ID ?>" />

Your input name is "hidden[]1" etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM