Hey Guys,
I need some help with Deleting data from a MySQL .

Currently I have, Two PHP files.
the First is the main page.

<?php 
$host="";
$username="";
$password="";
$db_name="";
$tbl_name="";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<p>This Page Refreshes Every 10 Seconds</p>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><table width="200%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="7" bgcolor="#FFFFFF"><strong>Current Flight Plans using the GVA Flight Planner</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Tail Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Departure Airport</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Destination Airport</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>IFR or VFR</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cruising Altitude</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Remarks</strong></td>
<td align="center" bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><center><? echo $rows['ID']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['Username']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['TailNumber']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['DepartureAirport']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['DestinationAirport']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['IFRorVFR']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['CrustingAltitude']; ?></center></td>
<td bgcolor="#FFFFFF"><center><? echo $rows['Remarks']; ?></center></td>
<td bgcolor="#FFFFFF"><center><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></center></td>
</tr>
<?

// close while loop 
}

// close connection; 
mysql_close();

?> 
</table></td>
</tr>
</table>

And then I have my Delete_ac.php page with this

<?php

$host="";
$username="";
$password="";
$db_name="";
$tbl_name="";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$id=$_GET['id'];

$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='Administrator.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

mysql_close();

?>

And now My problem is when I click Delete on the Main page, it says it Deleted it Successfully but When I go back to the Main Page it still Shows the row the I clicked Delete on.
Can someone Please help me on why this is Happening? Thanks in Advance :)

Recommended Answers

All 10 Replies

Code is looking w/o error.
can you check $tbl_name variable in both page.

The table name Variable in Both Pages is the Same so no Error there.

Member Avatar for diafol

is this for admin only?

<? echo $rows['id']; ?>

you're using sort tags - do you see the id value in the querystring (url)?

this not v. secure - if anybody guesses the name of your page, they'll cause havoc. protect all deletes with login check and possibly a confirmation hash.

Yes It is For Administrators only, will be getting to the Password Protection soon.
Yeah, When I click Delete it does show something more than the ID value:
URL Looks like this
delete_ac.php?id=<br%20/><b>Notice</b>:%20%20Undefined%20index:%20id%20in%20<b>C:\xampp\htdocs\Flight%20Planner\Administrator.php</b>%20on%20line%20<b>52</b><br%20/>
But Im not sure If that is the ID or what.

Member Avatar for diafol

Nope, that's an error.

It says line 52, and my line 52 in the Administrator.php page is

<td bgcolor="#FFFFFF"><center><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></center></td>

But I dont see why there would be an error there, Maybe should I use something other than

<a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a>

?

Member Avatar for diafol
<? echo $rows['id']; ?>

did you change this to:

<?php echo $rows['id']; ?>

as I mentioned previously?

I Did change that to

<?php echo $rows['id']; ?>

But It is Still giving me this Error in the URL. The same one too, pointing to Line 52 Again. Nothing Different.

Member Avatar for diafol

OK, prob not a short tag issue, but you;ve also got a short tag here:

<?
 
// close while loop 
}
 
// close connection; 
mysql_close();
 
?>

So essentially what you have is:

<?php
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
...
while($rows=mysql_fetch_array($result)){
...
?>
<a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a>
...
<?php
...
}
?>

1. Does the 'id' field exist or is it called something else in the DB?
2. if it exists, are there any records? check with mysql_num_rows()
3. If all OK. Echo out the sql statement and see if it makes sense, and then run it in phpMySQL or the GUI of your choice. Does it work as expected / return what you thought?

Just use this code to test your sql statement is correct.

echo $sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.