0

我对使用数据库非常陌生,我已经研究了几天,但我无法通过无效参数编号。我已经削减了代码和选项,只是试图使用 PHP 5.2 将两个数据导入 MySQL 5.1 db 我可以很好地连接到 db,并且基于 echo 语句,我确信我可以进入 prepare 语句。

完整代码如下

 $DBHandle= new PDO('mysql:localhost;dbname=nameishere','userishere','passishere');

//* $DBHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); --for USE
//* below is what I am currently using for debugging

$DBHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

//*next bit is to insure that if connection is lost database is not partially updated-I think- right now commented out
//* $DBHandle->beginTransaction();


    $mainIncrement= NULL;
    $firstnameOBS= $_POST['touristfirstname'];
    $todaysdateOBS= $_POST['touristdatetoday'];
    //*$picturenow= $_POST['picturesubmitted'];

    $JSONfirstname = json_encode($firstnameOBS);
    $JSONtodaysdate = json_encode($todaysdateOBS);
    //*$JSONpicturenow = json_encode($picturenow);

    echo ($JSONfirstname);


    $senditin = $DBHandle->prepare("INSERT INTO 'fkarnd'('firstname','datetoday') VALUES(:field1,:field2)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

    $senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
    $senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);

    //* $myinputarray = array('firstname'=> $JSONfirstname, 'datetoday' => $JSONtodaysdate );

    $senditin->execute();


    //* commit allows transaction begun to complete
    //* $DBHandle->commit();



  //*  catch ( PDOException $e ) 
     echo "I'm sorry, I can't do that Dave......";
  //*file_put_contents( 'dbErrors.txt', $e->getMessage(),FILE_APPEND);  


  //*  echo "successful submission for the preservation of JohnsPass";
    $DBHandle = null;

我尝试将数据放入数组中然后执行,我尝试了几种不同格式的 prepare 语句,包括INPUT ()INTO table WHERE().... 我试过没有绑定。我真的只是想从我创建的 HTML5 表单中将数据放入数据库中。表单数据基于 echo statments 可以正常处理,并且大多数 PHP 示例似乎处理的是 SELECT 而不是 INPUT。

下面是输出和我无法弄清楚的错误

"Dread"
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /home/mabelsbi/public_html/johnspass.org/Science/FirstTry.php on line 46

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number in /home/mabelsbi/public_html/johnspass.org/Science/FirstTry.php on line 46
4

1 回答 1

0
$senditin = $DBHandle->prepare("INSERT INTO 'fkarnd'('firstname','datetoday') VALUES(:field1,:field2)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

$senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
$senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);

您在查询中使用:field1and :field2,然后将:firstnameand设置:datetoday为参数;你只需要在你设置的内容上保持一致:

$senditin = $DBHandle->prepare("INSERT INTO `fkarnd` (`firstname`,`datetoday`) VALUES(:firstname,:datetoday)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

$senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
$senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);

(编辑为在 SQL 中围绕字段和表名使用反引号)

于 2012-10-25T16:09:23.663 回答