Jump to content

Populate list, and delete rows PHP/MySQL


Celcius

Recommended Posts

I am trying to do a query to populate a list (or table, or whatever).

Then if you click on them, they get deleted from their MySQL table.

I have 11 or so tables I want to do for this, but here is the CREATE TABLE statement for one of them.

CREATE TABLE Airline(
name VARCHAR(20),
code CHAR(5) PRIMARY KEY,
website VARCHAR(40)
);

 

So I want to populate a list with th entries of this table, and then when you click on a delete button beside the row, it deletes that tuple. 

I found this script, but it's not working.

<?php
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("SELECT * FROM Airline ORDER BY name"); 
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the enws
      $title=$r["name"];//take out the title
      $code=$r["code"];//take out the id
     
      echo "<a href='delete.php?cmd=delete&code=$code'>$title - Delete</a>";
      echo "<br>";
    }
}
?>

<?php
if($cmd=="delete")
{
    $sql = "DELETE FROM Airline WHERE code='$code'";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

 

 

Any ideas on a better script or how to fix that one?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/197387-populate-list-and-delete-rows-phpmysql/
Share on other sites

what version of php do you have? that seems to rely on an old setting that has become deprecated.

 

instead of

$cmd

try replacing occurrences of that with

$_GET['cmd']

 

btw the equivalent of lists in PHP are known as arrays. I don't believe there is a data structure in PHP that is like a tuple though.

Like this?

<?php
if(!isset($_GET['cmd'])) 
{
   //display all the news
   $result = mysql_query("SELECT * FROM Airline ORDER BY name"); 
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the enws
      $title=$r["name"];//take out the title
      $code=$r["code"];//take out the id
     
      echo "<a href='delete.php?cmd=delete&code=$code'>$title - Delete</a>";
      echo "<br>";
    }
}
?>

<?php
if($_GET['cmd']=="delete")
{
    $sql = "DELETE FROM Airline WHERE code='$code'";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>

 

Or am I just being dense? Because that doesn't seem to have helped.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.