首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >插入到MySQL时在PHP中转义单引号

插入到MySQL时在PHP中转义单引号
EN

Stack Overflow用户
提问于 2010-04-22 10:25:33
回答 8查看 232.4K关注 0票数 216

我有一个令人困惑的问题,我似乎不能理解...

我有两条SQL语句:

  • 首先将表单中的信息输入到数据库中。
  • 第二个从上面输入的数据库中提取数据,发送电子邮件,然后记录transaction

的详细信息

问题是,单个引号似乎只在第二个条目上触发了MySQL错误!第一个实例可以正常工作,但第二个实例会触发mysql_error()

表单中的数据与表单中捕获的数据的处理方式是否不同?

查询1-没有问题地工作(并且没有转义单引号)

代码语言:javascript
复制
$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-输入带有单引号的名称时失败(例如O‘’Brien)

代码语言:javascript
复制
$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')");
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2687866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档