Question about One to Many Form Data


#1

I have a MySQL table with the following format:
Task_ID
Op_ID
Op_Desc
Each task can have up to 6 Operations,

I have used the following code to select the Op_Descs (ordered by Op_ID), and can get the form to display the Operations.

   <!DOCTYPE html>
  <?php
    require 'conn.php';
  $id = 1; /* In live situation this would be picked up from $_SESSION */
  $used = 0;
  $desc = array();
  $original = array();

  if ($used == 0) {
      $query = $conn->query("SELECT `desc` FROM `test` WHERE id = 1 ORDER BY `subid`") or die(mysqli_error($conn));
    while ($row = mysqli_fetch_array($query)) {
        echo $row['desc'];
        echo "<br />";
        $desc[] = $row['desc'];
        /* $original will be used to see if data has changed for saving back to MySQL table */
        $original[] = $row['desc'];
    }
    $used = 1;
}
?>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
  </head>
  <body>
    <form class="form-horizontal" value = "POST" action="#">
        <input id="one" name="one" value="<?php echo $desc[0] ?>" type="text">
        <input id="two" name="two" value="<?php echo $desc[1] ?>" type="text">
        <input id="three" name="three" value="<?php echo $desc[2] ?>" type="text">
        <input id="four" name="four" value="<?php echo $desc[3] ?>" type="text">
        <input id="five" name="five" value="<?php echo $desc[4] ?>" type="text">
        <input id="six" name="six" value="<?php echo $desc[5] ?>" type="text">
        <input type="submit" value="submit">
      </form>
  </body>
.</html>

I have tried to use an if (isset($_POST…) construct to check the form output against the $original array elements, so only need to update those that have changed.

However I haven’t been able to get this to work, and am wondering if anyone can point me in the correct direction?


#2

No you didnt. The code is ordered by subid which is not a field you listed. How about telling us what the real problem is you are trying to solve instead of asking about your attempted solution to the real problem.


#3

Sorry me bad -

Typed in the code rather than copying and pasting.

Should have read:

"SELECT `desc` FROM `test` WHERE task_id =  '$id' ORDER BY `sub_id`"

Basically, as said above (sorry if it isn’t clear), the form displays the data taken from the table ok, but I cannot get to save it back to the table after clicking the submit button.

What I want to do is only update fields where the data has changed, which is why I want to compare the submitted form items with the original table data in the $original array.

This is my first foray into PHP and this is converting an old DOS (Clipper) program


#4

Okay, I think I understand now. You don’t need to do a comparison. Just do the update. The database will only update the changes


#5

Thanks - didn’t realise it would only update when needed (difference from thge old desktop database the Clipper program was using).

However am I doing the form correctly (using the $dsec array)? and I can’t seem to get the values back to PHP when doing an if (isset($_POST…) construct.

Thanks


#6

What do you mean “back to Php”? You need to post your code. We cant see your screen.