0

I am trying to figure out how to update MySQL table with array.

The Tables has 4 fields. REGNO, BATCHNO, NAMES and ATTEN_SUM. The REGNO has the unique value.

$i = 0;

while($row_recordset = mysql_fetch_array($query_run)) {

echo "<tr>";

echo "  <td>{$row_recordset['REGNO']}</td>
        <td>{$row_recordset['NAME']}</td>

        <td><input type='text' name='atten_ave".$i."'></td>

     ";

echo "</tr>";

$i++;

Here's my html code for the previous page after the update page.

foreach($_POST as $textbox => $values) {

$query_update = "UPDATE `grades` SET `ATTEN_SUM` = '$values' WHERE `BATCHNO` = '$sessionbatch'";
if(mysql_query($query_update)) {
     echo 'SUCCESS';
       } else{
       die(mysql_error());
              }
}

$_POST is a array from dynamic inputs from the previous page. Here's the example of my output in the table.

REGNO  |  BATCHNO       |   NAME       |    ATTEN_SUM
====================================================
  1    |  ARPA 00-055    |   Jason      |      99 
  2    |  ARPA 00-055    |   Mark       |      99 
  3    |  ARPA 00-055    |   Edgar      |      99

It updates all the rows with the last value that I input.

halfer
  • 19,824
  • 17
  • 99
  • 186
zeus2026
  • 131
  • 13

4 Answers4

1

html

 //<input type='text' name='atten_ave".$i."' 
 <input type='text' name='atten_ave[]'...

php

//foreach($_POST as $textbox => $values) {
foreach($_POST['atten_ave'] as $textbox => $values) {

BUT this update is useless. it just update all record use last textbox.

i think you need to pass name or id to php, then sql query add something like 'where id="$_POST['ID']"...

*CHECK mysql injections.
JOE LEE
  • 1,058
  • 1
  • 6
  • 6
0

Well as you said, REGNO is the unique key, but you are updating on BATCHNO, which is not unique, according to your output.

keaton_fu
  • 444
  • 2
  • 9
  • thanks for the reply but sir, Because I have many different BATCHNO in my table, and all I just want is to update all of the ATEN_SUM of same BATCHNO. – zeus2026 Apr 29 '13 at 02:48
0

If you look at the WHERE clause carefully, (WHERE BATCHNO = '$sessionbatch') you'll understand that it DOES update the SAME row each time and thus you see the last update.

Print the query statement, It'll be much clear to you.

if(mysql_query($query_update)) {
  echo $query_update.'</br>';
J A
  • 1,776
  • 1
  • 12
  • 13
0

1)

You should not use mysql_* functions anymore. They're deprecated and may potentially leave holes for mysql injections. You should learn to use either Mysqli or PDO.

2)

Your code is totally open to any possible mysql injection. You should use mysql_real_escape_string or see 1) above.

3)

With foreach($_POST) you're iterating through every $_POST item. I'm assuming (maybe I'm wrong) that you're submitting data from an HTML form. Try var_dump($_POST) and you'll see there are many values that are not related to what you want to do. For example:

<input type=”submit” value=”Some value” name="update">

will result in PHP

echo $_POST["update"];
// result: Some value

if you could post the output of var_dump($_POST) we could all see what you're passing inside of the foreach loop and perhaps give more detailed solution.

Community
  • 1
  • 1
Saturnix
  • 10,130
  • 17
  • 64
  • 120