PHP if(isset($_POST...)) 条件失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12612069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP if(isset($_POST...)) condition FAIL
提问by user1701756
PHP problem here, I've made a login/logout kind code, with a insertand deletefunction that stand by loginand logout.
PHP 问题在这里,我制作了一个登录/注销类型的代码,带有一个insert和delete功能,支持login和logout。
So the problem is that after I insert the the text I simply cannot delete it, cause the delete button is like a simple
turn back botton, and doesn't make his work, nothing
in the if(isset($_POST['delete']))condition seems to work.
所以问题是,在我插入文本后,我根本无法删除它,因为删除按钮就像一个简单的返回按钮,并且不会使他的工作,if(isset($_POST['delete']))条件中的任何内容似乎都不起作用。
May the problem be that I'm using two void action that refer to the same page? cause the first button work and the second not.
问题可能是我使用了两个指向同一页面的 void 操作吗?导致第一个按钮起作用,第二个按钮不起作用。
Anyone can understand why?
任何人都可以理解为什么?
<html>
<header></header>
<body>
<!-- START PHP -->
<?php
//If not submit i put the submit form
if(!isset($_POST['send'])){
echo "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}<!-- IF END -->
//If submit was set I insert $text into the db and I render
//the delete button
else {
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
$dato=$_POST['dato'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
echo "Operation complete";
//Now i render the delete submit button...
echo "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
//...and if i push it NOTHING, like it's only
//a return to the first form button
if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
echo "<br>Text'".$text."' deleted";
}
}<!-- ELSE END-->
?><!-- END PHP -->
</body>
</html>
回答by Develoger
Here is the right way to do this, it is a quick tip, you need to work a little more on mysql insert security etc.
这是执行此操作的正确方法,它是一个快速提示,您需要在 mysql 插入安全性等方面做更多工作。
<html>
<header>
<body>
<?php
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
if(isset($_POST['send'])){
$text = $_REQUEST['text'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
$answer = "Operation complete";
$form = "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
}
else if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
$answer = "Text'".$text."' deleted";
}
else {
$form = "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}
print "<h1>" . $answer . "</h1>";
print $form;
?>
</body>
</header>
</html>
回答by Tim Fountain
There is a logic problem with your code. When the delete button is clicked, the script runs again. The first condition you have - if(!isset($_POST['send']))will now pass, since the send button is no longer set, and so it goes into the if statement and never runs your delete code.
您的代码存在逻辑问题。单击删除按钮后,脚本将再次运行。您拥有的第一个条件 -if(!isset($_POST['send']))现在将通过,因为不再设置发送按钮,因此它进入 if 语句并且永远不会运行您的删除代码。
Your script also appears to be vulnerable to SQL injection.
您的脚本似乎也容易受到 SQL 注入的影响。
回答by Kishore Kick
I think it may also work...
我觉得它也可以工作...
if (!isset($_POST['submit']) || isa($_POST['submit'] != 'login'))

