0

PHP problem here, I've made a login/logout kind code, with a insert and delete function that stand by login and 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.

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.

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>

3
  • where are you defining $text? Sep 26, 2012 at 23:49
  • @Paul i think he is using register globals
    – Flosculus
    Sep 26, 2012 at 23:51
  • the <!-- IF END --> style syntax inside your <?php ?> block gives me a syntax error.
    – S. Albano
    Sep 27, 2012 at 1:02

3 Answers 3

2

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.

Your script also appears to be vulnerable to SQL injection.

3
  • It should trigger the delete here if(isset($_POST['delete'])){ Sep 26, 2012 at 23:53
  • It will never get to that point since it won't go into that else. Sep 26, 2012 at 23:54
  • What he is saying is that because you have that statement inside the else of the first statement, it will never run because the delete form isnt sending the 'send' variable anymore. Place the delete statement above the first statement so when the delete is run, the delete is made, then site returns to its original state.
    – Flosculus
    Sep 26, 2012 at 23:54
1

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.

<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>
1
  • Many tanks :D I must admit that in a day I had changed the code a lot and now it work like I want, but I've understood some nice things from your code, again tanks :D Sep 28, 2012 at 0:38
0

I think it may also work...

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.