PHP if(isset($_POST...)) condition FAIL



PHP问题在这里,我制作了一个登录/注销类型的代码,具有insertdelete功能,支持loginlogout

所以问题是插入文本后我根本无法删除它,因为删除按钮就像一个简单的回头波顿,不做他的工作,什么都没有在if(isset($_POST['delete']))条件下似乎有效。

问题可能是我正在使用两个无效操作引用同一页面?导致第一个按钮工作和第二个不。

任何人都能理解为什么?

<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>

您的代码存在逻辑问题。单击删除按钮时,脚本将再次运行。您拥有的第一个条件 - if(!isset($_POST['send']))现在将通过,因为不再设置发送按钮,因此它会进入 if 语句并且永远不会运行您的删除代码。

您的脚本似乎也容易受到 SQL 注入的影响。

这是执行此操作的正确方法,这是一个快速提示,您需要在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>
我认为

它也可能有效...

if (!isset($_POST['submit']) || isa($_POST['submit'] != 'login')) 

最新更新