mysql concatenate and increment by one - mysql

I am trying to update mysql table MYTABLE using two value. One is STAR column which should be incremented by one on each query, and the second one is COMMENT column which should be concatenated with existing one on each time and separated by comma.
Below is the command I used, but not working.
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, ','.$comment) where ID='$id'";

$query = "update MYTABLE set STAR=STAR+1,COMMENT = CONCAT(COMMENT, ',', '$comment') where ID=$id";
where ID='$id'
is incorrect because $id might be a number, so, delete the "'".
Have you escaped the $comment variable ?
Otherwise you may use prepared statements with PDO :)
I hope you're using PDO...

you should but string in '' and update your query, it has error syntax :
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, '$comment') where ID='$id'";

To make it more secure, just use following code...
$query = "update MYTABLE
set `STAR` = `STAR`+1,
`COMMENT`= CONCAT(COMMENT, '$comment')
where `ID`='$id'";
Happy Coding...

Related

How to separate string with comma using concat function in mysql?

code:
$insert = "UPDATE TABLE SET FIELD = CONCAT_WS(',', '', '$string') WHERE something = 'something';
In this query I have an variable $string where I want when I click on submit button it store data like ,a,b,c,d but now what happen when I click on submit button it update my table with ,a and doing same process at second time it update ,b but I want ,a,b so how can I do this? Please help me.
Thank You
You were almost there !
If you want to append a new string to your field here is the query :
$insert = "UPDATE TABLE SET FIELD = CONCAT_WS(',', FIELD , '$string') WHERE something = 'something';
Because if you look how work
CONCAT_WS (W3school link)
it's CONCAT_WS(separator, expression1, expression2, expression3,...)
here expression1 is the FIELD you want to update and expression2 the $string

insert/update data to 3 tables on one query joomla

I had a problem need to optimize following 3 query in one. can I create a function?
$query = "UPDATE #__sbc SET payment_reference ='".$payref."', payment_status = '".$state."' WHERE reference ='".$ref."'";
$db->setQuery($query);
$db->query();
//update allforms table
$query = "UPDATE #__allforms SET payment_reference ='".$payref."', payment_status = '".$state."' WHERE reference ='".$ref."'";
$db->setQuery($query);
$db->query();
$query = "UPDATE #__printxml SET payment_reference ='".$payref."', payment_status = '".$state."' WHERE reference ='".$ref."'";
$db->setQuery($query);
$db->query();
I don't clearly understand what you mean here by optimizing, sorry if I misunderstood but maybe you meant something like this? Explain what do you mean by optimizing 3 queries in one
$params=array("sbc","allforms","printxml");
foreach ($params as $param){
$query = "UPDATE #__".$param." SET payment_reference ='".$payref."', payment_status = '".$state."' WHERE reference ='".$ref."'";
$db->setQuery($query);
$db->query();
}
You can combine queries by adding semi column (;) to the end of the query. For example:
UPDATE TABLE1 SET `field1`=2 WHERE `id`=5;UPDATE TABLE1 SET `field1`=3 WHERE `id`=6;
The thing is, combining queries is unlikely to provide any (even slightly) noticeable performance boost.

Update multiple values in WHERE MySQL

I want to update multiple itemIDs with the same status. I know that this is imposible with mysql_query but I can't figure out a way to get this working.
$upd = "UPDATE booking SET status='$status' WHERE itemID='$itemID', '$itemID2'";
$retval = mysql_query($upd, $con);
Note: The itemIDs are inputted by user in prev page like this
$itemID= $_POST["itemID"];
$itemID2= $_POST["itemID2"];
Use IN clause as like:
WHERE itemId in ('$itemID', '$itemID2');
instead of WHERE itemID='$itemID', '$itemID2'
Use IN
$upd = "UPDATE booking SET status='$status' WHERE itemID IN ('$itemID', '$itemID2')";

Changing boolean value in SQL database

I have this query, which spits out that I have an error in syntax. I cannot for the life of me understand what it is. I have a table, where one column is email and the other is subscribed (the latter of which is a boolean using tinyint). Any idea what's wrong with this syntax?
$query = "UPDATE $DB_TABLE SET $DB_IS_SUBSCRIBED_KEY = 0 WHERE $DB_EMAIL_KEY = $email";
Your email value needs to be wrapped in quotes.
UPDATE tablename SET columname = 1 WHERE emailcolumn = "email#email.com"

How do I change the case on every field in a mysql table in one call?

I have a table with 27 varchar fields. I want to make all fields lowercase, but i want to do it in one short mysql call.
This does a single field:
UPDATE table
SET field = LOWER(field)
How do I do the equivalent of this (which doesn't work):
UPDATE table
SET * = LOWER(*)
You can't do it with your creative attempt SET * = LOWER(*) etc.
You can however do it like this:
UPDATE table SET
column1 = LOWER(column1),
column2 = LOWER(column2),
-- etc, listing all text type columns
columnN = LOWER(columnN);
The reason there's no "shortcut" is probably because this pattern is so infrequently needed.
The consensus is that this cannot be done in a single mysql query.
Here is a super quick PHP script that does this for N fields (thanks for the idea #alex):
$sql = "SHOW COLUMNS
FROM table";
$results = mysqli_query($dbcon,$sql);
while($column = mysqli_fetch_assoc($results))
{
$column = $column["Field"];
$sql = "UPDATE table
SET $column = LOWER($column)";
$success = mysqli_query($dbcon,$sql);
}