0

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

1
  • How do you have the array? Put that array here too. Commented Aug 28, 2012 at 7:29

4 Answers 4

1

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");
    }
Sign up to request clarification or add additional context in comments.

4 Comments

because i want use $value and insert it into each row of the column name (value). As written above... could you please modify your code accordingly...
Ya thats the thing confused me little. clause should be given like this?? $sql= "UPDATE TABLENEAME "."SET value = $value "."WHERE id = $id" ;
first could you add echo $sql; and check that all the queries are being formed..?
if the queries are forming correctly try the edited code. Hope it should work
1

Try this :

foreach ( $array as $key => $value )
{
     $query = "INSERT INTO table
                  SET values = $value,
                  id = $key
                  ON DUPLICATE KEY UPDATE values = $value";
}

I do not know what sort of database you are using, this example is for MySQL.

And I also don't know how you insert information in the DB ( simple mysql_query, pdo, frameworks, ... ), so you should use the function you need and pass that query string and, if needed, don't forget to properly sanitize the array.

EDIT : I also assume that id is a primary key or unique index

2 Comments

Insert query does not have a where clause.
God, thank you Karan Punamiya, I was not paying attention. I updated the answer.
0
foreach($yourArray as $key => $value){
   // <INSERT QUERY LINE HERE > ( you have to insert $value insite 'values' )
}

5 Comments

@Peteris ya i did the same... but i think the problem is my database query
it should be smth like mysql_query("INSERT INTO table_name (value) VALUES ('$value')");
its printing the first value of the array $value
has your id (in mysql) have AUTO INCREMENT? AAND try to echo $value; if it prins out only first value - problem is in your array,btw, you really could show us your array!
the array is fine.. when i echo the value, it prints the proper array values as i wanted
0

Traverse the array values in a loop and on each iteration of the loop make and execute a query. The sample code will look something like this, change it as according to your variables and need.

foreach ($array as $id => $values) {
    $query = "INSERT INTO table (id ,values) VALUES ($id , $values)";
    $result = mysql_query($query);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.