SQL Delete语句不起作用

时间:2015-09-21 09:10:38

标签: php mysql

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替换为实际主键时,删除功能正常工作

2 个答案:

答案 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);

?>

错误是

  1. getDbConnect()
  2. 中没有定义任何功能
  3. 如果您对'"感到困惑,请将功能拆分

    $id = $_REQUEST['crew_id'];
    $sql = "DELETE FROM crewlist WHERE id = '$id' ";
    
  4. 以正确的格式使用mysqli_querymysqli_error

  5. mysqli_query中的错误,您没有将连接传递给MySQLi
  6. 当数据库部件完成时,关闭连接mysqli_close($conn);

答案 1 :(得分:-1)

更正您的查询:

mysqli_query("DELETE FROM crewlist WHERE id ='".$_GET['crew_id']."'") or die('Error: ' . mysqli_error());