<?php
include_once("FTMdatabase.php");
$id = $_GET['AccountID'];
$sql = "DELETE FROM personaldetails WHERE AccountID = '$id'";
$result = mysql_query($sql, $conn);
$msg = "Data Has Been Removed";
confirm($msg);
mysql_close();
?>
<?php
function confirm($msg)
{
echo "<script language=\"javascript\">
alert(\"".$msg."\");
document.location.href='FTMlogin.php';
</script>";
}
?>
I try this code. But I got an error that says 'AccountID' undefined index. My 1st row in the database is AccountID. Why does it happen?
<?php
include_once("FTMdatabase.php");
$sql="SELECT * FROM personaldetails";
$result = mysql_query($sql, $conn);
while($row = mysql_fetch_array($result))
{
$id = $row['AccountID'];
$name = $row['Name'];
$phone = $row['Phone'];
$car = $row['Car'];
$comment = $row['Comment'];
?>
<table border="1">
<tr><td width="10"><?php echo $id; ?></td>
<td width="10"><?php echo $name; ?></td>
<td width="300"><?php echo $phone; ?></td>
<td width="100"><?php echo $car; ?></td>
<td width="100"><?php echo $comment; ?></td>
</tr>
<td><a href="FTMdelete.php?id=<?php echo $id;?>">
Delete</a>
</table>
<?php
This is where to get $id
. The reason I use $_GET['AccountID']
The error you are facing is not database error it's an php warning.
'AccountID' undefine index is because of this line
$_GET['AccountID'];
this is because you do not pass the AccountID to the page. You should use like this
if($isset($_GET['AccountID'])){
echo "No value for AccountID given";
exit;
}
If you are using mysql_query(); then there is no need of connection so
the line
$result = mysql_query($sql, $conn);
Should be like this
$result = mysql_query($sql);
By the way mysql is deprecated so use mysqli.
So the final result code will look like this
<?php
include_once("FTMdatabase.php");
if($isset($_GET['AccountID'])){
echo "No value for AccountID given";
exit;
}
$id = $_GET['AccountID'];
$sql = "DELETE FROM personaldetails WHERE AccountID = '$id'";
$result = mysql_query($sql);
if($result){
$msg = "Data Has Been Removed";
confirm($msg);
} else {
echo "error on query";
}
mysql_close();
function confirm($msg)
{
echo "<script language=\"javascript\">
alert(\"".$msg."\");
document.location.href='FTMlogin.php';
</script>";
}
?>