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
$data[1].' '.$data[2]in bind param$data[1]and$data[2]don't exist. Also you can't pass a concatenated string tobind, you have to pass a variable.