MYSQL DELETE多行不起作用

时间:2017-08-25 09:02:09

标签: php jquery mysql

这是delete.php

<?php 
if(isset($_GET['id']) && !empty($_GET['id']))
{
    $id = $_GET['id'];
    $servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "DELETE FROM table WHERE memberid IN ('".$id."' )";


if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully" ;
} else {
    echo "Error updating record: " . $conn->error;
}


$conn->close();


}
?>

这是home.php

    $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "select memberid from table group by memberid having count(*) > 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Member ID: " . $row["memberid"]. " | <a href='javascript:void(0);' onClick='deleteId(".$row["memberid"].")'>reset</a><br>";
    }
} else {
    echo "0 results";
}
$conn->close();



<script>
function deleteId(id)
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
            alert(xmlhttp.responseText);
        }
    };
    xmlhttp.open("GET", "delete.php?id=" +id, true);
    xmlhttp.send();
    //window.location.reload();

}

</script>

我试图通过onclick从home.php删除表中的多行,但遗憾的是它根本不起作用。

在表格行中记录了许多相同的memberid,所以我试图通过memberid调用并在1click中删除所有这些

我是php和mysql的新手。

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

$sql = "DELETE FROM table WHERE c ='$id'";

答案 1 :(得分:0)

试试这个:

$idArr = explode(",", $id);
$idStr = implode("','", $idArr);
$sql = "DELETE FROM table WHERE c IN ('$idStr' )";

另外,在您的客户端代码中,

确保 deleteId()中的 id 警告012345而不是12345.否则,请在调用deleteId()之前将整数转换为字符串。

答案 2 :(得分:0)

您正在发送一个ID,因此不需要使用IN(),普通id = ?将删除具有该ID的所有行。但是你需要使用绑定参数来阻止某人发送类似1 or 1=1的内容来删除表中的所有行,即使从你的javascript发送它也不信任输入