mike
mike

Reputation: 1896

Why will similar mysql query not execute?

Using PHP PDO with MYSQL

The following works

$customer = "2";
$ammount = "123.50";
$sql = $db->query("INSERT INTO transactions(transaction_customer,transaction_ammount) VALUES ($customer,$ammount)");

The following does not work

$biggy = "Jim";
$chum = "Tester";
$sql = $db->query("insert into customers(customer_firstname,customer_lastname,customer_telephone) values ($biggy,$chum,'7576632423');");

Any ideas why?

Upvotes: 0

Views: 37

Answers (2)

maček
maček

Reputation: 77778

Your first example works because you are working with numbers.

You need quotes around the strings in the 2nd SQL statement

$sql = $db->query("insert into customers(customer_firstname,customer_lastname,customer_telephone) values ('$biggy','$chum','7576632423');");

You can avoid pitfalls like this using PHP's PDO and using prepared statements rather than writing SQL by hand. I highly recommend you look into it :)

Upvotes: 4

Jay
Jay

Reputation: 3305

Strings need to be quoted.

... values ('$biggy','$chum','7576632423');");

You should be using mysql_real_escape_string too, though.

Upvotes: 3

Related Questions