SQL syntax with simple WHERE - php

I'm having a problem with updating a table where the id matches the post-id. My code looks like this at the moment.
$id = $_POST['id'];
$vote =$_POST['vote'];
$sql = "UPDATE images SET votes=votes+1, value=value+$vote, WHERE 'id'='$id'";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "hej då";

Try this
$sql = "UPDATE images SET votes=votes+1, value=value+'$vote' WHERE id='$id'";

The syntax of your query appears a bit off. Try removing the single quotes around $id in the WHERE clause, and also get rid of that trailing comma after the SET list.
$sql = "UPDATE images SET votes=votes+1, value=value+$vote WHERE id=$id";

$sql = "UPDATE images SET votes=votes+1, value=value+$vote, WHERE 'id'='".$id."'";

Related

I am trying update multiple rows using values is this correct?

$query = "UPDATE INTO Sanctions SET (idNumber, lastName,firstName, section,sanction,expireDate) VALUES('$idNumber','$lastName', '$firstName','$section','$sanction', '$dueDate') WHERE id= '$id'";
Wrong
$query = "UPDATE INTO Sanctions
SET (idNumber, lastName,firstName, section,sanction,expireDate)
VALUES('$idNumber','$lastName', '$firstName','$section','$sanction', '$dueDate')
WHERE id= '$id'";
Correct way:
$query = "UPDATE Sanctions
SET idNumber = '{$idNumber}',
lastName = '{$lastName}', ....
WHERE id = '{$id}'";
The INTO command is not valid for UPDATE query. You need to assign the table equals to (=) values for every column you want to edit.
Notes:
These query are not well secured, please use prepared statement insted. :)

Can't update mysql database with text

I can update my database using the following code:
$id = 1;
$client = 3456;
$sql = "UPDATE production SET client=$client WHERE id=$id";
However, if the $client variable is a text string (instead of numeric), it won't work.
$id = 1;
$client = "some text";
$sql = "UPDATE production SET client=$client WHERE id=$id";
The client field in my database is VARCHAR with a limit of 50 characters. Is there anything obvious I'm overlooking?
Add single or double quotes at start and end of string to make is string in mysql query.
Replace
$sql = "UPDATE production SET client=$client WHERE id=$id";
With
$sql = "UPDATE production SET client='$client' WHERE id=$id";
The above can break if there is single quote in string so you can use addslashes to value.
Try
$sql = "UPDATE production SET client='".addslashes($client)."' WHERE id=$id";
Note:
There are SQL injection possibilities in above query. Please try to use prepare query to prevent SQL injections
add single quotes in query while you pass the string like this,
$sql = "UPDATE production SET client='$client' WHERE id=$id";

SQL Multiple Statements

I am using php and mysql to update rows in my DB. I have 4 update statements in a row, yet only the last one works. I have confirmed that the statements work if they are used alone, but when I have them executed one after another only the last one executed works. I am receiving no error messages. Any help? Thanks!
$sql = "UPDATE comlog SET name='$name1', message='$message1' WHERE id=1";
$sql = "UPDATE comlog SET name='$name2', message='$message2' WHERE id=2";
$sql = "UPDATE comlog SET name='$name3', message='$message3' WHERE id=3";
$sql = "UPDATE comlog SET name='$name', message='$message' WHERE id=4";
In the above code, only the row with id 4 is being updated.
The answer is simple.
You are declaring the same variable for EACH sql string.
You need to declare it something like:
$sql1 = "";
$sql2 = "";
$sql3 = "";
$sql4 = "";

Updating SQL using PHP - Error

Right so i have php code to update a SQL table. If i replace $_GET['emailID'] with a number say 1 the database IS updated. But otherwise no update. What seems to be wrong here
Table: emails
Fields: mailbox, emailID
$query = 'UPDATE `emails` SET `mailbox`=\'trash\' WHERE `emailID`='.(int)$_GET['emailID'];
Do like this
$query = "UPDATE `emails` SET `mailbox`='trash' WHERE `emailID`=".intval($_GET['emailID']);
Can you try this,
$query = 'UPDATE `emails` SET `mailbox`=\'trash\' WHERE `emailID`="'.(int)$_GET['emailID'].'" ';
Value of attribut must be selected by single quotes. Try this:
$query = "UPDATE `emails` SET `mailbox` = 'trash' WHERE `emailID` = '" . intval($_GET['emailID']) . "'";
$query = "UPDATE `emails` SET `mailbox`='trash' WHERE `emailID`= ".$_GET['emailID'];
Try this one sure it will work

MySQL Query in PHP - Not Correct?

What is wrong with this query? It appears to be correct to me:
mysql_query("UPDATE culture SET cult_desc=$culture WHERE cult_id is $UID");
Modified it, NetBeans is still giving me an error. Here's my total code for the page:
$culture = $_POST["culture"];
if (isset($_POST["id"]))
$UID = $_POST["id"];
mysql_query("UPDATE culture SET cult_desc='$culture' WHERE cult_id=$UID");
else
mysql_query("INSERT INTO culture
VALUES(cult_desc='$culture')");
what's the value of $culture?
If it's a string, you'll need to encapsulate it with quotes.
Same thing for $UID.
Also, The 'is' in the where-condition should be '='
Also: watch our with this code. Make sure that $culture and $UID can not contain any malicious values (e.g. malicious input from users)
cult_desc probably string so need to wrap with ' '
mysql_query("UPDATE culture SET cult_desc='$culture' WHERE cult_id = $UID");
Seeing the newly edited code, your update-statement is now correct, but your insert statement now is wrong.
Try:
mysql_query("INSERT INTO culture (culture_desc)
VALUES ('$culture')");
if SET cult_desc is a string then
mysql_query("UPDATE culture SET cult_desc='$culture' WHERE cult_id = $UID");
or
mysql_query("UPDATE culture SET cult_desc=$culture WHERE cult_id = $UID")
your problem in the { and } of if else statement
$culture = $_POST["culture"];
if (isset($_POST["id"])){
$UID = $_POST["id"];
mysql_query("UPDATE culture SET cult_desc='$culture' WHERE cult_id=$UID");
}else{
mysql_query("INSERT INTO culture
VALUES(cult_desc='$culture')");
}
$sql = "UPDATE 'culture' SET `cult_desc` = '$culture' WHERE `cult_id` = '$UID'";
Basically, you're using is instead of =
Depending on the data type of $culture and $UID you might be missing quotes. Cult_desc sounds like a string and thus $culture should be enclosed in quotes.
You should always check the output of mysql_error.http://php.net/manual/en/function.mysql-error.
I also usually use = instead of 'is' and also wrap all of my input data in quotation marks. eg
$sql = "UPDATE 'culture' SET cult_desc = '".$culture."' WHERE cult_id = '".$UID."'";

Categories