简体   繁体   中英

How to get a unique value from the html form in a auto generating for loop

I have a Html form in a loop. So when my PHP script fetch a row from my DB each row has a same html form form attached to it. Then I take those form values and pass it to a ajax script which then call a php file which then update those values into DB . The problem is when I Update those value the form the updates every column in all the rows.(The sample image attached below. My Criticality and Priority column has the same values after storing the update values in the Db) 在此处输入图片说明

**And yes I have a Auto-generated primary key in both the tables **

Thats my loop

          <?php
      foreach($results as $data){

          echo '<tbody>
              <tr class="dropDown">
              <td>1</td>
              <td>'.$data['Title'].'</td>
              <td>'.$data['criticality'].'</td>
              <td>'.$data['Priority'].'</td>
              <td>'.$data['Description'].'</td>
              <td>'.$data['Date_Submitted'].'</td>
              <td></td>
          </tr>
          </tbody>';

      }

    ?>

That's my web form:

        <form action="/action_page.php">
      <fieldset>
        <label>XYZ Questions </label><br>
          <label class="radio-inline">
            <input type="radio" name="optradio">
            <label>YES</label>
        </label>
          <label class="radio-inline left">
            <input type="radio" name="optradio">
            <label>NO</label>
        </label>
      </fieldset>
      <fieldset>
        <label>XYZ Questions </label><br>
          <label class="radio-inline">
            <input type="radio" name="optradio1">
            <label>YES</label>
        </label>
          <label class="radio-inline left">
            <input type="radio" name="optradio1">
            <label>NO</label>
        </label>
      </fieldset>
      <div class="checkbox">
        <label><input type="checkbox"> Remember me</label>
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
    </form>

That's my ajax script to get the values

      var launchAjax = function () { // event handler for button click
      $.get(
     "php/inbetween.php/",
     {
         question: $("[name=optradio]:checked").val(),
         question1: $("[name=optradio1]:checked").val(),

     }
   );
  }

  $("#no").click(launchAjax);

and that's how convert those values and save a number in to a bd

    function getMark($answer, $mark = 1){ 
    $result = 0;
    if($answer == 'YES'){
        $result = $mark;
    }
    return $result;
}
    $p = 0;
    $p += getMark($question, 1); // provide the answer and the mark
    $p += getMark($question1, .5);

    $c = 0;
    $c += getMark($question, 0.5); // provide the answer and the mark
    $c += getMark($question1, 1);

That's my Php script to update the form values in a table

 $command1 = "UPDATE rating SET criticality = '$c' , Priority = '$p'";

   // prepare and executing
$stmt1 = $dbh->prepare($command1);
$result1 = $stmt1->execute();

The problem lies in your SQL code:

UPDATE rating SET criticality = '$c' , Priority = '$p';

This is asking your database to update the table 'rating', and set 'criticality' and 'priority' to the values stored in $c and $p . Notice how it's not telling the database which row to update, so it's running the update on ALL rows.

Each of your database rows should have a primary key, which is usually an incrementing integer. This is usually the way you'd carry out this kind of update:

UPDATE rating SET criticality = '$c' , Priority = '$p' WHERE id = '$id';

You'll need to find a way to include the ID of the row in your Ajax call, which your PHP script can then pass to MySQL to update the correct row.

You need to set a WHERE clause un your UPDATE statement.

$command1 = "UPDATE rating SET criticality = '$c' , Priority = '$p' WHERE id = $id ";

That will only update the row that matches the id you pass.

@tombeynon got there first haha

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