Update multiple values in WHERE MySQL - 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')";

Related

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.

mysql concatenate and increment by one

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...

Inserting data into MySQL while looping over a result set

How can I execute insert queries while retrieving data from database?
I mean that after running a select query where I am retrieving data using a while loop, I also want to insert some fields into another table.
I want to execute inserts forcefully in PHP code.
I tried:
$firstview=mysql_query("insert into kadam_firstview(jobtype,jobname,admin_id,date,datetime)values('".$trial."','".$jobname."','".$adminid."','".$time."','".$date."')");
$firstview.execute();
But it doesn't work.
i want to run upper query inside select query as i am getting data from select query at that same time i want to save those data inside another table but query giving error which is mentioned above
I think your question is more for php developer then for dba.
Anyway if I understand you correctly you would like to select a record set from a table and insert this values (or modified one) into another table?
<?php
$db = new mysqli("host", "username", "password", "databasename");
$result = $db->query("SELECT * FROM table_name");
while ($row = $result->fetch_object()) {
$trail = $row->trail;
$jobname = $row->jobname;
$adminid = 25;
$time = $row->time;
$date = $row->date;
$query = "insert into kadam_firstview(jobtype,jobname,admin_id,date,datetime) values('".$db->escape_string($trial)."','".$db->escape_string($jobname)."','".$db->escape_string($adminid)."','".$db->escape_string($time)."','".$db->escape_string($date)."')";
$db->query($query);
}
this will copy the rows from the table "table_name" into the table "kadam_firstview" and set the adminid to 25.
http://php.itronic.at/manual/en/class.mysqli.php

Deleting my data in my table but not in database

I have a query here. I wanted to delete my data in my browser table but I dont want it to be deleted permanently, I just want it to be hidden. My problem is I don't know what kind of query will I be needing. Here is my delete query.
My table feilds are Id Name and Flag. I want to do is when the flag is 1 it shows up the table and if it is 0 it will be hidden when the delete button is pressed.
function delete()
{
$id = $this->input->get('id');
$sSQL = "DELETE FROM tableq where id = ?";
$this->db->query($sSQL, array($id));
}
You can just update the flag, and in your select query select only flag=1 Try this,
$sSQL = "update tableq set `flag` =0 where id = ?";
$this->db->query($sSQL, array($id));

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