PDO多记录插入麻烦

时间:2014-05-09 04:41:44

标签: pdo insert

我正在尝试使用带有MySQL数据库的PDO进行多记录插入语句。在搜索并解决了一些错误之后......我遇到了困难,需要帮助。

插入11列...我相信有189条记录要插入。

我检查了我的阵列以确保有' 11'每个()集合中的?并且在我的&#中有189个(?,?,?,?,?,?,?,?,?,?,?) 39;值数组'我传入。所以我认为问题在于数据阵列' (而不是'占位符'数组)。 (如果这有意义的话)

查询回显的示例< d out:     QUERY:INSERT INTO fontEntries(date_entered,font_name,font_maker,font_format,optimized_for,font_price,font_image,font_url,description,youtube_id,ip_address)VALUES(?,?,?,?,?,?,?,?,?,?, ?),(?,?,?,?,?,?,?,?,?,?,?),...... n

所有189套'在那里。

现在我遇到了无效的参数编号错误

我的代码:

<?php
$conn=new PDO("mysql:host=localhost; dbname=test;","root","");
//$conn->exec("SET CHARACTER SET utf8");


// displays all the file nodes
if(!$xml=simplexml_load_file('saberEntries.xml')){
    trigger_error('Error reading XML file', E_USER_ERROR);
}
//print_r($xml);
$totalCount = 0;
$ip_address = $_SERVER["REMOTE_ADDR"];
$detailArray = array();
foreach($xml->entry as $fontEntry){
    global $totalCount;

    $details = array($fontEntry->fontName, $fontEntry->attributes()->submissionDate, $fontEntry->fontCreator. $fontEntry->fontFormat,$fontEntry->optimized,$fontEntry->fontPrice,$fontEntry->fontImage,$fontEntry->fontURL,$fontEntry->demoLink,$fontEntry->description,$ip_address);

    //$detailArray[] = $details;
    foreach($details as $item){
        $detailArray[] = $item;
    }
    $totalCount++;
    //echo ($totalCount + 1 '.) Array Check: '.$details[0] ."<br>");
}
echo "Total Entries in XML: $totalCount <br>";
echo "Total Array Length: ". count($detailArray) ."<br>";
//echo 'Array Check: '.$detailArray[0] ."<br>";

$qMarks = str_repeat('(?,?,?,?,?,?,?,?,?,?,?), ', count($detailArray)-1) . '(?,?,?,?,?,?,?,?,?,?,?)';
//echo 'Q-MARKS: '.$qMarks . '<br>';
//var_dump($detailArray) ."<br>";

$myQuery = "INSERT INTO fontEntries(date_entered, font_name, font_maker, font_format, optimized_for, font_price, font_image, font_url, description, youtube_id, ip_address) VALUES $qMarks";

$import_statement = $conn->prepare($myQuery);

$import_statement->execute($detailArray);   

//check is insert was successful
 if ($import_statement->execute()) {
    //true 
    echo 'INSERT SUCCESSFUL';
}else{
    //false
    echo 'INSERT FAILED <br>';
    $errorcode = $import_statement->errorCode();
    echo 'ERROR CODE: '.$errorcode .'<br>';
    $error = $import_statement->errorInfo();
    echo 'ERROR CODE 2: '.$error .'<br>';
}
?>

错误:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\wamp\www\xml_tests\simpleXML_test.php on line 79

第79行:

$import_statement->execute($detailArray);

有人指出我的错误是什么?感谢。

工作代码:(感谢您的常识和建议/帮助)

<?php
$conn=new PDO("mysql:host=localhost; dbname=test;","root","");
//$conn->exec("SET CHARACTER SET utf8");

// displays all the file nodes
if(!$xml=simplexml_load_file('target_xml.xml')){
    trigger_error('Error reading XML file', E_USER_ERROR);
}

$totalCount = 0;
$ip_address = $_SERVER["REMOTE_ADDR"];

foreach($xml->entry as $fontEntry){

    $details = array($fontEntry->attributes()->submissionDate, $fontEntry->fontName, $fontEntry->fontCreator, $fontEntry->fontFormat, $fontEntry->optimized, $fontEntry->fontPrice, $fontEntry->fontImage, $fontEntry->fontURL, $fontEntry->description, $fontEntry->demoLink, $ip_address);    

    //dump/parse into one big/long (1-dimensional) array to pass to PDO
    foreach($details as $item){
        $detailArray[] = $item;
    }

    $totalCount++;
    echo ('Entry#: '.$totalCount.'<br/>');
}
echo "Total Entries in XML: $totalCount <br>";
echo "Total Array Length: ". count($detailArray) ."<br><br>";

$qMarks = str_repeat('(?,?,?,?,?,?,?,?,?,?,?), ', $totalCount-1 ). '(?,?,?,?,?,?,?,?,?,?,?)';
//echo 'Q-MARKS: <br>'.$qMarks . '<br>';
//print_r($detailArray) ."<br>";
//var_dump($detailArray) ."<br>";

$myQuery = "INSERT INTO fontEntries(date_entered, font_name, font_maker, font_format, optimized_for, font_price, font_image, font_url, description, youtube_id, ip_address) VALUES $qMarks";

$import_statement = $conn->prepare($myQuery);

$import_statement->execute($detailArray);   

//check is insert was successful
 if ($import_statement->execute()) {
    //true 
    echo 'INSERT SUCCESSFUL';
}else{
    //false
    echo 'INSERT FAILED <br>';
    $errorcode = $import_statement->errorCode();
    echo 'ERROR CODE: '.$errorcode .'<br>';
    $error = $import_statement->errorInfo();
    echo 'ERROR CODE 2: '.$error .'<br>';
}
?>

1 个答案:

答案 0 :(得分:0)

代替$detailArray[] = $details;使其成为

foreach ($details as $item) $detailArray[] = $item;