0

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.

4 Answers 4

1

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

<a href=\"delete.php?id=$z[theIdKey]\">
Sign up to request clarification or add additional context in comments.

Comments

0

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");
?>

1 Comment

@shail base64 is encoding, not encryption.
0
<td><a href=\"delete.php\">delete</a></td>

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

1 Comment

Am not sure the correct way to pass the id so that it can delete that particular item retrieved from the data base
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.