How to combine multiple text value into a single sql database row - php

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);

Related

How to make a multiple update on mysql with string replace with value of another column

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;

how to update array in crate

I would like to know how I can update a value stored in an array, in crate.io
I have a blog table - blog_tbl
A column, with data type array - tags
A id column
Inside the tags column I have - ["tag1","tag2","tag3"]
I would to know how I would go about changing 'tag1' to 'tag99'
I tried
update blog_tbl set tags['tag1'] = 'tag99' where id = '1';
Also how would I add one the the end? so making it -
["tag1","tag2","tag3","tag4"]
many thanks
Unfortunately it's not possible currently. Array elements can only be selected using the subscript notation (e.g. select tags[1] from blog_tbl;) but not updated. Maybe add a GH issue requesting that feature.
You can use the pattern found here: https://crate.io/docs/reference/sql/occ.html#optimistic-update
However, that requires you to perform the modification on client side. Pseudo code:
updated = False
while not updated:
cursor.execute('SELECT array_field, "_version" FROM table WHERE id=1')
row = cursor.fetchone()
current_array_field = row[array_field]
current_array_field.append('newtag')
cursor.execute('UPDATE array_field = current_array_field WHERE id=1 AND "_version" = row[version]')
if cursor.rowcount > 0:
updated = True
This will make your update semi safe for concurrent updates of the same field.

Update MySql values with it's current value in php

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'

MySQL returning an empty result set

I tried doing this in PHP but I got 0 rows returned all the time. Then after some time searching around on StackOverflow, I saw a tip to try doing it in SQL first to see if the results are returned properly.
I tried to do it in SQL and it's returning an empty result set all the time, even tho the values are there.
SQL
SELECT * FROM `serials_table` WHERE `ser_key`='ABCD-EFGH-IJKL-MNOP'
PHP
$result = $link->query("SELECT * FROM serials_table WHERE ser_key='$key'");
Both are returning null value.
ser_key column is set to text type, coallition: utf8_unicode_ci, Null: No, Default: None
The serial key entry is in there and the column 'ser_key' exists as well as the table 'serials_table'. Also I directly copy-pasted the serial key from the table and placed it into the query to avoid any typos.
Did I make some errors with the table structure or something?
I have no idea what to do here, any help would be appreciated.
When this works
SELECT * FROM serials_table WHERE ser_key like '%ABCD-EFGH-IJKL-MNOP%'
Then you have leading or trailing spaces in your data.
To revert that update your existing table data like this
update serials_table
set ser_key = trim(ser_key)
After that check where you insert or update the ser_key. In that code segment check if you put only trimmed data in there.
Try
SELECT * FROM `serials_table` WHERE TRIM(`ser_key`)='ABCD-EFGH-IJKL-MNOP'
Remove white spaces
UPDATE `serials_table` set `ser_key`= TRIM(`ser_key`);

UPDATE in mysql didn't work in my PHP page.

First of all I'm a rookie to Programming, I created a PHP page to update a value from my mysql(myadmin) database, but the value is not updating. I also tried to retrieve values from database it's working just fine but this UPDATE code is not working! I don't know why, please check out my code below.
$qs=mysql_query("update staff set review=$newrate where name=$rateuser");
$resu=mysql_query($qs);
All variables are double defined, assigned with proper values, checked and I tested variables using echo, table name is also checked, it's all fine, but I think the problem is with Update query, I searched internet for the syntax but it's not different than mine. Please help me out
How are $newrate and $rateuser set?
mysql_query("UPDATE staff SET review = '".mysql_real_escape_string($newrate)."' WHERE name = '".mysql_real_escape_string($rateuser) ."'");
http://php.net/manual/en/function.mysql-real-escape-string.php
Try:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
Do not use second line.
You probably just need some " around your values $newrate and $rateuser
But if you did an echo, why not actually echo for us what the query-string becomes?
You need single quotes around string values on your query:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
(assuming both variables are strings)

Categories