mysql_query wont update if the String value contains a comma (,) - php

Why is it, that I can't UPDATE a field (type:longtext) with a string that contains a comma (,)...
$result=mysql_query("UPDATE table_name SET column1=$a WHERE column2=$b AND column3='price'");
Works well with
$a="10"; or $a="10.99";
$b="15";
doesn't work with
$a="10,99";
$b="15";
neither works with:
$a="10,99";
$a=mysql_real_escape_string($a);
What am I doing wrong here?

When your query string is being evaluated with the values that contain commas, this is the query:
"UPDATE table_name SET column1=10,19 WHERE column2=15 AND column3='price'";
The comma is creating an issue because the value 10,19 is not a string. You need to put quotes around your variables. If you use double-quotes, you will need to escape the quotes. Single quotes work as well; either of the following should do the trick:
$result=mysql_query("UPDATE table_name SET column1=\"$a\" WHERE column2=\"$b\" AND column3='price'");
or
$result=mysql_query("UPDATE table_name SET column1='$a' WHERE column2='$b' AND column3='price'");

Change (you need to wrap string or text type of field with ')
$result=mysql_query("UPDATE table_name SET column1=$a WHERE column2=$b AND column3='price'");
To
$result=mysql_query("UPDATE table_name SET column1='$a' WHERE column2='$b' AND column3='price'");

Related

unable to update value in mysql database using php

I am not able to update MySQL table using PHP. How can I do that?
I have tried by changing the order of double quotes.
$name=mysql_real_escape_string($_POST["steel"]);
$db->execute("UPDATE order SET need=$name WHERE raw-id='1'");
It should store $name in the database.
You should wrap your $name with single quote, because you are trying to pass a string into the SQL
$db->execute("UPDATE order SET need='$name' WHERE `raw-id`='1'");
You should wrap your {$name} with single quote and bracket to , because need row is a string into the SQL
$db->execute("UPDATE order SET need='{$name}' WHERE `raw-id`='1'");
You need to wrap your column name in back-ticks because it has a dash in it, e.g:
$db->execute("UPDATE order SET need = '$name' WHERE `raw-id` = 1");
By referring to the manual I think you should first prepare your
query and then use execute() method. Something like this:
$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
$stmt = $mysqli->prepare($query);
$stmt->execute();

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

Select a row where field name is enable and value is true

I am new to php, in my sql table I have a row with these columns:
id, custid, name, value
id is auto increment, custid is unique value, name is a enable (status parameter) and value set to true or false.
Now I just want to select a case where
$sql = 'cu_id FROM table WHERE name = 'enable' AND value = 'true'' ;
in a PHP file, but my php file says, line has syntax error, at enable.
Can anyone please have a look what is it :)
use double quotes,
$sql = "cu_id FROM table WHERE name = 'enable' AND value = 'true'";
but the best way to do is to use prepared statement to avoid from sql injection.
You have following errors in your query:
You are using single quotes within single quotes. You can fix this by wrapping double quoting the query string and keeping single quotes for column values. Or you can choose to escape the single quotes used in the values with a backslash, e.g. \'enable\'
Your table name is one of MySQL reserved words i.e. table. When you use one of MySQL reserved words you need to quote them with backticks e.g `table`.
Please try the following:
$sql = "cu_id FROM `table` WHERE name = 'enable' AND value = 'true'";
Use double quotes and instead of using true and false you can use 1 and 0. With default value 0 or 1 as you wish so that query will look like. You can also replace value as status
$sql = "select cu_id FROM table WHERE name = 'enable' AND status = 0";
Pass your query with in the double coats, it is the best practice
$sql = "cu_id FROM table WHERE name = 'enable' AND value = 'true'" ;

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

my mysql query doesn't update my table

$query=UPDATE americana SET 7='99' WHERE Bdate='2011-04-15';
mysql_query($query);
7 is a column name and Bdate too
it doesn't update my table?
if so enclose it with backtick "`"
$query="UPDATE americana SET `7`='99' WHERE Bdate='2011-04-15'";
$query="UPDATE americana SET `7`='99' WHERE Bdate='2011-04-15'";
mysql_query($query);
The query should be enclosed in quotes and also marked column name with backtick(`)sign if it has name with starting from numeric value, try it now and see if it works better now...

Categories