首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php中插入带有特殊字符的查询

问在php中插入带有特殊字符的查询
EN

Stack Overflow用户
提问于 2013-04-26 01:32:27
回答 2查看 1.6K关注 0票数 1

以下是我的代码

代码语言:javascript
复制
<?php

$con    =   mysql_connect('localhost','test','test');
mysql_select_db('test',$con);


require_once("xml2json.php");

$testXmlFile = 'mytest.xml';

$xmlStringContents = file_get_contents($testXmlFile); 
$jsonContents = "";
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();
foreach($obj->rss->channel->item as $item) {

     echo $item->title."\n";
     echo $item->description."\n";
     $rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";
} 
        $del_horoscope = "delete from jos_horoscope";
        mysql_query($del_horoscope);

        $query = mysql_real_escape_string("INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ".implode(', ',$rows));
        mysql_query($query);

  if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();

?>

我无法在数据库中插入标题和描述。它总是说

代码语言:javascript
复制
Oh no! Something went wrong with the query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'What\'s special today for you\',\'Your birth on the 25th day of the month (7 e' at line 1

我尝试插入的一些数据是

代码语言:javascript
复制
What's special today for you
Your birth on the 25th day of the month (7 energy) modifies 
your life path by giving you some special interest in technical,
 scientific, or other complex and often hard to understand subjects.
 You may become something of a perfectionist and a stickler for
 details. Your thinking is logical and intuitive, rational and responsible.
 Your feelings may run deep, but you are not very likely to let them
 show. This birthday makes you a more private person, more 
introspective and perhaps more inflexible.
Aries Horoscope for Thursday, April 25, 2013
Although most of us when we make a decision we don`t have to worry what it will impact for the world we live in, but when you make a decision we have to remember that it does impact our society in so many ways. Our choice has to be an ethical one, regardless of the outcome. Your emotions are brought into more conscious focus these few days. You may find that you can more easily communicate your feelings at this time.

第一行是标题,其余是描述,直到白羊座星座。在那之后,另一个标题开始。请帮帮我。我试了很多方法,但对我不起作用

下面是我的表结构

​

​

请帮帮我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-26 01:51:06

您多次转义您的查询,以下是正确的方法:

代码语言:javascript
复制
foreach($obj->rss->channel->item as $item) {
     $rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";
} 

现在你不需要再逃避它了:

代码语言:javascript
复制
$query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ".implode(', ',$rows);
mysql_query($query);
票数 3
EN

Stack Overflow用户

发布于 2013-04-26 01:55:17

首先,您不希望转义实际的插入字符串。只需插入要插入的值。

代码语言:javascript
复制
$query = mysql_real_escape_string("INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ".implode(', ',$rows));

就是你目前正在做的事情。只需对要插入的每个值进行转义。这就引出了下一个问题。如果$rows[]多次运行,则在foreach循环中构建的foreach数组的格式不正确。你的insert应该在foreach的内部。只需删除数组即可。

代码语言:javascript
复制
foreach($obj->rss->channel->item as $item) {
 echo $item->title."\n";
 echo $item->description."\n";
 $title = mysql_real_escape_string($item->title);
 $description = mysql_real_escape_string($item->description);

$query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ('$title', '$description')";
mysql_query($query);
} 

那我就不太确定delete查询的目的了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16221125

复制
相关文章

相似问题

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