1

I'm building the following script and trying to insert each row using PDO.

<?php
/**
 * Created by PhpStorm.
 * User: Andy
 * Date: 13/03/2016
 * Time: 18:15
 */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//connect to the database
require_once('./members/config/database.php');

if (isset($_FILES['uploadedfile'])) {

    // get the csv file and open it up
    $file = $_FILES['uploadedfile']['tmp_name'];
    $handle = fopen($file, "r");
    try {
        // prepare for insertion
        $stmt = $pdo->prepare('
            INSERT INTO memberstest (
                user_healer_number,
                user_real_name,
                user_email,
                user_address,
                user_phone,
                user_mobile_number,
                user_paid_until,
                user_name,
                user_password_hash
            ) VALUES (
                :memberNumber,
                :memberRealName,
                :memberEmail,
                :memberAddress,
                :memberHomePhone,
                :memberMobilePhone,
                :memberPaidUntil,
                :memberUserName,
                :memberPassword
            )
        ');

        $stmt->bindParam(':memberNumber', $data[0]);
        $stmt->bindParam(':memberRealName', $data[1].' '.$data[2]);
        $stmt->bindParam(':memberEmail', $data[11]);
        $stmt->bindParam(':memberAddress', $data[3].' '.$data[4].' '.$data[5].' '.$data[6].' '.$data[7].' '.$data[8]);
        $stmt->bindParam(':memberHomePhone', $data[9]);
        $stmt->bindParam(':memberMobilePhone', $data[10]);
        $stmt->bindParam(':memberPaidUntil', $data[12]);
        $stmt->bindParam(':memberUserName', $data[0]);
        $stmt->bindParam(':memberPassword', strtolower($user_password_hash));

        // unset the first line like this
        fgets($handle);

        // created loop here
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
            $stmt->execute($data);
        }

        fclose($handle);

    } catch(PDOException $e) {
        die($e->getMessage());
    }

    echo 'Projects imported';

} else {
    echo 'Could not import projects';
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET['success'])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
    Choose your file: <br />
    <input name="uploadedfile" type="file" id="csv" />
    <input type="submit" name="Submit" value="Submit" />
</form>

</body>
</html>

But I keep getting the following errors:

Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/spha/csv-import-2.php on line 47

Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/spha/csv-import-2.php on line 47

Fatal error: Uncaught Error: Cannot pass parameter 2 by reference in /Applications/MAMP/htdocs/spha/csv-import-2.php:47 Stack trace: #0 {main} thrown in /Applications/MAMP/htdocs/spha/csv-import-2.php on line 47

Line 47 is $stmt->bindParam(':memberRealName', $data[1].' '.$data[2]);

and an example of the CSV is this:

H3,Wilfred,Baker,Woodcott,none,Hardington Moor,town,county,postcode,01935 123456,None,[email protected],31/03/2016

I've looked at a few questions/answers on here but don't 100% understand where I'm going wrong.

Any help is appreciated

Andy

7
  • I guess you cannot have this $data[1].' '.$data[2] in bind param Commented Mar 13, 2016 at 18:48
  • $data[1] and $data[2] don't exist. Also you can't pass a concatenated string to bind, you have to pass a variable. Commented Mar 13, 2016 at 18:49
  • try define variable for : $data[1].' '.$data[2] like : $foo = $data[1].' '.$data[2]; and use $foo in your bind Commented Mar 13, 2016 at 18:50
  • Hmmm okay, let me test some stuff Commented Mar 13, 2016 at 18:50
  • Okay so that appears to fix those errors, but i get undefined variable $data? Commented Mar 13, 2016 at 18:53

1 Answer 1

2

This has to change to :

$stmt->bindParam(':memberRealName', $data[1].' '.$data[2]);

This:

$merged_value = $data[1].' '.$data[2];

$stmt->bindParam(':memberRealName', $merged_value);

To hide php notice add the below code:

error_reporting(E_ALL & ~E_NOTICE);
Sign up to request clarification or add additional context in comments.

14 Comments

I still get errors, i've put all concatenated values into variables and it's saying undefined variable data :/
you can add this error_reporting(E_ALL & ~E_NOTICE); to hide notices
Hiding the notice won't fix that this code isn't inserting to the database :P
I cant see from where you are getting data in your data[0], data[1] etc
Why wouldn't you use a chat? This conversation is not helpful for a community.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.