0

I am trying to write code that can update record in a table. I have a FORM with two checkbox,s named "repaired" using the format of:

<input name="repaired[]" type="checkbox" id="repaired" value="1" />

When the FORM loads it can contain any number of records row by row and each row has a "HIDDEN" FORM field containing a unquieid for that record.

If the user selects any of the records by selecting it's corrosponding "CHECKBOX", and then submits the form I need to LOOP through each selected record and update a table.

My FORM code:

<form id="form1" name="form1" method="post" action="<?php echo $editFormAction;?>">
<?php 
do { 
    echo $row_Faults['SeqID'];
    echo $row_Faults['SeqHeader'];
    echo $row_Faults['Room'];
    <input name="repaired[]" type="checkbox" id="repaired" value="1" />
    <input name="UniqueID" type="hidden" value="<?php echo $row_Faults['UniqueID'];?>" />
    } while ($row_Faults = mysql_fetch_assoc($Faults));
?>

<input type="image" src="../images/actioned_button.png" name="button" id="button" value="1" />
<input name="MM_update" type="hidden" value="form1" />
<input name="submit" type="hidden" value="submit" />
</form>

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

    if(!empty($_POST['UniqueID']) && $_POST['repaired'] == "1"){

        foreach($_POST['repaired'] as $Selected){

//THIS IS WHERE I AM STUCK

        UPDATE QUERY.......

        }
    }
}

This is where I am stuck, how can I loop through the $_POST data and update the records where the "UniqueID" matches the $_POST['UniqueID'] and if $_POST['repaired'] == 1.

Any help or a pointer to what I need to do would be great,

Many thanks in advance for your time.

DCJones
  • 3,121
  • 5
  • 31
  • 53

1 Answers1

1

There are few issues with your code, such as:

  • Your do-while loop with throw Undefined variable $row_Faults error once you run the code, and that's because it's defined later in while(...); statement. You need to use a simple while() loop in this case, like this:

    while ($row_Faults = mysql_fetch_assoc($Faults)){
        ...
    }
    

    Also, you can't use repaired and UniqueID <input> HTML elements inside PHP like that, it'll throw other errors.

  • You don't need a separate <input> element for each each row for unique ID, you can incorporate that in the checkbox input element as well, like this:

    <input name="repaired[<?php echo $row_Faults['UniqueID']; ?>]" type="checkbox" id="repaired" value="1" />
    

    So based on the above two points, replace your do-while loop with the following while loop.

    while ($row_Faults = mysql_fetch_assoc($Faults)){
        echo $row_Faults['SeqID'];
        echo $row_Faults['SeqHeader'];
        echo $row_Faults['Room'];
        ?>
        <input name="repaired[<?php echo $row_Faults['UniqueID']; ?>]" type="checkbox" id="repaired" value="1" />
        <?php
    }
    
  • Since you're using image input element for form submission, there's no point using a separate hidden submit element. So you can delete the following line altogether,

    <input name="submit" type="hidden" value="submit" />
    

    So once you submit the form, this is how you can process the form and update the corresponding checked rows,

    if (isset($_POST["MM_update"]) && $_POST["MM_update"] == "form1"){
        foreach($_POST['repaired'] as $uniqueID => $repairedValue){
            // your UPDATE query
        }
    }
    

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Hi and many, many thanks for your reply. I have intergrated the code you gave me and it works up to a point. when I submit the FORM and echo out the query statements this is what I see in the WHERE clause: WHERE UniqueID= 'NXLHR01011483974920' WHERE UniqueID= 'NXLHR01011483974920'. As you can see, both the UniqueID;s are the same. One of them should be "NXLHR01011483916595" for testing. Any ideas why? – DCJones Jan 14 '17 at 21:13
  • @DCJones Please post your form submission code on [pastebin.com](http://pastebin.com/index.php) and give me it's link here. – Rajdeep Paul Jan 14 '17 at 21:16
  • @DCJones That's the HTML code, I was asking about the PHP code. Also, do `var_dump($_POST);` and give me this data as well. – Rajdeep Paul Jan 14 '17 at 21:29
  • Hi, I have placed some code with comments and the VAR DUMP in the same pastebin. The submit, submits to the same page. http://pastebin.com/6j1dNxhh – DCJones Jan 14 '17 at 21:37
  • @DCJones Where's your `UPDATE` query? It's not there in the pastebin doc. I'm suspecting there is something wrong with your `UPDATE` query. – Rajdeep Paul Jan 14 '17 at 21:41
  • Sorry, I have inserted the query. – DCJones Jan 14 '17 at 21:44
  • @DCJones See the `UPDATE` query. You're using `... WHERE UniqueID= '".$_POST['UniqueID']."'" ...` where you should be using `... WHERE UniqueID= '". $uniqueID ."'" ...` – Rajdeep Paul Jan 14 '17 at 21:51
  • Also copied the echo query – DCJones Jan 14 '17 at 21:51
  • Perfect. Thank you so must for all your time. Where do I send the beer. Many, many thanks. – DCJones Jan 14 '17 at 21:54
  • @DCJones You're very welcome! Glad I could help. Cheers! ;-) – Rajdeep Paul Jan 14 '17 at 21:57