SQL Insert Problem
Posted: Tue Nov 08, 2011 6:40 pm
Hi,
I have a small amount of experience writing php and am currently running into an issue inserting array values into a sql table. A snippet of the code I have is below.
The array "$flashVars" holds several variables which are supposed to be looped through and inserted into the table into their respective columns, identified by the "$columns" array. I've echoed the result of $query and everything looks as it should, with the values matching up to their columns, but none of it gets written to the database.
I've also tried it without the "columns" part without any luck. Any help would be greatly appreciated as I've been trying to solve this issue for a while. Thanks.
I have a small amount of experience writing php and am currently running into an issue inserting array values into a sql table. A snippet of the code I have is below.
The array "$flashVars" holds several variables which are supposed to be looped through and inserted into the table into their respective columns, identified by the "$columns" array. I've echoed the result of $query and everything looks as it should, with the values matching up to their columns, but none of it gets written to the database.
I've also tried it without the "columns" part without any luck. Any help would be greatly appreciated as I've been trying to solve this issue for a while. Thanks.
Code: Select all
//CODE
$db = mysql_connect("database", "username", "password");
if (!$db){
die('Could not connect: ' . mysql_error());
}
$table = mysql_select_db("table", $db) or die ("Unable to select database.");
$result = mysql_query("SELECT * FROM table WHERE $uID = uID");
while($row = mysql_fetch_assoc($result)){
$columns = array_keys($row);
}
foreach($flashVars as $value){
$query = 'INSERT INTO table "'.$columns[$i].'" VALUES "'.$value.'"';
$result = mysql_query($query);
}
mysql_close($db);