Update Query in PHP - php

I want to update 2 columns of a table, I've echoed the $sql1, it is showing dates what I want(y-m-d) to insert into database, but when I fire the query, dates were updated as 0000-00-00 (y-m-d) in database.
output of echo $sql1
UPDATE member SET
reg_date='2014-03-05' AND expiry='2014-03-06'
WHERE bill_id='9'
following are my query statements--
$sql1="UPDATE member SET
reg_date='$date1' AND expiry='$date'
WHERE bill_id='$_REQUEST[bid]'";
mysql_query($sql1,$con)or die(mysql_error());
Can you please solve the error?

The correct syntax is to separate by commas in your SET clause.
UPDATE table
SET field1 = 1, field2 = 2, field3 = 3
WHERE field = 1
In your case:
$sql1="UPDATE member
SET reg_date='$date1', expiry='$date'
WHERE bill_id='" . $_REQUEST[bid] . "'";
mysql_query($sql1,$con)or die(mysql_error());
I also separated out your WHERE clause, as it looks as if it may have trouble parsing the string.

You should separate your values with commas, not with and
$sql1="update member set reg_date='$date1', expiry='$date' where bill_id='$_REQUEST[bid]'";

$sql1="update member set reg_date='$date1', expiry='$date' where bill_id='$_REQUEST[bid]'";
You should not use "and" to separate values, instead you can use "commas" to separate values.

Related

Table values will not change with this Update query . No error is displayed and values remain the same

Im trying to update my tables using this query. Once I hit 'update' im redirected to the view all page and all values remains the same. No change takes place but Not getting an error either. Please Help!
$sql = "UPDATE myaddressbook.contacts SET (firstName,lastName,nickName,cellNumber,homeNumber,workNumber) VALUES ('$firstName','$lastName','$nickName','$cellNumber','$homeNumber','$workNumber') " ; "UPDATE address SET(street,city,state,country) VALUES('$street','$city','$state','$country')"; "UPDATE contacts SET (email,birthday,memo)
VALUES ('$email','$birthday','$memo') id = '{$_REQUEST['id']}'";
Your queries are little bit off. Update queries use UPDATE [table] SET [column] = [value], [column] = [value] ... WHERE [condition], [condition] ... syntax, and it looks like you've confused that with INSERT syntax, which is INSERT INTO [table] ([column], [column], ...) VALUES ([value], [value], ...);
Try the following (hopefully it gives you a good idea of how to rewrite it):
UPDATE myaddressbook.contacts
SET firstName = '$firstName', lastName = '$lastName', ...
WHERE id = '$_REQUEST["id"]'
UPDATE does not use VALUES -- it should be formed like this...
UPDATE your_table_name SET your_field='your_value' WHERE ID='the_myself_id'
(where ID = is a unique identifier column for the table)

How to update value of a column as difference between two variables in sql?

Here, update chooses bal(for balance) column of register table whose is to be changed to difference of bal and the value in variable $tax for only the row saisfying the condition. It's not working in following code.How to do it?
$query3 = mysql_query('update table register set bal=bal-'.$tax.' where name='.$user.')' , $connection);
You are using in an incorrect way.
Try this:
$query3 = mysql_query("update table register set bal=(bal-$tax) where name='$user'" , $connection);
It seems, there is extra ')' after user, that hasn't '('

Unknown Column " " in field list - Update Query

This query had previously worked, now when it is run again we get Unknown Column in field list error.
The query works well if we do not use variables and set raw data. The columns match those in the database.
$update_order_id = "UPDATE order_tbl SET o_process=$process, o_payment=$payment, o_paymentType=$paymenttype WHERE o_id=$orderid AND o_active='1'";
You need wrap single quotes for the values in the query as
o_process='$process'
etc
So the query will be as below. For string values its necessary.
$update_order_id = "UPDATE order_tbl
SET o_process='$process',
o_payment='$payment',
o_paymentType='$paymenttype'
WHERE o_id= '$orderid' AND o_active='1'";
You might need to surround your variables with quotes, only integer columns doesn't need quotes.
$update_order_id = "UPDATE order_tbl SET o_process='$process', o_payment='$payment', o_paymentType='$paymenttype' WHERE o_id='$orderid' AND o_active='1'";

UPDATE MySQL table that contains comma (,) in the field

Imported into a MySQL table is a report that contains a user generated field value.
Possible field values include:
PREFIX,1234
PREFIX1234
PREFIX_1234
All the example values represent the same thing but are typed in differently (I don't control the input system).
I would like to have all the values use this format:
PREFIX_1234
I was trying to run this PHP query against the database to fix the comma values:
$sql = "UPDATE tableA
SET unit_number = replace(unit_number,'FRGN,','FRGN_')
WHERE unit_number like 'FRGN,%'";
But this doesn't seem to be working.
Do I need to escape the comma in the query in order for it to work?
Try this:
$sql = "UPDATE tableA
SET unit_number = concat('FRGN_', replace(replace(replace(unit_number,'FRGN,','') ,'FRGN_',''), 'FRGN', ''))";
or
$sql = "UPDATE tableA
SET unit_number = concat('FRGN_', replace(replace(text,'FRGN,','') ,'FRGN',''))
WHERE unit_number NOT LIKE 'FRGN\_%'";

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