duanhemou9834 2015-01-15 08:26
浏览 13
已采纳

在某些特殊情况下不会插入数据?

Here is the code I am using

if (!empty($_REQUEST['content'])&&!empty($_REQUEST['title'])&&!empty($_REQUEST['writer'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$writer = $_POST['writer'];
require_once 'htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();
$pure_content = $purifier->purify($content);
$entity_content = htmlentities($pure_content);
$entity_content = mysql_real_escape_string($entity_content);
mysql_query("INSERT INTO stories (TITLE, WRITER, CONTENT, UPVOTE, DOWNVOTE) VALUES ('$title', '$writer', '$content', 0, 0)"); 

Now, after some testing I found out whenever I type an apostrophe some where like it's then the values don't get inserted in table. How do I prevent this? Are there any other special characters that might cause this problem. Here is what I am working on: http://8mags.com/bored/people/

Edit

I have updated these two lines of code

$add_content = "INSERT INTO stories (TITLE, WRITER, CONTENT, UPVOTE, DOWNVOTE) VALUES ('$title', '$writer', '$content', 0, 0)"; 
$result = mysqli_query($mysqli, $add_content) or die(mysqli_error($mysqli));

Is there anything else that I need to change?

Second Update

I have changed this code too

$con = mysqli_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($db_database, $con);

Is there anything else? Thank you for the help.

  • 写回答

1条回答 默认 最新

  • duanlei2150 2015-01-15 08:53
    关注

    You just need to put this at time of insertion.

    $title = addslashes($_POST['title']);
    $content = addslashes($_POST['content']);
    $writer = addslashes($_POST['writer']);
    

    And at time of show(listing)

    stripslashes($VariableName)
    

    Hope this help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看