【发布时间】:2017-05-20 19:22:06
【问题描述】:
我正在使用 PHP 创建一个社交网站。用户可以选择将状态发布到他们的时间线。但是当我尝试发布到我的时间线时,我得到Post added to your timeline.echo 声明,但是当我在线访问我的数据库时,用户名列已填写,正文为空。有人能帮我吗 ?
profile.php:
<form action="poststatus.php" method="post">
<textarea rows="3" cols="25" name="status" id="status">
</textarea>
<button id="bt4" type="submit" name="bts">Post status</button>
</form>
poststatus.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include("connect.php");
include("auth_login.php");
// just define at the top of the script index.php
$username = '';
$username = $_SESSION['username'];
$body = '';
if(isset($_POST['bts'])) {
if (empty($_POST["status"])) {
echo"You didn't enter anything . <a href= profile.php>Try again</a>";
} else {
$sql = "INSERT INTO posts (username, body ) VALUES ('" . $username . "', '" . $body . "')";
if(mysqli_query($conn, $sql)){
echo"<a href= home.php>Post added to your timeline.</a>";
} else{
echo "<br>error posting . <br> <a href= profile.php>Try again</a> " .
mysqli_error($conn);
}
}
}
【问题讨论】:
-
您确定 yiu 在 $username 和 $body 中具有正确的值...在插入之前尝试 var:dump($username) ..
-
因为您要插入一个空值。您认为这一行将变量设置为什么?:
$body = '';另请注意,此代码对 SQL 注入完全开放,并且您允许用户执行任意代码在您的服务器上。 -
它给我看这个我不知道这是什么意思
C:\wamp\www\testwebsite\poststatus.php:14:string 'username264' (length=11) -
@David 我这样做了
$body= ''; $body= $_SESSION['body'];,但同样的事情正在发生 -
@mkd:那么会话值似乎没有设置为任何值。你在哪里设置?为什么要插入会话值而不是表单中发布的内容?