【问题标题】:MYSQL PHP column emptyMYSQL PHP 列为空
【发布时间】: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:那么会话值似乎没有设置为任何值。你在哪里设置?为什么要插入会话值而不是表单中发布的内容?

标签: php html mysql mysqli


【解决方案1】:

您尚未定义 $body 变量应从何处获取其值,因此您的代码应如下所示:

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 {
   $body = $_POST["status"];
   $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);
} 
 } 
}

另外你不应该使用 clear myslq_query,因为它对 MySQL 注入很危险,而是尝试 PDO

【讨论】:

  • 注意:这仍然对 SQL 注入完全开放,并且不应在任何实时系统中使用。
  • 现在我收到了这个 else 声明 You didn't enter anything . Try again
  • @David 是的,当然,我在版本中提到过。他应该使用 PDO 或至少 mysqli_real_escape_string
  • @mkd 您还应该更改 HTML 部分中的字段名称
  • 谢谢它的工作我接受了答案。我在什么上使用 mysqli_real_escape_string ?比如什么字符串?
猜你喜欢
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2016-07-30
相关资源
最近更新 更多