2010-08-25 72 views
1

我想做一两件事,只有一两件事。Mysql的转义字符串的Youtube嵌入代码不工作

$embedCode = mysql_real_escape_string('<object width="270" height="227"><param name="movie" value="http://www.youtube.com/v/pz-VWi5-tGA?fs=1&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/pz-VWi5-tGA?fs=1&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="270" height="227"></embed></object>'); 

现在如果我写的......

echo 'CODE = ' . $embedCode; 

我得到...

CODE = 

没什么......

的思考?

编辑:

好了,我的目的不是只打印$ embedCode,它是将其插入到数据库中,但我发现了一个空值。我想我会成为一自作聪明,这与我在这里的简单化的方法背道而驰。无论如何,重点是,它没有通过我的MySQL查询。

编辑2: 我使用WordPress的$对象WPDB

function insert_video(){ 

    global $wpdb; 
    $wpdb->show_errors(); 
    $table_name = $wpdb->prefix . "video_manager"; 

    $embedCode = mysql_real_escape_string('<object width="270" height="227"><param name="movie" value="http://www.youtube.com/v/pz-VWi5-tGA?fs=1&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/pz-VWi5-tGA?fs=1&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="270" height="227"></embed></object>'); 
    $title = 'this is my title'; 
    $description = 'this is my description'; 

    $wpdb->insert($table_name, array('title' => mysql_real_escape_string($title), 'embed_code' => $embedCode, 'description' => mysql_real_escape_string($description))); 

} 

function get_video_block($id){ 
    insert_video(); 
    global $wpdb; 
    $wpdb->show_errors(); 
    $table_name = $wpdb->prefix . "video_manager"; 
    $query = "SELECT * FROM " . $table_name . " WHERE `index` = '$id'"; 
    $results = $wpdb->get_results($query, ARRAY_A); 


    $results = $results[0]; 

    $returnString = $results['title'] . '<br>'; 
    $returnString .= $results['embed_code'] . '<br>'; 
    $returnString .= $results['description'] . '<br>'; 

    return $returnString; 

} 

并得到结果:

this is my title<br><br>this is my description<br> 
+2

你查看HTML文件,或者只显示位的来源是什么? 'mysql_real_escape_string'不是为了转义HTML而设计的;它的目的是为了逃避SQL查询中的东西。因此,它不会逃避标签,只有引号。 – Amber 2010-08-25 02:39:24

+0

我正在查看源代码,并且没有任何内容。不知怎的,我的字符串被杀死了。最终,它的意图是放在数据库中,但我不断地在我的行中得到一个空值。编辑(好吧,它在那里)... – 2010-08-25 02:41:03

+0

你能提供更多的代码上下文吗?也许你的变量超出了范围,或者没有被正确引用? – Amber 2010-08-25 02:43:46

回答

1

您打印您的HTML好吧。右键单击并查看它应该在那里的源代码。

mysql_real_escape_string并不意味着逃避HTML的。

如果你看一下实际的数据在你的表与phpMyAdmin会发生什么?如果它不存在,那么问题是,当你输入的是数据的采集。

好了,所以你逃避它,而你用别的东西来sanytise数据^喜欢用strip_tags它写入表? Strip_tags会把所有的HTML都带走。

是否有可能在wpdb_Class正在清理该HTML呢?

是看着codex.wordpress.org/Function_Reference/wpdb_Class你可以只需$ wpdb-> query('query')来运行任何查询,只需插入。如果它起作用,你已经修好了。

+0

这是我的问题。我没有收到数据到我的数据库。我已经在Edit:2中写出了上面的函数。我最初的问题有点简单,因为我有点沮丧。真的很高兴让每个人都为我跳回来:P无论如何,这就是我所能提供的。感谢您的输入。 – 2010-08-25 02:53:36

+0

好吧,大家都跳了起来,你必须写一个可以理解的问题,你可能会得到答案。 :)我正在看自己。 – Iznogood 2010-08-25 02:56:25

+0

RE:“它可能吗” 这是一个很好的问题。我敢打赌他们是。 (当我查看源代码时,我确实看到了嵌入代码,呵呵)。我想我可以在他们周围运行我自己的查询。 – 2010-08-25 03:09:33