SQL Insert Problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jth133
Forum Newbie
Posts: 2
Joined: Tue Nov 08, 2011 6:37 pm

SQL Insert Problem

Post 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);
Last edited by Benjamin on Tue Nov 08, 2011 10:10 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: SQL Insert Problem

Post by manohoo »

Code: Select all

$query = "INSERT INTO $table ({$columns[$i]}) VALUES ($value)";
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: SQL Insert Problem

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: SQL Insert Problem

Post 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.
Post Reply