I am currently trying to insert data from a CSV file into my mysql data base. The form and all the functions work properly because I've tested to make sure It's reading file, etc. When I prepare the PDO to insert into mysql I get no error in Chrome. Now when I let it execute()
that's when I get server error in Chrome(any browser) and I looked in my error log file that showed up where function is saying:
SQLSTATE[HY000] [2002]
No connection could be made because the target machine actively refused it.
I'll paste my code on the function that I have made.
Code:
function returnBack(){
header("Location:csvuploader/csvuploaderform.php?seterror=1");
exit;
}
if (isset($_POST['submit'])){
/*******************************CHECK TO MAKE SURE FORM IS PROPERLY FILLED AND GET NAME OF COLUMNS IN CSV FILE *********************************************************/
if (empty($_FILES['file'] ) )
{
returnBack();
}
if (empty($_POST['firstname'])){
echo "firstname is empty";
returnBack();
}
if (empty($_POST['lastname'])){
echo "lastname is empty";
returnBack();
}if (empty($_POST['email'])){
echo "email is empty";
returnBack();
}
if (empty($_POST['phone'])){
returnBack();
}
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file , "r");
$fileop = fgetcsv($handle,1000,",");
$fileop=array_map("strtoupper",array_map("trim",$fileop));
$firstname_index = array_search(strtoupper($_POST["firstname"]),$fileop);
if ($firstname_index===false){
returnBack();
}
$lastname_index = array_search(strtoupper($_POST['lastname']),$fileop);
if ($lastname_index===false){
returnBack();
}
$email_index = array_search(strtoupper($_POST['email']),$fileop);
if ($email_index===false){
returnBack();
}
$phone_index = array_search(strtoupper($_POST['phone']),$fileop);
if ($phone_index===false){
returnBack();
}
/***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEM INTO CSV TABLE IN DB *************************************/
try {
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo "I'm sorry, I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
/*$fileop = fgetcsv($handle,1000,",")*/
while (($fileop=fgetcsv($handle)) !== false)
{
$fileop=array_map("trim",$fileop);
$firstname = $fileop[$firstname_index];
$lastname = $fileop[$lastname_index];
$email = $fileop[$email_index];
$phone = $fileop[$phone_index];
$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')");
//var_dump($insertdata);
$insertdata->execute();
}
}
$DBH = null;
UPDATED ------------------------------------- Heres some info
PHP info - PHP Version 5.3.10, System Windows NT W03 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586
UPDATED ------------------------------------
I am able to connect to MySQL server and browse it - insert data from other functions that I have made and they all work perfect. It is just this one will give me an error.
UPDATED -------------------------------------------------------------------------
Figured it out guys thanks for all your help. I actually needed to fill in all the data columns for it to work. For instance I had (firstname,lastname,email,phone) I needed to fill in the username,password,status part. Thanks for all your help guys! Hope this will help anyone who falls in the same hole!
See Question&Answers more detail:os