Forum Moderators: coopster

Message Too Old, No Replies

Update mysql column

         

mrnoisy

11:17 pm on Mar 19, 2006 (gmt 0)

10+ Year Member



I have a mysql table:
id ¦ seq
---------
1 ¦ 5
2 ¦ 1
3 ¦ 3
4 ¦ 6
3 ¦ 4
6 ¦ 2

I need to update the 'seq' column with new values submitted from a form like this:

3,5,1,6,2,4

So I explode it:

$seq = explode(",", $seq);

Then I can print it:

foreach ($seq as $num)
{
echo "$num\n";
}

How do I update the 'seq' column of the table with the values?

I tried this:

foreach ($seq as $num)
{
$insertentry="update table set seq='$num'";
mysql_query($insertentry) or die(mysql_error());
}

But each row in the 'seq' column gets the last value, 4 for example.

henry0

11:27 pm on Mar 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this
$array;
// what will go in your DB

$str = implode("`/`", $array);
// saves the array as a string ; separe pieces with:/

//use that $str to load input in DB

//get result:

// using $str;

$array = explode("`/`", $str);
// splits $str into array elements and del `/`.

mrnoisy

11:59 pm on Mar 19, 2006 (gmt 0)

10+ Year Member



Thanks henry0

But I don't want to input a string into the database, I want one value in each row of the 'seq' column.

So if I have this string:

3,5,1,6,2,4

I want to enter it into the database as:

id ¦ seq
---------
1 ¦ 3
2 ¦ 5
3 ¦ 1
4 ¦ 6
3 ¦ 2
6 ¦ 4

omoutop

10:54 am on Mar 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When u create your string, create an array instead with ids and values u wanna insert into ech id.

When u explode the array u will get pairs of id-values

example: foreach ($my_array as $id=>$value)
{
// code here
}

Now u can update your data : UPDATE mytable SET field='$value' WHERE id='$id'

mrnoisy

5:04 am on Mar 22, 2006 (gmt 0)

10+ Year Member



Thanks for your help, it almost works.

But what if the 'id' field is not sequential? In that case id='$id' will not line up.