include_once 'dbfunction.php';
getDbConnect();
mysqli_query("DELETE FROM crewlist WHERE id = $_GET[crew_id]") or die (mysqli_error());
echo 'Delete success';
header ('Location: crewlisting.php');
此代码不起作用,但是当我通过硬编码将crew_id替换为实际主键时,删除功能正常工作
答案 0 :(得分:0)
使用此(MySQLi Procedural
)
dbfunction.php
应该是
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
?>
并且插入页面应为
<?
include ("dbfunction.php"); //include db connection
$id = $_REQUEST['crew_id'];
$sql = "DELETE FROM crewlist WHERE id = '$id' ";
if (mysqli_query($conn, $sql))
{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
错误是
getDbConnect()
如果您对'
和"
感到困惑,请将功能拆分
$id = $_REQUEST['crew_id'];
$sql = "DELETE FROM crewlist WHERE id = '$id' ";
以正确的格式使用mysqli_query
和mysqli_error
mysqli_query
中的错误,您没有将连接传递给MySQLi
mysqli_close($conn);
答案 1 :(得分:-1)
更正您的查询:
mysqli_query("DELETE FROM crewlist WHERE id ='".$_GET['crew_id']."'") or die('Error: ' . mysqli_error());