Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
which the variable? there's many variables in your code. –  Marc B Feb 19 '13 at 1:53
    
sorry, good point. It's the test_result I'm trying to update in the update query. –  Jason Feb 19 '13 at 2:33

3 Answers 3

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' ";

}   
share|improve this answer
    
Hi, thanks for this. I have tried that, but it doesn't work either. When I dump the variable $tresult, it's NULL, so I'm not collecting the contents for some reason. When I look at the $_POST array, everything I need is there, it just won't go into the $tresult variable. –  Jason Feb 19 '13 at 2:36
    
Hi. Can you try echo $_POST['test_result']; first line inside the foreach loop and see if it displays results correct? –  t0s Feb 19 '13 at 13:56
    
Look at the Edit further above in the answer. I added new code to be tested –  t0s Feb 19 '13 at 14:22
    
Hi t0s, thank you, I tried but still no joy writing to the database. I changed the vardump to the updated variables, and it displays the correct data for the last loop though. I'll try echoing the $_POST['test_result'] now –  Jason Feb 20 '13 at 10:11
    
Ok, I tried two things. Firstly the $_POST['test_result'] doesn't seem to output anything, however a var_dump of $testresult1 gives the last value entered. A var_dump of $key shows it is cycling through as expected. –  Jason Feb 20 '13 at 10:20

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.

share|improve this answer
    
Excellent, thank you. I knew I must have been doing something like that but couldn't see what. Is there a way to solve it? I've tries playing with the array within the foreach loop, but don't know the right syntax, should I be doing it outside the loop somehow? –  Jason Feb 19 '13 at 8:22

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

share|improve this answer
    
Thank you for this. I can see the logic in this, but it still doesn't insert anything into the database. The vardump for the $tresult is giving the last value, so looks like it's working through. –  Jason Feb 19 '13 at 9:47
    
Hmmm... chances are that it may be related to the query itself. Have you tried it on an SQL editor/executor? The thing to consider is, whether lab_item_id is a string or a number. In case it is a number, then there shouldn't be a pair of single quotes enclosing it. On the other hand, if it is a string, you may also want to use trim just as how you treat the test_result. –  peter.aryanto Feb 19 '13 at 10:12
    
the result entered would only ever be a number, I've tried with and without quotes though in both scenarios to test. I'll check the SQL but if memory serves I did try this yesterday in phpmyadmin and it worked. To be honest, I've tried so many things they are all beginning to blur :) –  Jason Feb 20 '13 at 10:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.