在&之后保存mysql数据库中的不完整文本字符

时间:2014-11-10 10:15:52

标签: php mysql

我有一个接受文本并从中提取链接的输入,当检索时,它会显示一个可点击链接,一切正常,直到插入了包含多个变量的链接,例如www.mysite,com / page。 php?id = 20& desc = How-to-by-it它放弃了&字符。插入时,我使用mysqli_real_escape_string和htmlentities但它不接受这个(&)字符。请问如何逃脱它以保存在数据库中? 下面是我的代码示例 感谢。

// Clean the posted variables
$osid = preg_replace('#[^0-9]#', '', $_POST['sid']);
$account_name = preg_replace('#[^a-z0-9]#i', '', $_POST['user']);
$data = $_POST['data'];
$data = urldecode($data); 
// I ALSO TRIED THIS BUT NOT SAVING & character
//$data = str_replace("&","&",$data);
//$data = mysqli_real_escape_string($db_conx, $data);

// Insert the status reply post into the database now
$sql = "INSERT INTO status(osid, account_name, author, type, data, postdate)
       VALUES('$osid','$account_name','$log_username','b','$data',now())";
$query = mysqli_query($db_conx, $sql);
$id = mysqli_insert_id($db_conx);

1 个答案:

答案 0 :(得分:0)

使用

$e_url=urlencode($url);

对网址进行编码,以便您可以保存它们。并使用

$url=urldecode($e_url);

解码已编码的网址。

有用的链接:

http://php.net/manual/en/function.urlencode.php

http://php.net/manual/en/function.urldecode.php