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

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\_%'";

Related

How can I increment a column when inserting values into a database table without using AUTO INCREMENT

I've got a column inside my database "#", I would like to increment that for each insert statement I do.
$sql = "INSERT INTO card (#,creditCard, expdate, cvv)
VALUES ('','$creditCardStore','2020-01-01','$cvv')";
I can't alter the table to use AUTO INCREMENT.
I found this on the web but i don't know what to put inside the for(..)
$value = 1;
for(...){
$sql = 'INSERT ...';
$value++;
}
Well you could try inserting the previous max value for # in your table, plus one:
INSERT INTO card (num, creditCard, expdate, cvv)
SELECT MAX(num) + 1, ?, '2020-01-01', ?
FROM card;
Here I am using ? placeholders for the credit card store and verification code, under the assumption that you should be using a PHP prepared statement.
How about checking what the last # was, set it as an variable and increase another varible and insert that variable?
Something like this (This is not a valid code, just a description from what I meant above)
SELECT # FROM card LIMIT 1 ORDER BY # ASC
n1 = #
n2 = # + 1
and then in value, insert n2.
Try this it will work, simply add plus one in this column.
$sql = "INSERT INTO card set id=id+1,creditCard='$creditCardStore', expdate=''2020-
01-01', cvv='$cvv'";

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'";

MySQL Update only certain fields in a table

I have some insurance information in a website, and I'd like to only edit certain fields the user wants to change like for example:
user, id, phone, address, city
and the user wants to change his city and phone...do i have to make a query for each specific case or is there a code that can help me retrieve the key(phone) and value (9397171602)??
to then send it in a query
Basic update would take the form of:
UPDATE table_name SET column_1 = value_1, column_2 = value_2 WHERE column_3 = value_3
Where col1, col2 would be your city and phone, and col3 would be the user id. Check out the MySQL website http://dev.mysql.com/doc/refman/5.0/en/update.html for more info
There are number of ways to update a record safely. Conside the following pseudo code + php program.
class Example
{
function UpdateRecord($editedRecord)
{
//Fetch existing record from the database
$originalRecord=DB::fetchExample($editedRecord->getId())
//validate each edited field and it is valid then assign its value
//to the originalRecord's field
if(IsValid($editedRecord->getName())
{
$originalRecord->setName($editedRecord->getName());
}
.....
//update the record.
$originalRecord->Update();
}
}
Just add some sql to it:
$sql = "UPDATE example SET col_1 = val_1, col_9 = val_9 WHERE col_7 = val_7";
mysql_query($sql);
Then replace the columns and values with you stuff. For further info: PHP MySql Update

Categories