I'm trying to insert the results of a POST submission into a MySQL database.
<?php
if(isset($_POST['activity'])&&isset($_POST['timeLength'])&&isset($_POST['datevalue']))
{
ini_set('display_errors', 1);
$name=$_POST['activity'];
$timeLength=$_POST['timeLength'];
$datevalue=$_POST['datevalue'];
$datevalue=date("m-d-Y",strtotime($datevalue));
echo $name." ".$timeLength." ".$datevalue;
if(strlen(trim($timeLength))>0)
{
$dsn="mysql:host=database.net;dbname=databasename;charset=utf8";
$db=new PDO($dsn,'databasename','password');
$insertSQL="insert into preasc_activityLog (name,timeLength,datevalue) values (?,?,?)";
$stmt=$db->prepare($insertSQL);
$stmt->bindParam("sis",$name,$timeLength,$datevalue);
$stmt->execute();
}
}
else
{
echo "FAIL!";
}
?>
With my test data, the echo()
call returns:
program 2 11-22-2016
Fields in table to be inserted into have the respective data types char(40)
,mediumint
, and datetime
, so the data seems like it should be good for the INSERT statement.
But I get the following error result:
<b>Notice</b>: A non well formed numeric value encountered in
<b>/home/server/module/submit.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter
number: number of bound variables does not match number of tokens in
<b>/home/server/module/submit.php</b> on line <b>17</b><br />
The date seems like it should be acceptable, but it's causing the "non well formed" error (because this error disappears if I only use the first two parameters).
And there are 3 tokens in $insertSQL
, which match the 3 parameters in the bindParam()
call. So where's the inconsistency between tokens and bound variables?