I'm trying to update a database using arrays, but I can't seem to do that. I can only update it using strings, and numbers, but not arrays. Then I tried to echo out the string, number AND the array, just to see if I made an mistake in making the array. Please take a look:
$string = "string";
$num = 10;
$array[0][0] = "array";
echo $string."</br>";
echo $num."</br>";
echo $array[0][0]."</br>";
output:
string
10
array
But inserting data into a table using mysql:
$table = "userLogin";
$column = "username";
Using string:
$query= "INSERT INTO $table($column)VALUES($string)";
$result = mysql_query($query);
Output in table:
string
Using number:
$query= "INSERT INTO $table($column)VALUES($number)";
$result = mysql_query($query);
Output in table:
10
But when you're using an array:
$query= "INSERT INTO $table($column)VALUES($array[0][0])";
$result = mysql_query($query);
Output in table:
There is no change. Why is that? I figure that since all strings, numbers, and arrays can be echo, using the echo command, they can be use likewise when updating a database. But it seems like you can only update them using strings and numbers. Unless, I'm doing something wrong. If I'm doing something wrong, please let me know. Thank you.
$query= "INSERT INTO $table($column)VALUES({$array[0][0]})";By the way, if those variables contain user input it's better to use prepared statements to prevent SQL injection.mysql_queryis an obsolete interface and should not be used in new applications as it's being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices.echo $array."</br>";does it not showing any php notice like thisNotice: Array to string conversionbecause you can't echo array