Php Delete item from mysql data base

Hope some one can help me out here, i guess am not calling my functions right. Am trying to retrieve some data from my database and have a delete link attached to each items being retrieved, so that when ever i click on delete, it will delete that particular item which have the delete function.

My Code to retrieve items from database are as follows.

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction");
if(mysql_num_rows($y) != 0){

echo "<table bgcolor=\"white\" width=\"1000\" bordercolor=\"grey\" border=\"5\" >";
echo "<tr>
<td align=\"center\">No</td>
<td align=\"center\">Date</td>
<td align=\"center\">Current Balance</td>
<td align=\"center\">Avaliable Balance</td>
<td align=\"center\">Account Status</td>
<td align=\"center\">Delete Account</td>

</tr>";

while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
        echo "<tr>
        <td align=\"center\">".$count++."</td>
        <td align=\"center\">".$z[1]."</td>
        <td align=\"center\">".$z[2]."</td>
        <td align=\"center\">".$z[3]."</td>
        <td align=\"left\" width=\"300\">".$z[4]."</td>
        <td><a href=\"delete.php\">delete</a></td>
        </tr>";
    }
    echo "</table>";
    }
?>

And my code to delete

<?php
session_start();
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$id = $_GET['id'];
$sql = mysql_query("DELETE FROM transaction WHERE id='$id' LIMIT 1") or die   (mysql_error());
header("Location: vacct.php");
?>

I know am missing out the logic here and hope somebody can direct me or show me the easy way out. at the moment i can successfully retrieve my items from the data base my only problem is to be able to apply the delete function each time the delete button is tapped.

Upvotes: 0

Views: 2218

Answers (4)

Shail
Shail

Reputation: 1575

Use the below code.I have added validation and encryption

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction");
if(mysql_num_rows($y) != 0){

echo "<table bgcolor=\"white\" width=\"1000\" bordercolor=\"grey\" border=\"5\" >";
echo "<tr>
<td align=\"center\">No</td>
<td align=\"center\">Date</td>
<td align=\"center\">Current Balance</td>
<td align=\"center\">Avaliable Balance</td>
<td align=\"center\">Account Status</td>
<td align=\"center\">Delete Account</td>

</tr>";

while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
        echo "<tr>
        <td align=\"center\">".$count++."</td>
        <td align=\"center\">".$z[1]."</td>
        <td align=\"center\">".$z[2]."</td>
        <td align=\"center\">".$z[3]."</td>
        <td align=\"left\" width=\"300\">".$z[4]."</td>
        <td><a href=\"delete.php?id=".base64_encode($z[0])."\">delete</a></td>
        </tr>";
    }
    echo "</table>";
    }
?>

code to delete

<?php
session_start();
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$id = base64_decode($_GET['id']);
if(!empty($id)){
$sql = mysql_query("DELETE FROM transaction WHERE id='$id' LIMIT 1") or die   (mysql_error());
}
header("Location: vacct.php");
?>

Upvotes: 0

rationalboss
rationalboss

Reputation: 5389

Change:

<td><a href=\"delete.php\">delete</a></td>

to:

<td><a href=\"delete.php?id=".$z[0]."\">delete</a></td>

if $z[0] is the ID.

In your delete.php, make sure you also escape the word "transaction" using backtick:

DELETE FROM `transaction` WHERE id=123

this is because "transaction" is a reserved mysql keyword.

Please also read on SQL Injections.

Upvotes: 0

moonwave99
moonwave99

Reputation: 22817

You have to pass the id when you click on the delete link:

<a href=\"delete.php?id=$z[theIdKey]\">

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212412

<td><a href=\"delete.php\">delete</a></td>

How are you passing the id to delete to your delete.php script?

Upvotes: 0

Related Questions