mySQL逃脱字符串YouTube嵌入代码不起作用
-
01-10-2019 - |
题
我只是想做一件事和一件事。
$embedCode = mysql_real_escape_string('<object width="270" height="227"><param name="movie" value="http://www.youtube.com/v/pz-VWi5-tGA?fs=1&hl=en_US&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&hl=en_US&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="270" height="227"></embed></object>');
现在如果我写...
echo 'CODE = ' . $embedCode;
我得到...
CODE =
没有...
想法?
编辑:
好的,所以我的意图不仅要打印$嵌入码,而是将其插入数据库中,但是我得到了无效的值。我认为我会成为一个自作聪明,在这里以我简单的方法适得其反。无论如何,关键是,它没有通过我的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&hl=en_US&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&hl=en_US&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>
解决方案
您正在打印您的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')运行任何查询,因此只需插入即可。如果有效,您将修复。
不隶属于 StackOverflow