Jump to content

PHP MySQL Update Inconsistent


FullMetalAlc

Recommended Posts

$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 by FullMetalAlc
Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

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] 
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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));
    }
Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

 

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.  

Link to comment
Share on other sites

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.