dsue14118 2014-09-11 15:03
浏览 28
已采纳

使用php删除mysqli中的学生数据

I have just migrated my page from mysql to msqli. Now, deleting data is confusing. Here is my code in Admin_Delete.php

require 'Connect.php';
$id = intval($_GET['id']);
$query = "DELETE * FROM student_information WHERE student_id='$_GET[id]'";
// not using return value, and add some debug info
mysqli_query($query) or die(mysql_error().PHP_EOL.$query);
// let's see if anything actually happened...
$rowsDeleted = mysqli_affected_rows();
if($rowsDeleted == 0) {
    error_log("Nothing deleted for this query:".PHP_EOL.$query);
}
echo "<script language='javascript' type='text/javascript'>alert('$rowsDeleted row(s) deleted!')</script>";
echo "<script language='javascript' type='text/javascript'>window.open('Admin_Home.php','_self')</script>";
?>

This is my configuration to fix these. Connect.php

<?php
$host = "localhost";
$dbusername = "root";
$dbpassword = "123456";
$dbname = "student";
$link_id = mysqli_connect($host,$dbusername,$dbpassword,$dbname) or die("Error " . mysqli_error($link_id)); 
?>
  • 写回答

1条回答 默认 最新

  • duanjin9035 2014-09-11 15:19
    关注

    A couple of problems.

    The reference to mysql_error should be mysqli_error($link_id).

    The reference to mysqli_query should be mysqli_query($link_id, $query).

    The reference to mysqli_affected_rows should be mysqli_affected_rows($link_id)


    Also, you've used intval, to get an integer value from $_GET, but you're using reference to $_GET in the SQL text. If you aren't going to use a prepared statement, then you should be using mysqli_real_escape_string function to make potentially "unsafe" values "safe" for inclusion in SQL text.

    $sql = " ... WHERE id='" . mysqli_real_escape_string($link_id, $id) . "'";
    

    Syntax for DELETE statement is not correct. Either omit the *

    DELETE FROM student_information WHERE ...
    

    Or qualify the * with a table reference, or table alias

    DELETE s.* FROM student_information s WHERE ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序