2015-02-24 73 views
-3

我有一个源代码PHP PDO的问题。我尝试使用PHP PDO做一个CRUD,但是当我尝试使用源代码进行删除,并且在运行脚本时,数据不想被删除。我很抱歉如果我的发言没有错,我来自印度尼西亚CRUD - PHP PDO错误

感谢的

之前

脚本函数删除:

public function deleteData($id) 
    { 
     // fungsi menghapus data dari database 
    $this->id = $id; 
    $this->sql = "DELETE FROM student WHERE id=:id"; 
    $this->q = $this->dbh->prepare($sql); 
    $this->q->bindParam(":id",$this->id); 
    $this->q->execute(); 
    return true; 
    } 

脚本delete.php

<?php 
    include "belajar_crud.php"; 
    $obj = new crud; 
    $id = isset($_GET['id']) ? $_GET['id']:''; 
    $obj->deleteData($id); 
?> 
+0

什么是确切的异常行为?您需要显示导致此意外行为的代码? – 2015-02-24 15:08:43

+0

你有没有得到任何错误?代码本身看起来不错,但表名或字段名称可能不正确,ID可能包含无效字符,或者缺少正确的凭据来删除行。 – GolezTrol 2015-02-24 15:22:03

+0

此代码delete.php: https://gist.github.com/RiskyFeryansyah/0f0d968c7d0bd82c5bc6 这个代码功能删除PHP: https://gist.github.com/RiskyFeryansyah/b6951baa0ed4af7a9a70 – 2015-02-24 15:25:57

回答

0

改变这一行:

$this->q = $this->dbh->prepare($sql); 

进入这个:

$this->q = $this->dbh->prepare($this->sql); 

这是我在代码中看到的唯一错误... 如果不是这种情况,就需要提供更多的信息。

+0

感谢您的重播,我已经取代了它,但是当我运行数据时仍然不想被删除 – 2015-02-24 15:39:56

0

为什么你不试试?

的config.php:

define('HOST','your host'); 
define('BD','your database'); 
define('USER','your user'); 
define('PASS','your password'); 

BD.class.php:

class BD{ 
    private static $conn; 
    public function __construct(){} 
    public function conn(){ 
      if(is_null(self::$conn)){ 
       self::$conn = new PDO('mysql:host='.HOST.';dbname='.BD.'',''.USER.'',''.PASS.''); 
      } 
      return self::$conn; 
    } 
} 

您的删除代码:

@BD::conn(); 
$query = @BD::conn()->prepare("DELETE FROM YOURTABLE WHERE ID = ?"); 
if($query->execute(array($this->id))){ 
    echo "<script>alert('Delete Successful');</script>"; 
}