我在尝试运行mysql删除事务时遇到了致命的错误,所以我搜索了类似的问题,很明显使用mysqli_fetch_assoc是解决这个问题的方法。尝试了几次,但显然做错了什么,会感激任何帮助。
这是我的代码
<?php
$selected_admin = find_admin_by_id($_GET["admin"]);
if (!$selected_admin) {
redirect_to("sadmin.php");
}
while ($admin = mysqli_fetch_assoc($selected_admin)) {
$admin_id = $selected_admin["admin_id"];
}
$sql = "delete from admin where admin_id = {$admin_id} limit 1";
$result = mysqli_query($connection, $sql);
if ($result && mysqli_affected_rows($connection) == 1) {
$SESSION["message"] = "Admin successfully deleted.";
redirect_to("sadmin.php");
} else {
$SESSION["message"] = "Admin was not deleted successfully.";
redirect_to("sadmin.php?admin={$admin_id}");
}
?>
我正在调用的函数是
function find_admin_by_id($admin_id) {
global $connection;
$query = "select * from admin where admin_id = {$admin_id} LIMIT 1";
$current_admin = mysqli_query($connection, $query);
confirm_query($current_admin);
return $current_admin;
}
答案 0 :(得分:0)
你在while循环中使用了错误的变量,而我的另一个伙伴也在一小时前提到过,这里是正确的代码,只有很小的改动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<RelativeLayout
android:id="@+id/nav_header_container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi "
android:textStyle="bold"
android:layout_centerVertical="true"
android:textColor="#FFF"
android:paddingLeft="5dp"
android:id="@+id/hi"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:id="@+id/name"
android:textStyle="bold"
android:layout_centerVertical="true"
android:text="!"
android:layout_toRightOf="@+id/hi"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textStyle="italic"
android:textColor="#FFF"
android:id="@+id/edit"
android:paddingRight="5dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nav_header_container"
android:layout_above="@+id/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/hello"
android:text="Hello"
android:textColor="#000"
android:paddingLeft="5dp"
android:textStyle="italic"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
我改变了什么:
将$ selected_admin更改为$ admin。 并在执行DELETE STATEMENT之前添加if条件。