简体   繁体   中英

Delete option is not working with PDO ,php

unable to delete i row using PDO in php. Here is my code given below

this is inc.common.php

<?php 
    @session_start();
    include_once '../common/inc.config.php';
    include_once '../common/inc.globalConstants.php';
    $db = new PDO("mysql:host=$mdbhost;dbname=$mdbname",$mdbuser,$mdbpass );
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    include_once '../classes/cls.common.php';
    $Cobj=new common($db);
?>

this my cls.class.php

 <?php
class common {
    public function common($dbcon) {
        $this->dbCon = $dbcon;
    }
    public function getCustomData($tableName,$fields, $conditions = "") {
        $stmt = "";
        $sql = "";
        $sql = "SELECT $fields FROM $tableName  $conditions ";
        $stmt = $this->dbCon->query($sql);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
      }
    public function delet($tableName, $class_id){
        $inputArray['classId']=$class_id;
        $count = $this->$dbCon->prepare("DELETE FROM $tableName WHERE classId =:classId");
        $result = $stmt->execute($inputArray);
        return $result;
     }
 ?>

this is my addinfo.php

<?php 
    include '../common/inc.common.php';
    $class_id=$_POST['refid'];
    if(isset($_POST['mode']))
    {
        $tableName="class";
        $class_id=$_POST['refid'];
        $res=$Cobj->delet($tableName, $class_id);
    }
?>

on passing variables using AJAX call i couldn't able to delete the row .Ajax call is success . only problem with PDO delete.

$.ajax({
        url: "../masters/addinfo.php", 
        type: "POST",        
        data:"refid="+class_id+"&mode=delete",
});   

my class table has 4 fields classId , name , date , stat .

function delet()$stmt变量未定义。

public function delet($tableName, $class_id){
 $inputArray['classId']=$class_id;
 $stmt = $this->$dbCon->prepare("DELETE FROM $tableName WHERE classId =:classId");
 $result = $stmt->execute($inputArray);
 return $result;
}

Will fix this. Note what changed; $count changed to $stmt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM