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