PHP状态未发布

时间:2018-01-07 21:55:29

标签: php html html5 phpmyadmin

我正在创建一个社交网站,其中一项功能就是让用户发布状态。我目前在这里遇到问题。我创建了一个表单并在其中放置了一个文本区域,以便用户可以编写他们想要发布的状态。当他们单击post status按钮时,状态将被假定为进入数据库但不会发生。我继续得到其他声明You didn't enter anything . Try again。有人可以告诉我我的代码出错了吗?

profile.php:

<form action="poststatus.php" method="post">
<textarea rows="4" cols="40" id="txt1" placeholder="Post a status">
</textarea>
<button id="bt4" type="submit" name="bts">Post status</button>
</form>

poststatus.php:

// just define at the top of the script index.php
$username = ''; 
$username = $_SESSION['username'];

//Initializing variable
$body = ""; //Initialization value; Examples
         //"" When you want to append stuff later
         //0  When you want to add numbers later
//isset()
$body = isset($_POST['body']) ? $_POST['body'] : '';
//empty()
$body = !empty($_POST['body']) ? $_POST['body'] : '';

 if(isset($_POST['bts'])) {

    if (empty($_POST["body"])) {
         echo"You didn't enter anything . <a href= profile.php>Try again</a>";
        } else {
        $body = $_POST["body"];

          $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);
} 
} 
} else {
echo"Wasn't submitted";
}

2 个答案:

答案 0 :(得分:1)

textarea缺少attr名称

<form action="poststatus.php" method="post">
<textarea rows="4" cols="40" name="body" id="txt1" placeholder="Post a status">
</textarea>
<button id="bt4" type="submit" name="bts">Post status</button>
</form>

答案 1 :(得分:0)

name="body"添加到textarea:

<textarea name="body" rows="4" cols="40" id="txt1" placeholder="Post a status">

来自w3schools:

  

name属性指定元素的名称。

     

此name属性可用于引用JavaScript中的元素。

     

对于表单元素,它还在提交数据时用作参考。