iseriouslyneedhelp Posted July 12, 2012 Share Posted July 12, 2012 Hi, I have a delete function and it's not deleting or going to my header location, any idea what I'm not doing right? <?php require("../config.php"); // DELETE ROW function del ($conn) { if(isset($_GET['ID']) && $_GET['ID'] == "$ID") $stmt->query("DELETE FROM contacts WHERE ID = '$ID'"); header('Location: ' . $_SERVER['HTTP_REFERER']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/ Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 For starters, use brackets for conditionals. if(isset($_GET['ID']) && $_GET['ID'] == "$ID") { $stmt->query("DELETE FROM contacts WHERE ID = '$ID'"); header('Location: ' . $_SERVER['HTTP_REFERER']); } Where are you defining $ID? The function parameter is $conn, but you are using $stmt. Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360958 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 12, 2012 Author Share Posted July 12, 2012 For starters, use brackets for conditionals. if(isset($_GET['ID']) && $_GET['ID'] == "$ID") { $stmt->query("DELETE FROM contacts WHERE ID = '$ID'"); header('Location: ' . $_SERVER['HTTP_REFERER']); } Where are you defining $ID? The function parameter is $conn, but you are using $stmt. If I change it to the following, I'm defining ID in the function, is that right? And your other question... The function parameter is $conn, but you are using $stmt.. I changed it to $mysql, does that make a difference? I'm not sure what you mean, bare with me I'm learning! With my select functions, I did function name() { //somthing... } I'm very confused and trying to understand..I had this working until I put my db connection in a class yesterday...now everything is bonkers. <?php require("../style/inc/config.php"); // DELETE ROW function del ($conn) { $ID = $_GET['ID']; if(isset($_GET['ID']) && $_GET['ID'] == "$ID") { $mysqli->query("DELETE FROM contacts WHERE ID = '$ID'"); header('Location: ' . $_SERVER['HTTP_REFERER']); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360961 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 If I change it to the following, I'm defining ID in the function, is that right? No, now you are attempting to use $_GET['ID'] before the if conditional which checks to see if it is available. You need to put the assignment inside the if conditional. if(isset($_GET['ID']) && $_GET['ID'] == $ID) { $ID = $_GET['ID']; I changed it to $mysql, does that make a difference? No, it needs to be $conn - because that is what you used for the function parameter. I'm very confused and trying to understand..I had this working until I put my db connection in a class yesterday Can we see that? Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360965 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 12, 2012 Author Share Posted July 12, 2012 DB Conn <?php class DatabaseConn { public function __construct($host, $username, $pword, $db) { if (!@$this->Connect($host, $username, $pword, $db)) { echo 'Connection failed.'; } } public function Connect($host, $username, $pword, $db) { if(!mysqli_connect($host, $username, $pword, $db)) { return false; } else { return true; } } } $conn = new mysqli('localhose', 'user', 'pass', 'db') ?> Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360966 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 That class is doing absolutely nothing, and you're not even instantiating it. Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360967 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 12, 2012 Author Share Posted July 12, 2012 That class is doing absolutely nothing, and you're not even instantiating it. Wow...I'm really messed up...where can you point me or what can you show me? So I have a config file with my db connection and it's in a class called DatabaseConn. I've add this to my delete.php page by " require("/config.php");" In my delete.php file, I would also need put $db = new DatabaseConn, so my delete.php starts like this, right? require("/config.php");" $db = new DatabaseConn(); This is what you mean, right? Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360970 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 Honestly, I think you are in need of a good book or two. You seem to be missing some of the basic fundamentals of (PHP) programming. Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360971 Share on other sites More sharing options...
redarrow Posted July 12, 2012 Share Posted July 12, 2012 http://www.tipsntutorials.com/tutorials/PHP/76/ Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360972 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 http://www.tipsntutorials.com/tutorials/PHP/76/ Sorry, but that code looks like it was written 10 years ago. Please, do not use that. Everything in that wrapper is already natively supported by the mysqli driver. Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360976 Share on other sites More sharing options...
redarrow Posted July 12, 2012 Share Posted July 12, 2012 simple one for you then? <?php class Database{ private $host; private $user; private $pwd; private $rows; private $error; private $result; private $dbName; private $connection; private $isReady; public function __construct(){ $this->result = null; $this->isReady = false; $this->error = array(); } public function __destruct(){ @mysql_close($this->connection); } /* setters */ public function setHost($host){ $this->host = $host; } public function setUser($user){ $this->user = $user; } public function setPassword($pwd){ $this->pwd = $pwd; } public function setDbName($dbName){ $this->dbName = $dbName; } /* other interfaces */ public function init($host=null,$user=null,$pwd=null,$dbName=null){ if(!isset($host,$user,$pwd,$dbName)) die("Please provide require settings."); $this->setHost($host); $this->setUser($user); $this->setPassword($pwd); $this->setDbName($dbName); $this->isReady = true; } public function select($dbName){ $this->setDbName($dbName); mysql_select_db($this->dbName,$this->connection) or die("The said database does not exist."); } public function query($sql){ $this->result = mysql_query($sql,$this->connection) or die("Invalid query string!"); } public function connect(){ if(!$this->isReady) die("not ready to connect"); $this->connection = mysql_connect($this->host,$this->user,$this->pwd) or die("Could not connect to database. please check your credentials."); $this->select($this->dbName); $this->query("SET NAMES 'utf8'",$this->connection); //persian support } public function isConnected(){ if($this->connection) return true; return false; } public function disconnect(){ mysql_close($this->connection); $this->connection = null; } public function countRows($selectMode = true){ if($selectMode) return mysql_num_rows($this->result); return mysql_affected_rows($this->connection); } public function loadRows(){ if(!$this->result) die("Nothing found!"); $this->rows = array(); while($r = mysql_fetch_array($this->result,MYSQL_BOTH)) $this->rows[] = $r; mysql_free_result($this->result); return $this->rows; } public function siftDown($dataStack){ if(!is_array($dataStack)){ $dataStack = ereg_replace("[\'\")(;|`,<>]","",$dataStack); $dataStack = mysql_real_escape_string(trim($dataStack),$this->connection); $dataStack = stripslashes($dataStack); return $dataStack; } $safeData = array(); foreach($dataStack as $p=>$data){ $data = ereg_replace("[\'\")(;|`,<>]","",$data); $data = mysql_real_escape_string(trim($data),$this->connection); $data = stripslashes($data); $safeData[$p] = $data; } return $safeData; } public function secure($data){ return sha1(md5(sha1(md5(sha1($data))))); } }//Database class ?> <?php //usage require_once 'path/to/Database.class.php'; $db = new Database(); //Creating new object $db->init("localhost","test_root","test_pwd!","test_db"); //initializing by credentials. $db->connect(); //unicode support $test_value = $db->siftDown($test_value); //preventing harmful inputs $something_testy_else = $db->siftDown($something_testy_else); $db->query("SELECT * FROM test_table WHERE test_field = '$test_value' AND second_test_field = '$something_testy_else' LIMIT 1"); if($db->countRows()==1) $dbdata = $db->loadRows(); //returns a numeric/associative array as the result (MYSQL_BOTH) //TODO: To Process $dbdata $db->disconnect(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360977 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 It still doesn't add any useful functionality, and it is still using old code (namely ereg_* and mysql_*). Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360978 Share on other sites More sharing options...
iseriouslyneedhelp Posted July 12, 2012 Author Share Posted July 12, 2012 besides that fact that my class doesn't do anything and it's not instantiated...how do I get this code to delete my user? function mymy ($conn) { if(isset($_GET['ID']) && $_GET['ID'] == "$ID") $ID = $_GET['ID']; $conn->query("DELETE FROM contacts WHERE ID = '$ID'"); header('Location: ' . $_SERVER['HTTP_REFERER']); } Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360979 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 Well, where/how are you calling it? You will need to pass in the mysqli function. Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360984 Share on other sites More sharing options...
xyph Posted July 12, 2012 Share Posted July 12, 2012 how do I get this code to delete my user? For future reference, this question is WAY too vague. I can answer it correctly and accurate by simply responding 'by writing the code that executes a query to delete the user.' Quote Link to comment https://forums.phpfreaks.com/topic/265548-delete-stmt-not-deleting-or-going-to-header-location/#findComment-1360988 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.