php删除查询不起作用

时间:2017-05-22 18:28:22

标签: php mysql

我试图从此链接开始创建删除功能

 <a href="videogamedatabaseadduserlist.php?function=delete&IndEMail='.$_POST['IndEMail'].'&IdGame='.$row['IdGame'].'">Elimina<a>'.'<BR>'

这是它应与

交互的php
if ($_GET['function']== 'delete'){
    $conn = new PDO('mysql:host=localhost;dbname=videogamelist', 'root', 'root',array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
    $sql = "delete from lists where IndEmail = :IndEmail and IdGame = :IdGame";
    $res_prepare = $conn->prepare($sql);
    $res_prepare->bindParam(':IdGame',$_GET['IdGame']);
    $res_prepare->bindParam(':IndEmail',$_GET['IndEmail']);
    $a=$res_prepare->execute();
    echo "Gioco cancellato";
}

经过多次尝试在此网站上搜索,我无法找到查询无效的原因。

1 个答案:

答案 0 :(得分:0)

我测试过它有效!如果这对您不起作用,请检查数据库或表单

    try {
        $conn = new PDO("mysql:host=localhost;dbname=videogamelist", 'root', 'root');
        $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);;


        $sql = "DELETE FROM lists WHERE IndEmail = :IndEmail AND IdGame = :IdGame";
        $res_prepare = $conn->prepare($sql);
        $res_prepare->bindParam(':IdGame', $_GET['IdGame'], PDO::PARAM_INT);
        $res_prepare->bindParam(':IndEmail',$_GET['IndEmail'], PDO::PARAM_INT);
        $res_prepare->execute();

            echo "Deleted";
    }

    catch(PDOException $e)
    {
        echo $e->getMessage();
        }

不确定$row['IdGame']的来源,所以我只是使用基本表单测试代码:

<form  name="delete" action="test.php" method="get">
 <input type="text" name="IdGame">
 <input type="text" name="IndEmail">
 <input type="submit" name="delete" value="delete" >
</form>