PHP表单/发布错误

时间:2012-09-12 01:10:56

标签: php mysql html forms

好吧,我的帖子脚本有点问题。一切都很好,除了当我输入一个像撇号的短语,例如“这是我的标题”不会​​发布,把“Heres my title”发布。不知道为什么会这样。也许条带标签我真的不知道,我该怎么办?如果它不起作用我得到底部错误消息

} else {
if(isset($_POST['what'])&&isset($_POST['when'])&&isset($_POST['where'])&&isset($_POST['details'])&&isset($_POST['sponsored_by'])&&isset($_POST['collegeId'])){

$what = nl2br(htmlspecialchars(strip_tags(stripslashes(trim($_POST['what'])))));

$where = nl2br(htmlspecialchars(strip_tags(stripslashes(trim($_POST['where'])))));

$when = nl2br(htmlspecialchars(strip_tags(stripslashes(trim($_POST['when'])))));

$sponsored_by = nl2br(htmlspecialchars(strip_tags(stripslashes(trim($_POST['sponsored_by'])))));

$details = nl2br(htmlspecialchars(strip_tags(stripslashes(trim($_POST['details'])))));
    $collegeId = intval($_POST["collegeId"]);
    if(isset($_SESSION['username'])){

$username = htmlspecialchars(strip_tags(stripslashes(trim(($_SESSION['username'])))));
        $query = "select id, Name from users where username='$username' and activated = 1";
        $doQuery = mysql_query($query);
        if(mysql_num_rows($doQuery)>0){
            $results = mysql_fetch_array($doQuery);
            $userName = $results['Name'];
            $email = $username;
            $id = $results['id'];
            $query = "insert into events values(NULL,$id,$collegeId,'$what','$when','$where','$details','$sponsored_by',NOW())";
            if(mysql_query($query)) header("Location: collegeInfo.php?college=$collegeId&message=added");
            else echo "Failed to create new Event!".$query;

1 个答案:

答案 0 :(得分:6)

你不要逃避你的查询。尝试至少使用addslashesmysql_real_escape_string清理您的输入。所以stripslashes - 从行中删除反斜杠。使用addslashes添加它们,而不是在mysql查询之前删除。

而且,在新的php版本中,dude,mysql_query DEPRECATED ,请将PDO与准备好的语句一起使用。