micnie2020 Posted September 25, 2017 Share Posted September 25, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/305112-why-delete-not-working-classuserphp/ Share on other sites More sharing options...
micnie2020 Posted September 25, 2017 Author Share Posted September 25, 2017 (edited) 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 September 25, 2017 by cyberRobot Please enclose code with [code][/code] tags Quote Link to comment https://forums.phpfreaks.com/topic/305112-why-delete-not-working-classuserphp/#findComment-1551905 Share on other sites More sharing options...
Solution cyberRobot Posted September 25, 2017 Solution Share Posted September 25, 2017 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305112-why-delete-not-working-classuserphp/#findComment-1551906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.