Page 1 of 1

SQL Insert Problem

Posted: Tue Nov 08, 2011 6:40 pm
by jth133
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.

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);

Re: SQL Insert Problem

Posted: Tue Nov 08, 2011 7:01 pm
by manohoo

Code: Select all

$query = "INSERT INTO $table ({$columns[$i]}) VALUES ($value)";

Re: SQL Insert Problem

Posted: Tue Nov 08, 2011 8:27 pm
by mikosiko
manohoo wrote:

Code: Select all

$query = "INSERT INTO $table ({$columns[$i]}) VALUES ($value)";
Huh?.... you should reread the OP post and try again

Re: SQL Insert Problem

Posted: Tue Nov 08, 2011 8:38 pm
by mikosiko
@jth133

The code that you posted has so many errors that is really difficult to figure out what exactly are you trying to do... could you please try to explain what do you think that your code is doing (or should do)... I see incorrect and apparently useless SELECT.. undeclared variables, incorrect use of mysql reserved words, incorrect INSERT syntax... and inside a nasty loop, etc.

To better help please explain your code and your goals... and post your complete relevant code in case what you posted is not complete code.