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

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么