I have a column called name in a table info
So I want to update that with it's current value, like this name + user_input. I tried with this code but not working
mysql_query("UPDATE info SET name = name + '$user_input' WHERE id='$user_id'");
But It returns 0 and update column to 0....
Any idea how to accomplish this task??
You should use CONCAT to concatenate string in MySQL (+ is for addition arithmetic operations which I guess can't properly work with names) :
UPDATE info SET name = CONCAT(name,'$user_input') WHERE id='$user_id'
Related
Hello i'm new in mysql and i have to run a multiple update on my table.
I have 700 records in the table and i have to update them all this way:
table example :
store_id: 1
store_email: storename#gmail.com
for single update i use
UPDATE stores SET email = '1#gmail.com' WHERE id = 1;
i need to update all the emails and replace their name with their id, so it would be like this:
storename#gmail.com --> 1#gmail.com
storename#gmail.com --> 2#gmail.com
storename#gmail.com --> 3#gmail.com
those numers have to be the ID for each store.
Hope you can understand
Thanks for help.
P.S. i need to run it on magento 2
you can use CONCAT() and RIGHT() function for manipulating strings like this:
UPDATE stores SET email = CONCAT(id, RIGHT(email, 9));
The RIGHT('string', n) function extracts n characters (storemail = 9 chars in your case) from a string (starting from right).
Since you are adding id to String column gmail, you can use contact() fucntion like below :
UPDATE stors SET email=CONCAT(id, "#gmail.com") where id=2;
may I ask how will I be able to add two text values into a single row in my database?
I've tried the following, but no avail.
update tblLeave set AdminRemark='a' + 'b',Status=1,AdminRemarkDate=2019 where id=12
update tblLeave set AdminRemark=('a' + 'b'),Status=1,AdminRemarkDate=2019 where id=12
This works update tblLeaves set AdminRemark=concat('a', 'b'),Status=1,AdminRemarkDate=2019 where id=12
but when i tried to implement it in my code, it does not.
This is the actual code.
$sql="update tblLeave set AdminRemark=concat(:description,:admremarkdate),Status=:status,AdminRemarkDate=:admremarkdate, where id=:admid";
This is where i get the problem =
AdminRemark=concat(:description,:adminfullname)
The result should add two text values into AdminRemark (AdminRemark = done : John Smith)
EDIT: I've resolved it guys, I forgot to add $query->bindParam(':description',$description,PDO::PARAM_STR);
im trying to learn and understand mysql inject, i have created demo case.
SELECT ret_variable FROM data WHERE name = '".$name."' AND age = ".$age;
then if(ret_variable == 2){something} but query originally returns 1 and i need to force it to output 2
How to modify $age variable to set custom output field for ret_variable(only in response) ?
I have tried few ways with OR but didn't wroked.
I see no practical application other than learning. I assume since you know the code , you have permission to test this out. So let's give it a go!
You can only return a 2 for the ret_variable when there is a row in the database with a value of 2 as the ret_variable and you know the name value of that row. You can for instance enter that name and the following to bypass the correct value for the age.
age AND ret_value = 2
That would create the following query:
SELECT ret_variable FROM data WHERE name = 'John' AND age = age AND ret_value = 2;
The principle of mysql injection is this sort of manipulation of the query. But you can not force a value which is returned unless there is a row in the database with this value for ret_variable and you can somehow select this row.
When you don't know the name (or there is no record of your known name with a ret_variable of 2) it is not possible.
Since the AND operator has precedence over the OR operator you cannot manipulate the query to give a 2 as ret_variable. This is because the name = '?' part will always fail.
I'm having trouble adding 1 to a column value in MySQL. I've used backticks on the column name and value isn't incrementing. Here is my query:
$update = $connectdb->prepare("UPDATE `strings` SET posted=posted, `response-comment`=`response-comment` + 1 WHERE `id`=?");
$update->execute(array($id));
Why isn't my query working? The value $id is correct, the column response-comment should increase by 1.
Try using this for your SQL statement (presuming strings is the name of your table:
UPDATE `strings` SET `response-comment`=`response-comment` + 1 WHERE `id`=?
Be careful with tick marks
If improperly coded you can end up with quotes which is going to transform you integer value into a string and thus changing the behavior of your request.
Have you tried with out , justresponse-comment = response-comment + 1`
we have a auto-generated field in database table and we want to add prefix in auto-generated value Like AVL0001.
So you could do it a couple ways... Is this an auto-index field in the database? If it is an integer type, then you won't be able to include data like you are mentioning above, however, if it is something generated from a script, just concatenate the number and your prefix before insert. You could probably also do this with a trigger on the database. Any additional details would help improve this answer.
$currentdbvalue = 'example';
$prefix = 'AVL0001';
$newvalue = $prefix.$currentdbvalue;
outputs "AVL0001example"
or if you'd like an underscore u can use:
$newvalue = $prefix."_".$currentdbvalue;
which would output "AVL0001_example"
Let id be your table column. You can add AVL ahead of your id by concatenating both in your sql query.
ie In Mysql,
$yourid="1";
INSERT INTO table( id )
VALUES (
CONCAT( "AVL", $yourid, id )
)
Or you can concatenate the AVL with yourid before inserting it into database like,
$yourid="AVL"."1";
In either way you cannot add it into an auto incrementing field. Because its type is INT.