MrAshTonka Posted January 22, 2015 Posted January 22, 2015 Hello, i have a OOP login and register script and im building an admin panel for it and im trying to delete users but getting the ID from the Database and deleting them by the ID but im having some problems. here is the code im using. The problem is in the Index.php file with deleting people from the list then refreshing the list. Any help would be amazing. Index.php <?php require_once 'core/init.php'; $title = 'Shop'; $user = new User(); $select = DB::getInstance()->query("SELECT * FROM users"); $delete = DB::getInstance(); $data = $user->data(); $x=0; include_once 'temp/head.php'; if($user->isLoggedin()) { if(Session::exists('home')) { echo '<section class="success success"><p>' . Session::flash('home') . '</p></section>'; } if(isset($_POST['Delete'])) { $delete->delete('users', array('id', '=', $select->results()[$x])); } ?> <p>Hello, <?php echo escape($data->First_Name); ?>!</p> <!-- Users Name --> <p> <a href="addItem.php">Add Item</a> / <a href="logout.php">Log out</a> </p> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td><b>Image</b></td> <td><b>Unit Number</b></td> <td><b>QTY</b></td> <td><b>Part Number</b></td> <td><b>Descriptioin</b></td> <td><b>Net Price</b></td> <td><b>Delete</b></td> </tr> <?php foreach($select->results() as $select) { echo " <tr> <td>$select->First_Name</td> <td>$select->Last_Name</td> <td>$select->Email</td> <td>$select->Password</td> <td>$select->salt</td> <td>$select->joined</td> <td> <input type='submit' value='Delete' name='Delete'/> <input type='hidden' value='$select->id'> <input type='hidden' value='$x'> </td> </tr> "; $x++; } ?> <!-- Table list of items go here --> </table> </form> <?php }else{ ?> <table> <!-- Width: 100% --> <tr> <td><bold>Image</bold></td> <!-- Width: 10% --> <td><bold>Unit Number</bold></td> <!-- Width: 10% --> <td><bold>QTY</bold></td> <!-- Width: 10% --> <td><bold>Part Number</bold></td> <!-- Width: 10% --> <td><bold>Descriptioin</bold></td> <!-- Width: 50% --> <td><bold>Net Price</bold></td> <!-- Width: 10% --> </tr> <?php foreach($select->results() as $select) { echo " <tr> <td>$select->First_Name</td> <td>$select->Last_Name</td> <td>$select->Email</td> </tr> "; } ?> <!-- Table list of items go here --> </table> <?php } include_once 'temp/footer.php'; ?> This is the DB Class file. <?php class DB { private static $_instance = null; private $_pdo, $_query, $_error = false, $_results, $_count = 0; private function __construct() { try { $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/user'), Config::get('mysql/pass')); } catch(PDOException $e) { die($e->getMessage()); } } public static function getInstance() { if(!isset(self::$_instance)) { self::$_instance = new DB(); } return self::$_instance; } public function query($sql, $params = array()) { $this->_error = false; if($this->_query = $this->_pdo->prepare($sql)) { $x = 1; if(count($params)) { foreach($params as $param) { $this->_query->bindValue($x, $param); $x++; } } if($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); }else{ $this->_error = true; } } return $this; } public function action($action, $table, $where = array()) { if(count($where) === 3) { $operators = array('=', '>', '<', '>=', '<='); $field = $where[0]; $operator = $where[1]; $value = $where[2]; if(in_array($operator, $operators)) { $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; if(!$this->query($sql, array($value))->error()) { return $this; } } } return false; } public function get($table, $where) { return $this->action('SELECT *', $table, $where); } public function delete($table, $where) { return $this->action('DELETE', $table, $where); } public function insert($table, $fields = array()) { $keys = array_keys($fields); $values = ''; $x = 1; foreach($fields as $field) { $values .= '?'; if($x < count($fields)) { $values .= ', '; } $x++; } $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})"; if(!$this->query($sql, $fields)->error()) { return true; } return false; } public function update($table, $id, $fields) { $set = ''; $x = 1; foreach($fields as $name => $values) { $set .="{$name} = ?"; if($x < count($fields)) { $set .= ', '; } $x++; } $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}"; if(!$this->query($sql, $fields)->error()) { return true; } return false; } public function results() { return $this->_results; } public function first() { return $this->results()[0]; } public function error() { return $this->_error; } public function count() { return $this->_count; } } Quote
Sim Posted January 22, 2015 Posted January 22, 2015 I think it may be this line: Session::flash('home')... Quote
MrAshTonka Posted January 22, 2015 Author Posted January 22, 2015 I think it may be this line: Session::flash('home')... Why would it be a line that has nothing to do with that im having trouble with ? Quote
CtrlFreq Posted January 23, 2015 Posted January 23, 2015 Here are the issues that would keep this from working, or at least working as you'd expect it to: 1) Your form doesn't actually send anything useful to the server. Remove the three inputs in the final cell and replace with the following: <button type='submit' name='Delete' value='$select->id'>Delete</button> 2) There's no need for the $x variable - get rid of any references to it. 3) Move the check for deleting records before both the check for Session::exists('home') and the inclusion of the header. You want to finish any database interactions before writing anything to the client. 4) Use the value sent in $_POST['Delete'] to remove the record id you sent, and then immediately reload the page to prevent duplicate posts on refresh. $delete->delete('users', 'id='.mysql_real_escape_string($_POST['Delete'])); header('location: this_page.php'); Quote
MrAshTonka Posted January 28, 2015 Author Posted January 28, 2015 Thank you so much CtrlFreq its working great now :) Quote
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.