if isset($ _ POST问题

时间:2014-06-27 18:57:28

标签: php html mysql forms

我有一个实例,我的PHP网站上的用户可以登录,我试图在管理员发布公告的第二个实例。

我的登录工作非常精彩,但出于某种原因,我无法将我的公告提交给工作。尽管我的输入字段被正确命名,但该字段被视为"未设置"当我在表格上输入标题和公告时。这是我的代码:

FORM:

        <form action="announce.php">
    <input  type="text" name="title" />
    <textarea  name="announce" cols="20" rows="2" ></textarea>
    <input type="submit" name="submit" value="Announce" />
    </form>

这是我的PHP:

include_once 'creds.php';
$con=mysqli_connect("$db_hostname","$db_username","$db_password","$db_database");

if (isset($_POST['title']))
{
    $title = $_POST['title'];
    $announce = $_POST['announce'];

$query = "INSERT INTO announcements (labname, name, author, announce)
        VALUES ($lab, $title, $username, $announce)";

$insert = mysqli_query($con, $query);

mysqli_close($con);

echo "added successfully";}
else
{
echo "something went wrong";
}

我的其他形式似乎有效,但是这个没有...我的语法在某处错了吗?

2 个答案:

答案 0 :(得分:1)

您的表单是作为GET请求提交的,而不是POST。

更改此行:

<form action="announce.php">

到此:

<form action="announce.php" method="post">

编辑:我应该在发布之前刷新页面。所有归功于noobie-php在我发布此消息之前的comment 30分钟内获得此权限!

答案 1 :(得分:0)

Php:

include_once 'creds.php';
$con=mysqli_connect("$db_hostname","$db_username","$db_password","$db_database");

if (isset($_POST['submit']))
{
    $title = $_POST['title'];
    $announce = $_POST['announce'];

$query = "INSERT INTO announcements (labname, name, author, announce)
        VALUES ($lab, $title, $username, $announce)";

$insert = mysqli_query($con, $query);

mysqli_close($con);

echo "added successfully";}
else
{
echo "something went wrong";
}

我认为if (isset($_POST['submit']))这就是问题所在。