我正在处理一个简单的论坛脚本,我发布了一些安全功能,以防止发布空值。 问题是它没有给我正确的错误它只是显示第一条错误消息“你没有填写标题。”。
任何人都可以看到错误,因为我不能。
代码:
<?php
include('include/configdb.php');
session_start();
$username = $_SESSION['user_name'];
$title = $_GET['title'];
$message = $_GET['message'];
if ($title == NULL){
echo "U havent filled in a title. Go <a href='post.php'>Back</a>";
} else if($message == NULL) {
echo "U havent filled in a message. Go <a href='post.php'>Back</a>";
} else {
$sql = "SELECT * FROM forumuser WHERE username='$username'";
$query = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_array($query);
$beforep = $row['num_posts'];
$newposts = $beforep + 1;
$newsql = "UPDATE forumuser SET num_posts='$newposts' WHERE username='$username'";
mysqli_query($mysqli, $newsql);
header("location: index.php");
}
?>
表格代码:
<form method="POST" name="post" id="post" action="insert.php">
<b>Title of the topic</b><br />
<input name="title" type="text" id="title">
<br />
<b>the topic u want to post</b><br />
<textarea name="message" type="text" id="message" colls="50" rows="5">
</textarea>
<br />
<input type="submit" name="submit" value="Post Topic">
</form>
答案 0 :(得分:0)
传递带?title=
param的值以及表单操作。 $title
需要一些价值才能让您更进一步。
编辑
<input name="title" type="text" id="title" />
<br />
<b>the topic u want to post</b><br />
<textarea name="message" type="text" id="message" colls="50" rows="5">
</textarea>
<br />
<input type="submit" name="submit" value="Post Topic" />
您正在正确关闭代码。
答案 1 :(得分:0)
您的表单使用了帖子。所以
替换
$_GET
使用
$_POST
(我测试过,在我的表单中使用post并获得php不起作用)
另一件事是使用isset($_POST[variable])
来查看它是否真的为NULL。否则你会收到警告
所以你的代码应该像
<?php
include('include/configdb.php');
session_start();
$username = $_SESSION['user_name'];
if (!isset($_POST['title']) || $_POST['title'] == ''){
echo "U havent filled in a title. Go <a href='post.php'>Back</a>";
} else if(!isset($_POST['message']) || $_POST['message'] == ''
|| strlen(trim($_POST['message'])) == 0 || empty($_POST['message']) ){
echo "U havent filled in a message. Go <a href='post.php'>Back</a>";
} else {
$title = $_POST['title'];
$message = $_POST['message'];
$sql = "SELECT * FROM forumuser WHERE username='$username'";
$query = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_array($query);
$beforep = $row['num_posts'];
$newposts = $beforep + 1;
$newsql = "UPDATE forumuser SET num_posts='$newposts' WHERE username='$username'";
mysqli_query($mysqli, $newsql);
header("location: index.php");
}
?>
答案 2 :(得分:-1)
不确定这是否有用但是......
<?php
include('include/configdb.php');
session_start();
$username = $_SESSION['user_name'];
$title = isset( $_GET['title'] ) ? $_GET['title'] : NULL;
$message = isset( $_GET['message'] ) ? $_GET['message'] : NULL;
$msgs=array();
if( is_null( $title ) ) $msgs[]="U havent filled in a title. Go <a href='post.php'>Back</a>";
if( is_null( $message ) ) $msgs[]="U havent filled in a message. Go <a href='post.php'>Back</a>";
if( !empty( $msgs ) ){
echo implode( PHP_EOL, $msgs );
} else {
$newsql = "UPDATE forumuser SET num_posts=num_posts+1 WHERE username='$username'";
mysqli_query($mysqli, $newsql);
header("location: index.php");
}
?>
在我看来,您运行的查询只更新了forumusers表中的计数,因此您应该可以使用单个cmd执行此操作。
也就是说,使用你的代码存在严重的sql注入风险(因此也是我的代码) - 你真的应该开始使用PDO和预处理语句来缓解sql注入。
更新:为了记录,我只想注意,当我第一次回答问题时,表单的代码没有被包括在内,所以代码基于提供的$ _GET方法,最初有一些错位的关闭括号可能导致OP注意到的解析错误。