使用PHP插入数据库时​​如何避免引号 [英] How to escape quotes when inserting into database with PHP

查看:147
本文介绍了使用PHP插入数据库时​​如何避免引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP非常新,如果听起来像这么简单的问题...:)

I'm quite new to PHP so sorry if sounds such an easy problem... :)

当插入内容时,我有错误信息引用到我的数据库。
这是我试图逃避引号,但没有工作:

I'm having an error message when inserting content which contains quotes into my db. here's what I tried trying to escape the quotes but didn't work:

$con = mysql_connect("localhost","xxxx","xxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$nowdate = date('d-m-Y')

$title =  sprintf($_POST[title], mysql_real_escape_string($_POST[title]));

$body = sprintf($_POST[body], mysql_real_escape_string($_POST[body]));

$sql="INSERT INTO articles (title, body, date) VALUES ('$title','$body','$nowdate'),";

if (!mysql_query($sql,$con))
  {

die('Error: ' . mysql_error());

}

header('Location: index.php');

您可以提供任何解决方案吗?

Could you provide any solution please?

提前感谢

Mauro

推荐答案

sprintf东西

$title = mysql_real_escape_string($_POST[title]);
$body = mysql_real_escape_string($_POST[body]);

这篇关于使用PHP插入数据库时​​如何避免引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆