PHP MYSQL update with hash variables
I need some help understanding the correct way to m开发者_运维问答ix 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
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 . ";"
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 error has nothing to do with the SQL code.
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.
精彩评论