普通网友 2015-05-21 18:16
浏览 54
已采纳

从phpmyadmin数据库中删除数据

<?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']

  • 写回答

1条回答 默认 最新

  • douya7121 2015-05-21 18:34
    关注

    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>";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历