tags:

views:

26

answers:

3

I need some help understanding the correct way to mix variables with strings like this. I have tried every configuration I can think of and I keep getting an error.

<?php
include('connect.php');
foreach($_GET['item'] as $key=>$value) {
    mysql_query("UPDATE userprojectlist SET category_display_order = '$key' WHERE category_id = '$value' ");
}
?> 


Notice: Undefined index: item in updatedb.php on line 3

Warning: Invalid argument supplied for foreach() in pdatedb.php on line 3
+1  A: 
mysql_query('UPDATE userprojectlist SET category_display_order = ' . $key . ' WHERE category_id = ' . $value );

or

mysql_query("UPDATE userprojectlist SET category_display_order = $key WHERE category_id = $value" );

This is assuming both variables are integers.

hookedonwinter
+3  A: 

Is item an array in the url like item[]? If it's not that's the reason you get that error, you cannot iterate through a non-iterator object!

Also get rid of the single quotes around the values if they are numeric and use string concatenation,

"UPDATE userprojectlist SET category_display_order = " . $key . " WHERE category_id = " . $value . ";"
Hamid Nazari
+3  A: 
Notice: Undefined index: item in updatedb.php on line 3

This error is saying that there's no such variable as $_GET['item'] defined. You probably did not pass $_GET['item'] to this page.

Warning: Invalid argument supplied for foreach() in pdatedb.php on line 3

Thus because of the previous error, you get this, as $_GET['item'] is not an array.

The errors has nothing to do with the SQL code.

thephpdeveloper