简体   繁体   中英

PHP PDO INPUT SQLSTATE[HY093]: Invalid parameter number: parameter was not defined error

I am very new to working with Data Bases, I have researched this for several days and I have not been able to get through the Invalid parameter number. I have cut back code and options just trying to get two data into the MySQL 5.1 db using PHP 5.2 I get a connection to the db fine and based on echo statements I feel confident that I am getting to the prepare statement ok.

The full code is below

 $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;

I have tried putting the data in an array then executing, I have tried several different formats for the prepare statement including INPUT ()INTO table WHERE()... . I have tried having no binding. I really am just trying to put data into the db from an HTML5 form I created. The form data comes over ok based on echo statments and most of the PHP examples seem to deal with SELECT not INPUT.

Below is the output and the error I cannot figure out

"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
$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);

You're using :field1 and :field2 in your query, and then setting :firstname and :datetoday as parameters; you just need to be consistent in what you're setting:

$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);

(Edited to use backticks in SQL around field and table names)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM