简体   繁体   中英

HY093: Wrong number of parameters - positional

I am getting the HY093 error on the $q->execute line.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'

$stmt = "INSERT INTO `survey`(`user`,`notes`,`lat`,`lon`,`acc`,`timestampx`) VALUES(?,?,?,?,?,?)";
$q = sql::$db->prepare($stmt);
var_dump($data);
$q -> execute($data);

and my vardump echos:

array(6) {
   ["user"]=>string(9) "Your Name"
   ["notes"]=>string(5) "Notes"
   ["lat"]=>string(10) "35.1338614"
   ["lon"]=>string(19) "-106.64091979999999"
   ["acc"]=>string(4) "8512"
   ["time"]=>string(13) "1442043552884"
}

When I copy and paste this data into MySQL (replacing the ? with the quoted strings) it works.

Any idea what I might be missing?

You are mixing the two ways of working with PHP. If you want to use an associative array to supply the bind values, your query needs to reference them by name, with a colon ( : ) to indicate these are bind variables:

$stmt = "INSERT INTO `survey`" .
        "(`user`,`notes`,`lat`,`lon`,`acc`,`timestampx`) " . 
        "VALUES(:user, :notes, :lat, :lon, :acc, :time)";

Alternatively, you could leave $query as is and provide the parameters in a simple, positional, array:

$data = array("Your Name", 
              "Notes", 
              "35.1338614", 
              "-106.64091979999999", 
              "8512",
              "1442043552884");

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