Pari. Pari. - 1 month ago 6
PHP Question

How to insert array values into multiple rows of a column in a database table?

i have been trying to write a program to insert the array values in multiple rows of a column in a database table. After lot of trial I couldn't get the proper output I wanted.

My database table looks like this

id | values |
1 | |
2 | |
4 | |
5 | |
6 | |


and I have 2nd column values as an array and I want to enter those values in the second column.

The output should look like this:-

id | values |
1 | 2 |
2 | 3 |
4 | 7 |
5 | 9 |
6 | 10 |


So, I have two array values
$id
and
$values
...So, I want to fill
$values
in each row of the column for each respective
$id
values

NOTE :
$id
is already filled in the database

Answer

I am assuming that you have an array with the id values in it.

You may try something like this:

foreach($XArray as $key => $value){

$sql = "UPDATE TABLENEAME ".
       "SET value = $value ".
       "WHERE id = $key" ;

$retval = mysql_query( $sql, $conn );
}

I haven't checked the code, its just a sample.

With respect to the updated question:

$count=sizeof($value);
for($counter=0;$counter<$count;$counter++){

    $sql = "UPDATE TABLENEAME ".
           "SET value = $value[$counter]".
           "WHERE id = $id[$counter]" ;

    $retval = mysql_query( $sql, $conn ) or die ("Error in query: $sql");
    }