我有一个令人困惑的问题,我似乎无法理解...我希望这里有人能够指引我朝着正确的方向...在PHP中插入MySQL时转义PHP中的单引号
我有两条SQL语句: - 第一条从表单中输入信息到数据库中。 - 第二个从上面输入的数据库中获取数据,发送电子邮件,然后记录交易的详细信息
问题是,它似乎是一个单引号只触发第二个条目上的MySQL错误! !第一个实例没有问题,但第二个实例触发mysql_error()
。
来自表单的数据是否得到与表单中捕获的数据不同的处理?
查询#1 - 这工作没有问题(并且没有逃逸单引号)
$result = mysql_query("INSERT INTO job_log
(order_id, supplier_id, category_id, service_id, qty_ordered, customer_id, user_id, salesperson_ref, booking_ref, booking_name, address, suburb, postcode, state_id, region_id, email, phone, phone2, mobile, delivery_date, stock_taken, special_instructions, cost_price, cost_price_gst, sell_price, sell_price_gst, ext_sell_price, retail_customer, created, modified, log_status_id)
VALUES
('$order_id', '$supplier_id', '$category_id', '{$value['id']}', '{$value['qty']}', '$customer_id', '$user_id', '$salesperson_ref', '$booking_ref', '$booking_name', '$address', '$suburb', '$postcode', '$state_id', '$region_id', '$email', '$phone', '$phone2', '$mobile', STR_TO_DATE('$delivery_date', '%d/%m/%Y'), '$stock_taken', '$special_instructions', '$cost_price', '$cost_price_gst', '$sell_price', '$sell_price_gst', '$ext_sell_price', '$retail_customer', '".date('Y-m-d H:i:s', time())."', '".date('Y-m-d H:i:s', time())."', '1')");
查询#2 - 用单引号(即奥布莱恩)
输入姓名时,该故障$query = mysql_query("INSERT INTO message_log
(order_id, timestamp, message_type, email_from, supplier_id, primary_contact, secondary_contact, subject, message_content, status)
VALUES
('$order_id', '".date('Y-m-d H:i:s', time())."', '$email', '$from', '$row->supplier_id', '$row->primary_email' ,'$row->secondary_email', '$subject', '$message_content', '1')");
呀好,感谢在正确编码的教训然而,这并没有回答为什么这两个查询不抛出同样的错误问题... – sjw 2010-04-22 03:01:26
我不在寻找如何解决它的答案 - 我知道如何解决它。我正在寻找“为什么”! – sjw 2010-04-22 03:02:36
这不是一个*正确*编码的问题,这是一个空洞和危险的安全漏洞问题。话虽如此,我会在我的原始答案中添加一段代码来说明潜在的问题。我会在这里做,但格式化将是所有的。 :) – awgy 2010-04-22 03:08:17