Using PDO and running into problems
Posted: Mon Apr 30, 2012 4:21 pm
Hello Everyone:
I am learning how to use PDO and have executed a few simple queries using the process. But... I am running into a problem with INSERT queries and arrays. In essence I am working on a project where users will be able to upload a csv file and insert the contents into a mySQL table.
I've figured out how to upload the file and extract the different columns in the csv file (using a foreach statement). Within a foreach statement I have the following:
The echos are working perflectly. Col_1 is a first name, col_2 is a last name, and col_7 is an email address. When I echo out the content, it echoes accurate data for each column variable.
Now to my problem.
I figured once I successfully extracted each column from the array, all I'd have to do for the INSERT Statement (using PDO) would be to bind PDO keys to each column variable, as follows:
Well - the insert statement - which did work as a regular mysql_query($query) or die(mysql_error()); - does NOT work with this PDO syntax.
So... to do some testing I added $test= to the $prep_insert->execut() statement as follows:
Then I inserted this var_dump:
The result of this var_dump is as follows:
Any ideas, any one has on this would be truly appreciated.
Thanks Much:
Pavilion
I am learning how to use PDO and have executed a few simple queries using the process. But... I am running into a problem with INSERT queries and arrays. In essence I am working on a project where users will be able to upload a csv file and insert the contents into a mySQL table.
I've figured out how to upload the file and extract the different columns in the csv file (using a foreach statement). Within a foreach statement I have the following:
Code: Select all
$inner_array = fgetcsv($file);
$col_1=$inner_array[0];
$col_2=$inner_array[1];
$col_3=$inner_array[2];
$col_4=$inner_array[3];
$col_5=$inner_array[4];
$col_6=$inner_array[5];
$col_7=$inner_array[6];
echo "<br />";
echo($col_1) . "<br />";
echo($col_2) . "<br />";
echo($col_7) . "<br /><br />";Now to my problem.
I figured once I successfully extracted each column from the array, all I'd have to do for the INSERT Statement (using PDO) would be to bind PDO keys to each column variable, as follows:
Code: Select all
try
{
$prep_insert = $link->prepare($upload_query); // Prepare statement
$prep_insert->execute(array(
':fname'=>$col_1,
':lname'=>$col_2,
':title'=>$col_3,
':d_phone'=>$col_4,
':ext'=>$col_5,
':c_phone'=>$col_6,
':email'=>$col_7,
':fcontact'=>$firstcontact,
':user'=>$userid
));
}So... to do some testing I added $test= to the $prep_insert->execut() statement as follows:
Code: Select all
$test=$prep_insert->execute(array(
':fname'=>$col_1,
':lname'=>$col_2,
':title'=>$col_3,
':d_phone'=>$col_4,
':ext'=>$col_5,
':c_phone'=>$col_6,
':email'=>$col_7,
':fcontact'=>$firstcontact,
':user'=>$userid
));Code: Select all
var_dump($test);What am I doing wrong with this PDO statement? I'm following every syntax example I can find for binding PDO keys to variables, but it is almost as though the keys are not picking up the value of the variable.dump insert execute bool(false)
Any ideas, any one has on this would be truly appreciated.
Thanks Much:
Pavilion