Update column if received x otherwise update y - php

If I have a table with 3 columns: id,column1,column2. If i want to update column1 just when receiving "column1" parameter in URL request otherwise update column2 when receiving "column2" parameter in URL adress. Is that possible? I made that but i think that's not correct:
$sql= "UPDATE people SET
answer_yes= '$answer_yes'+1,
answer_no='$answer_no'+1";
Thank you for helping.
EDIT: Now that is working (based on Richard Vivian answer)
If($answer_yes==1)
{
$sql= "UPDATE people SET answer_yes= answer_yes +1"or die(mysql_error());
mysql_query($sql);
}
else if ($answer_no==0)
{
$sql= "UPDATE people SET answer_no= answer_no+1" or die(mysql_error());
mysql_query($sql);
}

You can create 2 SQL statement options:
If($answer_yes)
{
$sql= "UPDATE people SET answer_yes= '$answer_yes'+1"
}
else
{
$sql= "UPDATE people SET answer_no= '$answer_no'+1"
}

I'm unsure which database model you are using, but the logic would be to pass a NULL value if you don't have a value to pass, and check that the values not null before updating.
SQL Server
UPDATE Table
SET Column1=ISNULL(#Column1,Column1),
Column2=ISNULL(#Column2,Column2)
MySQL
UPDATE Table
SET Column1=IFNULL($Column1,Column1),
Column2=IFNULL($Column2,Column2)
What is happening here is that ISNULL/IFNULL is checking whether the first value passed to it is NULL, and if it is, its returning the 2nd value. The 2nd value is the same value as the current value, and therefore it updates it with the same value (ie. Not changing the value).

You can do this:
UPDATE people
SET answer_yes = COALESCE($answer_yes + 1, answer_yes),
answer_no = COALESCE($answer_no + 1, answer_no);
The COALESCE returns the first non NULLable value in the values passed to it. If any of the parameters $answer_yes or $answer_no were passed with a NULL value, then $answer_yes + 1 and $answer_no + 1 will be evaluated to NULL also, there for the COALESCE will return the column value, and in this case the column will be updated with its value, i.e, it won't changed.

Related

PHP PDO - update same value returns 0 in MYSQL

I want to update the same value in DB,
But the database returns 0 when I do that.
is it possible to return 1 or true? or force the DB update?
$sth =$pdo->prepeare("UPDATE table set x = :x where id=:id")
$sth->execute()
$result = $sth->rowCount() // that return 0;
x is the same value in DB and the result is 0 but i want to let the user to approve the form even with the same value
Updating with same values will always return false. You need to add some some distinct value that could be updated .
For example you can have field that stores timestamp (like updatedOn) so every time you pass same values for rest of field, it will be accepted as time stamp field will have unique value.
basically you will need to add a WHERE clause as you said you need to update some values
$sth =$pdo->prepeare("UPDATE table set x = :x where y = :y")

MYSQL - If first field is not null, then update second field, if second field is not null, then etc

I have a mysql question here. What I'm trying to do is create a button ('Add to Wishlist') that, when pressed, executes a MYSQL update query that enters a number, say 6, into multiple fields, however I'm trying to get is so that it only enters in one field that is not null. So in essence, the update query will look to see if field one (saved_courses) is empty, and if it is then insert value, but if it isn't then insert into the second field (saved_courses2).
I've looked into this and as a result of my research I have this:
mysqli_query($con, "UPDATE user_accounts SET saved_courses3 = case when saved_courses3 is null then saved_courses3 = $urlid else saved_courses4 = $urlid end WHERE id = 1; ") or die(mysql_error());
Try something like this:
UPDATE user_accounts SET
saved_courses4 = case when saved_courses3 is null then saved_courses4 else $urlid end,
saved_courses3 = case when saved_courses3 is null then $urlid else saved_courses3 end
WHERE id = 1
You can see it in action at:
http://sqlfiddle.com/#!2/c5553/1

How to update a row depending on a condition

I try to update a row in my database. i receive no error but the update does not happend
$sql = "UPDATE calendar_events
SET event_title='".$_POST["naam"]."'
WHERE id ='($event_id)'";
problem is by variable $event_id. If i place a nummber in place of $event_id ( WHERE id =6)
then it is ok and i have my update but any he do not accept any variable
I did change a lot of things. The type of variabls id and $event_id are the same. De value of the id and $event_id are de same . i did try with $event_id, and '".$event_id."'. every thing is ok
and no erroror from database bat the update do not happen.
can someone help me
thanks
Try with
$sql = "UPDATE calendar_events
SET event_title='".$_POST["naam"]."'
WHERE id ='$event_id'";
Otherwise, you are comparing it with (1), (2) or whatever number it has, but with parenthesis.
UPDATE: and, of course, SANITIZE your $_POST variables!!!

Filling NULL value again on deletion or updation

I have updated certain field in my database & that field has default value as NULL. How can I again fill NULL if field is emptied or deleted. As during this updation blank value is filled in database and of course that is not NULL. Is filling NULL instead of blank value good?
Using php how can I do that? Suppose I have condition if code
if(!empty($photo))
{ $a in database;
}
But in this condition if $photo is empty it will fill blank value of $a... Let me know to fill NULL in database.
You just have to write null, and not an empty value, to your database.
You SQL query would look like this :
update your_table
set your_field = NULL
where ...
instead of :
update your_table
set your_field = ''
where ...
To switch between those two queries, you might need some condition in your PHP code, depending on how it's organized ; maybe like this :
if (empty($photo)) {
// query to set the field to NULL
}
else {
// query to update the value
}
Note that if you have no value to store for a row in your database, you might also want to just delete that row :
delete from your_table
where ...
Of course, it's up to you to determine which is the best for your application : a row with a NULL value, or no row.

Update int in MySQL Field

How can I increment an int in a cell of a MySQL database? I know that auto-increment is no use because I never want to add a new row, just update an existing one. I'm currently using this (POST var used for clarify, is verified in the real code):
$columnToUpdate = 'type'.$_POST['voteid'];
$query = "UPDATE myTable $columnToUpdate = $columnToUpdate+1 WHERE id=1;";
if(!mysql_query($query)) {
echo json_encode(array('success' => false, 'message' => 'Update failed: '.mysql_error()));
exit;
}
In the database I have 6 fields, id, type1, type2, type3, type4, type5, and a single row with id set to 1. The intention is to recieve a number (1-5), and build a reference to the correct column before updating the field. That results in Update failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=type4+1 WHERE id=1' at line 1, so I guess it's not getting the field value out properly before it increments.
Once this is working I'm also going to need to decrement a field in the same way, unless its value is 0. So for bonus points, can I do all this in one query or would it be better to split it up?
I think you've missed the keyword 'SET' from your query - try
$query = "UPDATE myTable SET $columnToUpdate = $columnToUpdate+1 WHERE id=1;";
Edit:
To do the "decrement unless it's zero" you could use something like:
UPDATE myTable SET $columnToUpdate =
CASE $columnToUpdate
WHEN 0 THEN 0
ELSE $columnToUpdate - 1
END CASE
WHERE id=1;`
For bonus points, to decrement:
$query = "UPDATE myTable SET '$columnToUpdate' = '$columnToUpdate'-1 WHERE id=1 AND '$columnToUpdate' > 0";
Besides the injection issues, it seems as if your workflow may need some work. Are you sure you want to choose the column that will be updated based on POST variable? It seems like you would specify the column and use the variable to find the record that needs to be updated:
IE:
"UPDATE myTable SET votes=votes+1 WHERE id=$post_variable;"
Again you should send the variable as a parameterized query to protect yourself from SQL injection.

Categories