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'";
Related
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();
I'm quite new to coding mysqli in general and have a problem I can't solve on my own:
I have created an insert, delete and update page. Insert and delete new data in the database table works fine, but when I try to update an existing record, the query somehow deletes the content of other fields in my table row at random.
I have only used the sql sql = "UPDATE xxx SET name='$name' WHERE id='$id"; etc.
What am I doing wrong?
You've got an issue with your SQL.
This: sql = "UPDATE xxx SET name='$name' WHERE id='$id"
Should be: sql = "UPDATE xxx SET name='$name' WHERE id = $id" <=== no quote before id
Integer values should not have quotes around them.
Either that or: sql = "UPDATE xxx SET name='$name' WHERE id = '$id'", if your id column is not an integer
you didn't close the single quote in the WHERE clause.
id is an integer value, so you shouldn't need the quotes at all:
WHERE id=$id
I am trying to update a column for a certain user with PHP/MySQL. What is the proper way for me to set that equal to a variable?
$style is equal to a value that is from a form (post).
When setting 'style' equal to a string value that is also in single quotes, I do not get an error. I only get an error when setting 'style' equal to a variable.
$query = "UPDATE `users`
SET `style` = $style
WHERE `id` = $userid;";
Thank you very much.
You still have to put quotes around the variable as they are needed to tell MySQL that is a string. Remember, the variables are interpolated before the query is sent to the MySQL server. So $style is replaced by it's value before the query is run.
$query = "UPDATE `users`
SET `style` = '$style'
WHERE `id` = $userid;";
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'" ;
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'");