dpql57753
2016-01-30 17:06
浏览 77
已采纳

使用PDO删除mySQL数据库中表的记录[关闭]

I have been trying to delete a row from my data base but it keep showing me deleted without deleting anything please can someone figure out what the problem is with my codes

I have tried using PDO and mysql but is not deleting but will show deleted I don't know what to do again.

MYSQL

    if($user == $me){
        $sql_delete = "DELETE FROM replys WHERE id = '$id' AND rusername = '$me'";
        echo $sql_delete;
        mysql_query($sql_delete) or die(mysql_error());
        echo "Deleted";
    }
    else{
    }
}
?>

PDO

<?php
session_start();  
if(isset($_POST['DeleteThis'])){

    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "kindom";
    $db_name = "posters";

    try {
        $user = $_SESSION['username'];
        $form = $_POST;
        $id = $form['id']);
        $me = $form['me'];
        if($me == $user){
            $db_conn = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
            $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $stmt = $db_conn->prepare("DELETE FROM replys WHERE id = :id AND rusername = :user");

//I have tried using this SQL statements  
//$query = "DELETE FROM replys WHERE id = {$id} AND rusername = {$user}"; 
//$query = "DELETE * FROM replys WHERE id = :id AND rusername = :rusername"; 
            $query = "DELETE FROM replys WHERE id = :id AND rusername = :rusername"; 

            $stmt->bindParam(':id', $id); 
            $stmt->bindParam(':rusername', $user);

            $stmt->execute();
            echo "Deleted";
        }
        else{
            //Do nothing
        }
    }
    catch(PDOException $e)
    {
        echo "Error:" . $e->getMessage();
    }
    $db_conn = null;
}
?> 

HTML

<a href="javascript:void(0);" onclick="document.getElementById('deleterp').submit();"><i style="font-size:17px;color:#F00;" class="fa fa-trash"></i></a>
<form id="deleterp" action="" method="POST">
<input type="hidden" name="id" value="<?php echo $rpId;?>"/>
<input type="hidden" name="DeleteThis" value="1"/>
<input type="hidden" name="me" value="<?php echo $rplyuser;?>"/>
</form></div> 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqwh26750 2016-01-30 17:21
    已采纳

    PDO

    Change

    $stmt = $db_conn->prepare("DELETE FROM replys WHERE id = :id AND rusername = :rusername");
    

    to

    $stmt = $db_conn->prepare("DELETE FROM replys WHERE id = :id AND rusername = :user");
    

    Like ..

    <?php
    .
    .
    $stmt = $db_conn->prepare("DELETE FROM replys WHERE id = :id AND rusername = :rusername");
    $stmt->bindParam(':id', $id); 
    $stmt->bindParam(':rusername', $user);
    .
    .
    ?> 
    

    Change

    $id = $form['id']);
    

    to

    $id = $form['id'];
    

    for not getting

    Parse error: syntax error, unexpected ')' in /home/umu35534/public_html/questions.php on line 70

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题