FullMetalAlc Posted August 9, 2013 Share Posted August 9, 2013 (edited) $Database = new $DatabaseClass; $Database->Connect($_POST["host"], $_POST["username"], $_POST["password"], $_POST["database"]); $Database->Update($URL . "messages", "`status`='1'", "`index`='$Index'"); //status and index is INT and everything else is TEXT $Database->Disconnect(); if ($Database->Connect($_POST["host"], $_POST["username"], $_POST["password"], $_POST["database"]) == true) $Access = "Allowed"; $Database->Update($_POST["table"], "lastlogin='" . date("n/j/Y") . "'", "username='" . $_POST["username"] . "'"); //Everything is TEXT $Database->Disconnect(); Both of these code snippets work but I want to know why when I use the update query on the second piece of code I don't need single quotations around anything but I must have them around the first piece of code for it to work at all. Also when I try to use the first piece of code again to change status to a different value with the exact same code it does not work. // This is met when the file redirects to itself via a from submit and passes the following values via POST. This does not work. else if ($_POST["action"] == "delete") { $Database->Connect($_POST["host"], $_POST["username"], $_POST["password"], $_POST["database"]); $Database->Update($URL . "messages", "`status`='2'", "`index`='" . $_POST["deleteindex"] . "'"); //Does not work. $Database->Disconnect(); } I made sure I was able to connect to my SQL database with each snippet of code but I am not able to call the update query consistently. I am also sure that the values I passed in were corrected when I echoed them out. Edited August 9, 2013 by FullMetalAlc Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 9, 2013 Share Posted August 9, 2013 Without seeing the Update() method, we can't really tell what is happening. However, you are using single-quotes (') in the second code snippet. Were you referring to the back-ticks (`)? They are completely different puncuation and have completely different purposes. Also, what exactly does "it does not work" mean? You pobably need to output the error from the database client library. What database library are you using (mySql, mySqli, pdo, odbc)? Quote Link to comment Share on other sites More sharing options...
FullMetalAlc Posted August 10, 2013 Author Share Posted August 10, 2013 class DatabaseClass { private $Connection = null; public function __construct() { $this->Connection = null; } public function Connect($in_Host, $in_Username, $in_Password, $in_Database) { $this->Connection = @mysqli_connect($in_Host, $in_Username, $in_Password, $in_Database); if (@mysqli_connect_errno($this->Connection)) return false; return true; } public function Delete($in_Table, $in_Condition) { $SQL = "DELETE FROM $in_Table WHERE $in_Condition"; @mysqli_query($this->Connection, $SQL); } public function Disconnect() { @mysqli_close($this->Connection); } } Ooops I forgot to post the Update() code. The table I am reading from looks like this Field Type Collation Attributes Null Default Extra index int(11) No sender text latin1_swedish_ci No subject text latin1_swedish_ci No date text latin1_swedish_ci No message text latin1_swedish_ci No status int(11) No I get no warnings or errors when I use the code above in the first post and I am able to connect to my database but I am unable to update anything with the update query. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 10, 2013 Share Posted August 10, 2013 At the risk of repeating myself: I still do not see the Update() method. Forgot about the "@" operator. It hides errors it does not fix them. Use proper error reporting. Also, when a mysqli* method fails, you need to display the mysqli_error so you can see what the problem is. You did not answer my question: The second code snippet DOES HAVE single quotes in it - your post said it did not - so what are you talking about? Quote Link to comment Share on other sites More sharing options...
FullMetalAlc Posted August 10, 2013 Author Share Posted August 10, 2013 Sorry I'm not use to Notepad++'s code hiding thing and copied the wrong section. public function Update($in_Table, $in_Columns, $in_Condition) { $SQL = "UPDATE $in_Table SET $in_Columns WHERE $in_Condition"; mysqli_query($this->Connection, $SQL); } Even after removing the @ modifier from the functions I still get no output warnings or errors. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 10, 2013 Share Posted August 10, 2013 The UPDATE SQL Statement is used to modify the existing rows (columns) in a table. I only see the column name without setting a column value in the statement above. So, the right syntax for this sql command should be something like: UPDATE table_name SET column_name_1 = column_value1, column_name_2 = column_value2 [WHERE some condition] Quote Link to comment Share on other sites More sharing options...
mikosiko Posted August 11, 2013 Share Posted August 11, 2013 Sorry I'm not use to Notepad++'s code hiding thing and copied the wrong section. public function Update($in_Table, $in_Columns, $in_Condition) { $SQL = "UPDATE $in_Table SET $in_Columns WHERE $in_Condition"; mysqli_query($this->Connection, $SQL); } Even after removing the @ modifier from the functions I still get no output warnings or errors. in the line 43 of your first post where are you passing the parameter $ in_Condition to your update method?.. i don't see that your method manage in any way the missing parameter Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 11, 2013 Share Posted August 11, 2013 Sorry I'm not use to Notepad++'s code hiding thing and copied the wrong section. public function Update($in_Table, $in_Columns, $in_Condition) { $SQL = "UPDATE $in_Table SET $in_Columns WHERE $in_Condition"; mysqli_query($this->Connection, $SQL); } Even after removing the @ modifier from the functions I still get no output warnings or errors. You won't get a PHP message if the query is bad. But you are not testing to see if the query succeeded: if (! mysqli_query($this->Connection, $SQL)) { printf("Error: %s<BR>%s<BR>", $SQL, mysqli_error($this->Connection)); } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 11, 2013 Share Posted August 11, 2013 (edited) the query you are producing is equivalent to - $SQL = "UPDATE {$URL}messages SET `status`='2' WHERE `index`='{$_POST['deleteindex']}'"; assuming the $URL value is the same as in your first working example, so that something about what is in $URL isn't breaking the sql syntax, there's nothing wrong with that query and if it isn't updating anything, it's most likely that the $_POST['deleteindex'] value either doesn't match any index value or that it contains some leading character(s) that don't evaluate to a number that matches any index value. after you add the error checking logic that DavidAM has suggested, if it doesn't expose an sql error, what does using var_dump($_POST['deleteindex']); show? Edited August 11, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 11, 2013 Share Posted August 11, 2013 The UPDATE SQL Statement is used to modify the existing rows (columns) in a table. I only see the column name without setting a column value in the statement above. So, the right syntax for this sql command should be something like: UPDATE table_name SET column_name_1 = column_value1, column_name_2 = column_value2 [WHERE some condition] Wow......completely misread the line 43 of your first post last night. For sure, you should add the error checking logic that DavidAM has suggested and mac_gyver said above. Sorry about that. Quote Link to comment 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.