将 PHP PDO 与 MYSQL 结合使用
以下作品
$customer = "2";
$ammount = "123.50";
$sql = $db->query("INSERT INTO transactions(transaction_customer,transaction_ammount) VALUES ($customer,$ammount)");
以下不起作用
$biggy = "Jim";
$chum = "Tester";
$sql = $db->query("insert into customers(customer_firstname,customer_lastname,customer_telephone) values ($biggy,$chum,'7576632423');");
有什么想法吗?
最佳答案
您的第一个示例有效,因为您使用的是数字。
第二个 SQL 语句中的字符串需要用引号引起来
$sql = $db->query("insert into customers(customer_firstname,customer_lastname,customer_telephone) values ('$biggy','$chum','7576632423');");
您可以使用 PHP 的 PDO 避免此类陷阱并使用准备好的语句而不是手动编写 SQL。我强烈建议您研究它 :)
关于php - 为什么类似的mysql查询不会执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059150/