[mysql]SQL 删除语句不工作

标签: MySQL PHP
发布时间: 2017/1/9 22:13:19
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
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 替换的实际的主键,通过硬编码删除功能起作用

解决方法 1:

使用此 ( 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. 如果你是混淆和 '" 然后拆分功能

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

  4. 和中的错误 mysqli_query ,您不需要传递到连接MySQLi
  5. 当过数据库部分完毕后,关闭连接mysqli_close($conn);
官方微信
官方QQ群
31647020