0

这里的 PHP 问题,我制作了一个登录/注销类型的代码,其中一个insertdelete函数支持loginand logout

所以问题是,在我插入文本后,我根本无法删除它,因为删除按钮就像一个简单的返回按钮,并且没有使他的工作,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>

4

3 回答 3

2

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

您的脚本似乎也容易受到 SQL 注入的攻击。

于 2012-09-26T23:50:34.527 回答
1

这是执行此操作的正确方法,这是一个快速提示,您需要在 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>
于 2012-09-27T00:05:21.557 回答
0

我认为它也可以工作......

if (!isset($_POST['submit']) || isa($_POST['submit'] != 'login')) 
于 2012-10-13T06:54:56.377 回答