Jump to content

Why delete not working - CLASS.USER.PHP


Go to solution Solved by cyberRobot,

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
https://forums.phpfreaks.com/topic/305112-why-delete-not-working-classuserphp/
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;
    }
}
?>
Edited by cyberRobot
Please enclose code with [code][/code] tags
  • Solution

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.