-1

I am trying to run an sql query using PDO prepared statements

$sql = "INSERT INTO tickets (ticketnumber, status) VALUES (1234, Open) ";
$stmt = $connection->prepare($sql);
$stmt->execute();

But it is just not inserting. What have I done wrong?

Here is my connection:

$host = "localhost";
$db_name = "";
$username = "";
$password = "";
$connection = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
charlie
  • 1,356
  • 7
  • 38
  • 76

2 Answers2

3

Try this. It's much more secure. Make sure you have included your connection file.

EDITED

$sql = "INSERT INTO `tickets` (ticketnumber, status) VALUES (:ticketnumber, :status)";
$stmt = $connection->prepare($sql);
$stmt->bindValue(':ticketnumber', 1234, PDO::PARAM_INT);
$stmt->bindValue(':status', 'Open', PDO::PARAM_STR);
$stmt->execute();

Also, the named parameters used above must NOT be enclosed in quotes. If you do so, it'll be treated as a literal string and not a named parameter.

Shahlin Ibrahim
  • 1,049
  • 1
  • 10
  • 20
2
  1. You need to use quotes on strings before inserting them into a database.
  2. Why use prepare if you're not preparing your data before sending it to the database?
Marco Aurélio Deleu
  • 4,279
  • 4
  • 35
  • 63