Divine Grace Blanza
Divine Grace Blanza

Reputation: 29

Delete record in database

enter image description here enter image description here

$qry=mysql_query("Select useradmin from tbladmin");
$credentials=mysql_fetch_array($qry);
if(!isset($_SESSION['user'])){
header("location:index.php");
exit();
}
if(isset($_REQUEST['id'])){ 
mysql_query("DELETE FROM tblru WHERE userid=" . $_REQUEST['id']);
if(mysql_affected_rows($con)>0){
header("location:administration.php");
exit();
}
else{
echo "ERROR in deleting the user!";
}
}
?>

this upper part is placed on top. and the lower part is in the body. UserID Username Password Add Users

<?php
$result=mysql_query("SELECT * FROM tblru");
if(mysql_num_rows($result)>0){      
while($row=mysql_fetch_array($result)){
echo "<tr bgcolor='#999999'>
<td>" . $row[0] . "</td>
<td>" . $row[1] . " </td>
<td>" . $row[2] . "</td>
<td><a href='admininistration.php?id=$row[0]' onClick=\"return confirm('Confirm Deletion of Registered User?');\"><font color='#FFFFFF'>Delete</font></a></td></tr>";
}
}
?>

I cant delete the users data in the database i made. the tblru has id(this is int and is auto incremented, user, and password. i would like to ask if the deletion of data is possible.

Upvotes: 0

Views: 127

Answers (2)

Arrok
Arrok

Reputation: 116

Do you have an administration.php file? Is all this code in it?The errors says that the page does not exist.

This prints out just fine, it has " " not ' ' so it is seen as variable, just tested it.

$row[0] = rand(1,10);
$row[1] = rand(1,10);
$row[2] = rand(1,10);
echo "<tr bgcolor='#999999'>
<td>" . $row[0] . "</td>
<td>" . $row[1] . " </td>
<td>" . $row[2] . "</td>
<td><a href='admininistration.php?id=$row[0]' onClick=\"return confirm('Confirm Deletion of Registered User?');\"><font color='#FFFFFF'>Delete</font></a></td></tr>";

Are you sure you are passing by this part?

if(!isset($_SESSION['user']))
{
    header("location:index.php");
    exit();
}

if you are not, you will be redirected to index.php and the rest of the script isn't going to run.

set $_SESSIONp['user'] = "something"; just before that just for test, are you getting the "ERROR in deleting the user!" when trying to delete?

Also separate the logical part from the display. and use md5() or sha1() for password encryption.

$result=mysql_query("SELECT * FROM tblru");
if(mysql_num_rows($result)>0)
{      
    $output = "<table>"; 
    while($row=mysql_fetch_array($result))
    {
        $output .= "<tr bgcolor='#999999'>";
        $output .= "<td>" . $row[0] . "</td>";
        $output .= "<td>" . $row[1] . "</td>";
        $output .= "<td>" . $row[2] . "</td>";
        $output .= "<td><a href='admininistration.php?id=$row[0]' onClick=\"return confirm('Confirm      Deletion of Registered User?');\"><font color='#FFFFFF'>Delete</font></a></td></tr>";
        }
        $output .= "/table";
}

print_r $output;

Upvotes: 0

Craftein
Craftein

Reputation: 762

Your link on your code:

"</td><td><a href='admininistration.php?id=$row[0]'

Notice how you just include an array variable inside of a string? That should be...

"</td><td><a href='admininistration.php?id=".$row[0]."'

Use a proper string concatenation.

That's the reason you're not getting the right/proper value that you need.

You're getting $row[0] as the value, literally. Check your url to see.

Upvotes: 2

Related Questions