简体   繁体   中英

Update mysql database fields with array php

I'm trying to achieve a multiple update in one submit. I have a table with a number of rows and want to be able to update one field in each row just by tabbing to the next insert box.

My code is thus:- //start a table echo ' ';

//start header of table
echo '<tr>

<td width="60" align="center"><strong>Lab Item ID</strong></td>
<td width="60" align="center"><strong>Test Suite Name</strong></td>
<td width="60" align="center"><strong>Test Name</strong></td>
<td width="50" align="center"><strong>Result</strong></td>
</tr>';

//loop through all results
while ($row=mysql_fetch_object($sql)) {
    //print out table contents and add id into an array and email into an array
    echo '<tr>
    <td align="center"><input name="id[]" value='.$row->lab_item_id.' readonly> </td>
    <td align="center">'.$row->test_suite_name.'</td>
    <td align="center">'.$row->test_name.'</td>
    <td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
    </tr>';
}

//submit the form

echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
//if form has been pressed proccess it
if($_POST["Submit"])
{

//get data from form
//$name = $_POST['name'];

//$_POST['check_number'] and $_POST['check_date'] are parallel arrays

foreach( $_POST['id'] as $id ) {
     $tresult = trim($_POST['test_result']);
      $query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
      //execute query

}  
 print_r($_POST);
 var_dump($tresult);
//redirect user
$_SESSION['success'] = 'Updated';
//header("location:index.php");
}
?>

When I print the $_POST arrays, everything is populating fine, however the variable is Null. I know I can't do a foreach on multiple arrays (at least I don't think I can) so is there some other trick I'm missing please? I can't be far away as the $_Post print has the right data in it.

Incidentally, the whole thing is generated by a query, so I never know how many records I'll have to update.

I've been looking at this (and other) forums, but can't seem to get a solution. I thought I understood arrays, but now I'm beginning to wonder!

edit - it's the $tresult variable that isn't working.

Many thanks, Jason

Edit Thursday 21st Feb (05:41 UK time) Thanks for your input everybody. I've solved this one now, and your collective advice helped. The code that finally cracked it is:-

//get data from form
$id1 = $_POST['id'];
$test_result1 = $_POST['test_result'];
foreach ($id1 as $key => $value){
$query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id=$value ";
//execute query

Working through which variables etc were populated and what they were populated with was the key. Back to first principles, isn't it?

Cheers all. J

change the query like this :

  $query = "UPDATE tbl_lab_item SET test_result=$tresult WHERE lab_item_id = $id";

By adding single quotes ' ' you tell it to read is as a String and not to take the value of the var.

Edit

Replace your foreach loop with the following and let me know :

$id1 = $_POST['id'];
$test_result1 = $_POST['test_result'];

foreach( $id1 as $key => $value ) {
  $query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id = '$key' ";

}   

Actually, you might get it done by doing a simpler (basic) form of for loop:

//get data from form
//$name = $_POST['name'];

//$_POST['check_number'] and $_POST['check_date'] are parallel arrays

$numberOfData = count($_POST['id']);

for($index = 0; $index < $numberOfData; $index++)
{
     $id = $_POST['id'][$index];
     $tresult = trim($_POST['test_result'][$index]);
      $query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
       //execute query

}
 print_r($_POST);

I hope this helps.

Cheers

Problem is that you're telling PHP to build your input fields as arrays, but then treat it as a string later:

<td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
                            ^^--- array

 $tresult = trim($_POST['test_result']);
                 ^^^^^^^^^^^^^^^^^^^^^--- no array key, so you're assigning the entire array

  $query = "UPDATE tbl_lab_item SET test_result='$tresult'
                                                ^^^^^^^^^^--- array in string context

trim() expects a string, but you pass in an array, so you get back a PHP NULL and a warning. That null then gets stuffed into your SQL statement, and there's your problem.

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