我正在构建以下脚本并尝试使用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>
但我一直收到以下错误:
注意:未定义的偏移量:第47行/Applications/MAMP/htdocs/spha/csv-import-2.php中的1
注意:未定义的偏移量:第47行/Applications/MAMP/htdocs/spha/csv-import-2.php中的2
致命错误:未捕获错误:无法通过/Applications/MAMP/htdocs/spha/csv-import-2.php:47中的引用传递参数2堆栈跟踪:/ Applications / MAMP / htdocs中引发的#0 {main}第47行/spha/csv-import-2.php
第47行是$stmt->bindParam(':memberRealName', $data[1].' '.$data[2]);
以及CSV的一个例子是:
H3,Wilfred,Baker,Woodcott,none,Hardington Moor,town,county,postcode,01935 123456,None,email@domain.com,31/03/2016
我在这里看了几个问题/答案,但不要100%理解我哪里出错了。
感谢任何帮助
安迪
答案 0 :(得分:2)
这必须改为:
$stmt->bindParam(':memberRealName', $data[1].' '.$data[2]);
此:
$merged_value = $data[1].' '.$data[2];
$stmt->bindParam(':memberRealName', $merged_value);
要隐藏php通知,请添加以下代码:
error_reporting(E_ALL & ~E_NOTICE);