0

I'm trying to insert into a database for the first time using PDO but I keep getting the error

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

Google tells me there is a mis match somewhere with the values I'm inserting but from what I can tell they add up

    $db = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $db->prepare("INSERT INTO ticket (userid, tID, query, date, status) VALUES (:userid, :ticketid, :query, :timestamp, :status)");

$stmt->bindParam(':userid', $userid, PDO::PARAM_STR, 100);
$stmt->bindParam(':tID', $ticketid, PDO::PARAM_STR, 100);
$stmt->bindParam(':query', $query, PDO::PARAM_STR, 100);
$stmt->bindParam(':date', $date, PDO::PARAM_STR, 100);
$stmt->bindParam(':status', $status, PDO::PARAM_STR, 100);

if($stmt->execute()) {
  echo "Ticket has successfully been added";
}
else {
  echo "Didnt work";
}

$db = null;

Any advice?

1
  • 3
    :ticketid vs :tID, and :timestamp vs :date Commented Jul 21, 2016 at 11:29

1 Answer 1

3

You bind paramater tID and date

$stmt->bindParam(':tID', $ticketid, PDO::PARAM_STR, 100);
$stmt->bindParam(':date', $date, PDO::PARAM_STR, 100);

But you named it :ticketid and timestamp in your query.

VALUES (:userid, :ticketid,:query, :timestamp,

So you have two Options.

First rename Parameter in bind Statement:

$stmt->bindParam(':ticketid', $ticketid, PDO::PARAM_STR, 100);
$stmt->bindParam(':timestamp', $date, PDO::PARAM_STR, 100);

or change your Statement to:

VALUES (:userid, :tID,:query, :date,
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers guys, was over looking the obvious. Working perfectly now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.