Tutorial :PHP MYSQL update with hash variables



Question:

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  


Solution:1

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 . ";"  


Solution:2

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.


Solution:3

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »