[英]ISSET checking in php not working
我有這個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");
}
}
?>
<?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");
}
?>
通過縮進,我們可以看到您的else條件位於“ if (!isset($_POST['submit']))
”內。另外,我認為在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";
}
?>
編輯:
您需要執行“ if (isset($_POST['submit']))
”而不是“ if (!isset($_POST['submit']))
”來測試表單是否已提交。 我修復了上面的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.