0

trying to do and upload csv script and hitting an error that doesn't make sense. invalid token. It says my columns don't match my ?,?,?,?,?. 5 columns, 5 ? question marks, what did I miss?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
  <form enctype="multipart/form-data" method="POST">
        <input name="userfile" type="file">
        <input type="submit" value="Upload">
        <input name="row_name" type="text">
  </form>
<?php
$dsn = "mysql:host=$host;port=$port;dbname=$dbname"; //Data Source Name = Mysql
(isset($rowname = $_POST['row_name']));
$db = new PDO($dsn, $db_username, $db_password); //Connect to DB
$do = $db->prepare(
        "CREATE TABLE IF NOT EXISTS $username.$rowname (
         id INT AUTO_INCREMENT NOT NULL,
         list_name varchar(100) NOT NULL,
         fname char(60),
         lname char(60),
         list_email varchar(100),
         PRIMARY KEY (id)
        )
        CHARACTER SET utf8 COLLATE utf8_general_ci
        TRUNCATE TABLE $username.$rowname
        INSERT INTO $username.$rowname VALUES(?,?,?,?,?)"
        );
$csv_file = $_FILES['userfile']['tmp_name'];
if (($handle = fopen($csv_file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle)) !== FALSE) {
        $do->execute($data);
        var_dump($data);}
    fclose($handle);}
exit( "Complete!" );
?>
</body>
</html>

error

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/wemail1/www/pages/campaign-build.inc.php on line 58

Sample data:

fname, lname, email
joe, schmoe, bogus@bogus.com
mary, lamb, hoe@us.com
4

1 回答 1

0

您的 $data 没有 5 个参数。这也是错误消息的内容。您有 3 个值,而代码需要 5 个值。你需要这样的东西:

$insert = array(NULL, $data[0]." ".$data[1], $data[0],$data[1],$data[2]);
$do->execute($insert);

代替:

$do->execute($data);

那么,如果你有 5 ?在你的准备中,你需要一个有 5 个值的数组,否则你得到了那个错误。

于 2013-09-01T22:01:51.063 回答