我有这个PHP代码。当我尝试手动导航到此PHP脚本而不提交时,未显示未设置帖子的错误。我做错了什么?
<?php
if (!isset($_POST['submit']));
{
$productname=$_POST['productname'];
//$conn string will go here
$result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
if($result)
{
echo "<font color=\"green\">";
echo("Successfully Inserted new Products");
echo"</font>!";
}
else
{
echo("error");
}
}
?>
答案 0 :(得分:2)
<?php
// semicolon removed
if (isset($_POST['submit'])) {
$productname=$_POST['productname'];
//$conn string will go here
$result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
if($result) {
echo "<font color=\"green\">";
echo("Successfully Inserted new Products");
echo"</font>!";
}
} else { // else placed correctly ...
echo("error");
}
?>
答案 1 :(得分:1)
通过缩进,我们可以看到您的其他条件位于&#34; if (!isset($_POST['submit']))
&#34;
另外,我认为在if (!isset($_POST['submit']))
<?php
if (!isset($_POST['submit'])); // <== This semicolon shouldn't exist
{
$productname=$_POST['productname'];
//$conn string will go here
$result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
if($result)
{
echo "<font color=\"green\">";
echo("Successfully Inserted new Products");
echo"</font>!";
}
else
{
echo("error");
}
} // <== Need an "else" here
?>
以下是代码应该如何
<?php
if (isset($_POST['submit']))
{
$productname=$_POST['productname'];
//$conn string will go here
$result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
if($result)
{
echo "<font color=\"green\">";
echo("Successfully Inserted new Products");
echo"</font>!";
}
else
{
echo "<font color=\"red\">";
echo("Error when inserting");
echo"</font>!";
}
}
else
{
echo "error";
}
?>
编辑:
你需要做&#34; if (isset($_POST['submit']))
&#34;而不是&#34; if (!isset($_POST['submit']))
&#34;测试表单是否已提交。我修改了上面的代码。