Jump to content

Why delete not working - CLASS.USER.PHP


micnie2020

Recommended Posts

Hi All,
 
I try to delete an item in a datagrid, but it'll remove from the datagrid and NOT in the Database. When I refresh the datagrid. The record appear back.
 
Please advise.
 
delete_course.php
 
<?php
 
 
session_start();
require_once 'class.user.php';
$user_home = new USER();
 
$id = intval($_REQUEST['course_id']);
 
 
 
$stmt = $user_home->runQuery("delete from tblcourse where course_id=$id");
$stmt->execute();
echo json_encode(array('success'=>true));
 
 
 
?>
 
 
 
 
CLASS.USER.PHP
 
<?php
 
require_once 'dbconfig.php';
 
class USER
{
 
private $conn;
 
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
    }
 
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
 
:
:
 
 
Thank you.
 
Regards,
Michelle

 

Link to comment
Share on other sites

dbconfig.php

 

<?php
class Database
{
     
    private $host = "localhost";
    private $db_name = "xxx";
    private $username = "root";
    private $password = "";
    public $conn;
     
    public function dbConnection()
    {
     
        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }
         
        return $this->conn;
    }
}
?>
Link to comment
Share on other sites

Have you checked the return value of $stmt->execute();? It should return true if the prepared query executes successfully.

 

If it's returning false, PDO and MySQLi have methods for seeing the errors that are being thrown by MySQL.

 

Side note: you'll want to look into binding variables into prepared queries. Using the raw value from variables, like $id, makes the query susceptible to SQL Injection Attacks.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.