0

Why does this not work? I want to be redirected to id?1 and remove.php removes it from table. How is this done?

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages") 
or die(mysql_error());  

echo "<table border='1'>";
while ($row = mysql_fetch_array($result)) {
        echo "<li class='list-group-item'>";
            echo $row['header'];
            echo "<br/>";
            echo $row['description'];
            echo "<br/>";
            echo "<button type='button' class='btn btn-default btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ändra innehåll</button>";
            echo "&nbsp;&nbsp;";
            echo "<form method='POST'><a href='delete.php?". $row['page_id']."' name='action' class='btn btn-danger btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ta bort sida</a></form>";

            echo  "<td><a href='obrisi.php?id=$id'>Delete</a></td>";

        echo "</li>";
    }

echo "</table>";
?>

delete.php

<?php
if (isset($_POST['1']))
     {
          foreach ($_POST['id'] as $page_id)
          {
               if (!mysql_query("DELETE FROM pages WHERE id = '$page_id'"))
               {
                    echo mysql_error();
               }
          }
     }
?>

Please help me. Thanks in advance.

  • You have forms with nothing more than links in them so that is not going to work. You should start with the manual: http://php.net/manual/en/tutorial.forms.php – jeroen Jan 11 '16 at 19:39
  • Could you please show an example of how it's done? – user3127823 Jan 11 '16 at 19:48
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 11 '16 at 19:53
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 11 '16 at 19:53

1 Answers1

2

You can not get the id in POST becuase you are using it as a Query String. You can get it as:

$page_id = intval( $_GET['id'] );

Now you can check it as:

if( $page_id > 0 ){
     // your stuff.. use this id in your query
}

What I have changed:

Use $_GET (SUPER GLOBAL) instead of $_POST becuase of query string.

Side Note:

There is no need to use foreach loop becuase it's not an array.

I suggest you to use mysqli_* or PDO instead of mysql_* becuase its deprecated and not available in PHP 7.

Form - PHP Manual

devpro
  • 16,184
  • 3
  • 27
  • 38