简体   繁体   中英

SQLSTATE[HY000] [2002] error

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!

Not necessarily part of your issue but you are using prepared statmemnts incorrectly. PArt of the point of using prepared statments is so that the DB takes care of the db specific escaping. In order to do this you write the query with placeholders that then get replaced with the the values passed to the statement. So in this case:

$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES (?,?,?,?)"); 
$insertdata->execute(array($firstname,$lastname,$email,$phone));

Or with named placeholders which is a bit more robust:

$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES (:firstname, :lastname, :email, :phone)"); 
$insertdata->execute(array(
  ':firstname' => $firstname,
  ':lastname' => $lastname,
  ':email' => $email,
  ':phone' => $phone
));

Now as far as your error id be willing to bet you are using localhost as the hostname which can cause issues on windows - try using 127.0.0.1 instead. So for example:

$DBH = new PDO("mysql:host=127.0.0.1;dbname=the_db_name", $username, $password); 

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!

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

    $csvusername = $firstname . $lastname;

    $csvpasswordname = $lastname . $firstname;

    $status = "Open";




    $sth = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone, username, password, status, memberview) VALUES (?,?,?,?,?,?,?,?)"); 
    $sth->execute(array($firstname, $lastname, $email, $phone, $csvusername, $csvpasswordname, $status, $memberview));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM