使用php在mysql中插入特殊字符
当,我试图插入“我很好,你好吗”。而且,当我回顾数据库时,我发现只有“我很好”被插入,其余单词从该句子中被删除。当我在句子之间使用 ' 时,也会出现这个问题。
我的示例代码是
$title= mysql_real_escape_string($_POST['title']);
mysql_query("insert into table_db(TITLE) values ('".$title."')") or die(mysql_error());
我在谷歌上搜索的,找到了很多解决方案,但是,我的问题仍然没有解决。 帮助将不胜感激!
varchar(300)
When, I am trying to insert "I am Fine & How are you". And, when i look back to the database, then i see, only "I am Fine" is inserted rest words are trimmed from that sentence. This problem also arise, when i use ' in between the sentence.
My sample code is
$title= mysql_real_escape_string($_POST['title']);
mysql_query("insert into table_db(TITLE) values ('".$title."')") or die(mysql_error());
I searched on google, and found many solution, but, my problem still unsolved.
help will be appreciated !!
varchar(300)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用正确的方法使该字符串对于查询来说“安全”,因此查询本身不太可能导致问题。
TITLE
字段的类型/大小是什么?如果它是 char(9) 或 varchar(9),那么它只会保留您的“我很好”并忽略其余部分。You're using the proper method to make that string "safe" for the query, so it's unlikely to be the query itself causing the problem. What is the type/size of the
TITLE
field? If it's a char(9) or varchar(9), then that would just hold your "I am Fine" and ignore the rest.我对 PHP 不熟悉,但这种问题通常可以通过使用绑定参数(又名“绑定变量”)并让底层数据库 API 处理特殊字符来解决。
通过自己转义和连接字符串,您本质上是在押注转义函数相对于您选择的数据库是最新的,但这里的情况似乎并非如此。
I'm not familiar with PHP, but this kind of problem would generally be solved by using bound parameters (a.k.a. "bind variables") and letting the underlying database API worry about special characters.
By escaping and concatenating strings yourself, you are essentially betting that escape function is up-to-date relative to your database of choice, which appears not to be the case here.
尝试
代替
我的下面的代码工作正常
try
instead of
my below code works fine