Forum Moderators: coopster

Message Too Old, No Replies

Help with UPDATE query

         

ro1960

8:31 am on May 26, 2007 (gmt 0)

10+ Year Member



The following UPDATE query works fine if run directly into phpMyAdmin, but it doesn't update my records if run from the PHP script. I can't seem to figure out where the bug is:


if ($status == "update")
{

$sql="
UPDATE top10 SET
djid = '$djid', rank = '$rank1', artist = '$h_artist1', song = '$h_song1', mix = '$h_mix1', label = '$h_label1', comment = '$h_comment1'
WHERE id = '$item_id1';

UPDATE top10 SET
djid = '$djid', rank = '$rank2', artist = '$h_artist2', song = '$h_song2', mix = '$h_mix2', label = '$h_label2', comment = '$h_comment2'
WHERE id = '$item_id2';

UPDATE top10 SET
djid = '$djid', rank = '$rank3', artist = '$h_artist3', song = '$h_song3', mix = '$h_mix3', label = '$h_label3', comment = '$h_comment3'
WHERE id = '$item_id3';

UPDATE top10 SET
djid = '$djid', rank = '$rank4', artist = '$h_artist4', song = '$h_song4', mix = '$h_mix4', label = '$h_label4', comment = '$h_comment4'
WHERE id = '$item_id4';

UPDATE top10 SET
djid = '$djid', rank = '$rank5', artist = '$h_artist5', song = '$h_song5', mix = '$h_mix5', label = '$h_label5', comment = '$h_comment5'
WHERE id = '$item_id5';

UPDATE top10 SET
djid = '$djid', rank = '$rank6', artist = '$h_artist6', song = '$h_song6', mix = '$h_mix6', label = '$h_label6', comment = '$h_comment6'
WHERE id = '$item_id6';

UPDATE top10 SET
djid = '$djid', rank = '$rank7', artist = '$h_artist7', song = '$h_song7', mix = '$h_mix7', label = '$h_label7', comment = '$h_comment7'
WHERE id = '$item_id7';

UPDATE top10 SET
djid = '$djid', rank = '$rank8', artist = '$h_artist8', song = '$h_song8', mix = '$h_mix8', label = '$h_label8', comment = '$h_comment8'
WHERE id = '$item_id8';

UPDATE top10 SET
djid = '$djid', rank = '$rank9', artist = '$h_artist9', song = '$h_song9', mix = '$h_mix9', label = '$h_label9', comment = '$h_comment9'
WHERE id = '$item_id9';

UPDATE top10 SET
djid = '$djid', rank = '$rank10', artist = '$h_artist10', song = '$h_song10', mix = '$h_mix10', label = '$h_label10', comment = '$h_comment10'
WHERE id = '$item_id10'
";
}
else
{
$sql=" INSERT INTO top10
(djid, rank, artist, song, mix, label, comment)
VALUES
('$djid', '$rank1', '$h_artist1', '$h_song1', '$h_mix1', '$h_label1', '$h_comment1'),
('$djid', '$rank2', '$h_artist2', '$h_song2', '$h_mix2', '$h_label2', '$h_comment2'),
('$djid', '$rank3', '$h_artist3', '$h_song3', '$h_mix3', '$h_label3', '$h_comment3'),
('$djid', '$rank4', '$h_artist4', '$h_song4', '$h_mix4', '$h_label4', '$h_comment4'),
('$djid', '$rank5', '$h_artist5', '$h_song5', '$h_mix5', '$h_label5', '$h_comment5'),
('$djid', '$rank6', '$h_artist6', '$h_song6', '$h_mix6', '$h_label6', '$h_comment6'),
('$djid', '$rank7', '$h_artist7', '$h_song7', '$h_mix7', '$h_label7', '$h_comment7'),
('$djid', '$rank8', '$h_artist8', '$h_song8', '$h_mix8', '$h_label8', '$h_comment8'),
('$djid', '$rank9', '$h_artist9', '$h_song9', '$h_mix9', '$h_label9', '$h_comment9'),
('$djid', '$rank10', '$h_artist10', '$h_song10', '$h_mix10', '$h_label10', '$h_comment10')
";
}
$result = mysql_query($sql);

Can someone see the obvious?

IndiaMaster

11:33 am on May 26, 2007 (gmt 0)

10+ Year Member



Why dont you made the update query from phpmyadmin itself? I think it will work.

phranque

12:14 pm on May 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



$result = mysql_query($sql);

Can someone see the obvious?

i think it may become obvious with something like:

$result = mysql_query($sql);
echo mysql_errno() . ": " . mysql_error() . "\n";

barns101

2:28 pm on May 26, 2007 (gmt 0)

10+ Year Member



From past experience I believe that you cannot execute multiple queries inside one SQL statement by separating them with semi-colons. I think phpMyAdmin gets around the problem by building an array of the queries and executing them one after another.

henry0

2:36 pm on May 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



barns seems to be correct
However the neat thing to perform is:
at script level echo $result
then whatever is the result paste it in PHPmyAdmin
and read the errors.
VS pasting a query as is (as a hard coded script) and not the result of that query

Habtom

11:55 am on May 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn't it be something like:

$sql = "UPDATE top10 SET djid = '". $djid ."', rank = '". $rank1."',
artist = '". $h_artist1."', song = '". $h_song1."', mix = '". $h_mix1."',
label = '". $h_label1."', comment = '". $h_comment1."'
WHERE id = '". $item_id1 ."'";

Let me know if this works.

Habtom