Here i am trying to increment the value in database by + 1.for example
here in database i have value 3 in one column by name owners now when i call the below function. i want to increment the column by +1 i.e the column value now should become 4
function updateOwners($product_id){
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE product_list set product_current_co_owners = where product_id ='$product_id'";
$result = $conn->query($sql);
/*if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<b>Total co-owners:</b><br><span>'.$row["product_co_owners"].'</span>';
}
} else {
echo "No data available";
}*/
$conn->close();
}
how can i do this?
$sql = "UPDATE product_list set product_current_co_owners =# here i want add +1 to previous value where product_id ='$product_id'";
Just add +1 to the previous value like
UPDATE product_list
set product_current_co_owners = product_current_co_owners + 1
where product_id ='$product_id';
Try this but your column must be integer type
UPDATE product_list set product_current_co_owners =product_current_co_owners+1 where product_id ='$product_id'
$sql = "UPDATE product_list set product_current_co_owners = owners + 1 where product_id ='$product_id'";
Related
I've script with import csv and column mapping.
After mapping column .csv in html table I need udate data.
I try write this script:
$file_data = $_SESSION['file_data'];
unset($_SESSION['file_data']);
foreach($file_data as $row)
{
$data[] = '("'.$row[$_POST["sku"]].'", "'.$row[$_POST["stock"]].'")';
}
if(isset($data))
{
$query = "
UPDATE products
SET (sku, stock)
VALUES ".implode(",", $data)."
WHERE sku = ".$row[$_POST["sku"]]."
";
$statement = $connect->prepare($query);
if($statement->execute())
{
echo 'Data Updated Successfully';
}
}
But I have problem with construct correct and valid udate query:
$query = "
UPDATE products
SET (sku, stock)
VALUES ".implode(",", $data)."
WHERE sku = ".$row[$_POST["sku"]]."
";
I try also in this way but still something went wrong:
$file_data = $_SESSION['file_data'];
unset($_SESSION['file_data']);
foreach($file_data as $row)
{
$query = "
UPDATE products
SET sku = '.$row[$_POST["sku"]].', stock = '.$row[$_POST["stock"]].'
WHERE sku = '.$row[$_POST["sku"]].'
";
$statement = $connect->prepare($query);
I try also:
$file_data = $_SESSION['file_data'];
unset($_SESSION['file_data']);
foreach($file_data as $row)
{
$data1[] = '("'.$row[$_POST["sku"]].'")';
$data2[] = '("'.$row[$_POST["stock"]].'")';
}
if(isset($data, $data2))
{
$query = "
UPDATE products
SET stock = ".implode(",", $data2)."
WHERE sku = ".implode(",", $data)."
";
$statement = $connect->prepare($query);
if($statement->execute())
{
echo 'Data Updated Successfully';
}
}
I need update values stock with rule where sku = and here post values sku from csv file
i am trying to update a table column which same column from same table select.
Here is the code (updated)
public function UpdateStockIn($id, $subUnitValue) {
$query = "UPDATE BRAND_LIST SET CURRENT_STOCK_BOTTLE = (SELECT CURRENT_STOCK_BOTTLE FROM BRAND_LIST WHERE ID = ?) + '.$subUnitValue.' WHERE ID = ? ";
$success = 0;
try {
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $id);
$stmt->execute();
$success = 1;
} catch (PDOException $ex) {
echo $ex->getMessage();
}
return $success;
}
it Show error like this
You can't specify target table 'BRAND_LIST' for update in FROM clause
Try run these 2 sqls, The first one will store a value into mysql local variable then use into 2nd sql.
SELECT #old_subUnitValue := GROUP_CONCAT(table1.CURRENT_STOCK_BOTTLE) FROM BRAND_LIST AS table1 WHERE table1.ID=2;
UPDATE BRAND_LIST AS table2 SET table2.CURRENT_STOCK_BOTTLE = #old_subUnitValue + '.$subUnitValue.' WHERE table2.ID=2;
Use the below query
$query = "UPDATE BRAND_LIST SET CURRENT_STOCK_BOTTLE = CURRENT_STOCK_BOTTLE + ".$subUnitValue." WHERE ID = ?";
I want to get all emp_ids of employees which is manager_id equal to login user_id. But why this query show only first result of the column. I could not able to find the problem.
$userID=$_SESSION['userID'];
var_dump($userID);
$result1 = mysql_query("SELECT emp_id FROM employee where manager_id='$userID' ORDER BY emp_id");
$array = mysql_fetch_array($result1);
$id1=$array['emp_id'];
Please help !
You should loop over the result
and eg: you can store all the empID in an array
$cnt = 0;
while ($row = mysql_fetch_array($result1)) {
echo "ID: " . $row[0] ;
$myArrayOfEmpID[$cnt] = $row[0];
$cnt++;
}
instead you only set
$array = mysql_fetch_array($result1);
$id1=$array['emp_id'];
I am having a weird issue but I do not understand what is happening. I amcreated a function to update up to three columns (end_plan_date, balance and server) in a table (user) and 2 inserts in another table.
For some reason, my last update (column server of the user table) is not committed ($query = mysql_query("UPDATE user SET server='$serv' WHERE email='$subemail'");) unless I give a value for at leat one of the two other values ($subamt or $subday).
Do you know why this query is not updating the user table with the server value I parsed?
function addBalance($subemail, $subamt,$subday,$userid,$serv) {
$q = "SELECT * FROM user WHERE email = '$subemail'";
$result = mysql_query($q, $this->connection);
$dbarray = mysql_fetch_array($result);
$endplan_date=$dbarray['end_plan_date'];
if($subday >0){
if($endplan_date=="0000-00-00" ){
$endplan_date = date('Y-m-d');
$new_endplan_date = date('Y-m-d',strtotime($endplan_date . "+".$subday." days"));
}else{
$new_endplan_date = date('Y-m-d',strtotime($endplan_date . "+".$subday." days"));
}
$query = mysql_query("UPDATE user SET end_plan_date='$new_endplan_date' WHERE email='$subemail'");
$recdate=gmdate('Y-m-d H:i:s');
$q = "INSERT INTO com_gest(recdate,userid,type,recvalue) VALUES ('$recdate','$userid','Plan Date','$subday')";
mysql_query($q, $this->connection);
}
if($subamt >0){
$query = mysql_query("UPDATE user SET balance=balance+".$subamt." WHERE email='$subemail'");
$recdate=gmdate('Y-m-d H:i:s');
$q = "INSERT INTO com_gest(recdate,userid,type,recvalue) VALUES '$recdate','$userid','Balance','$subamt')";
mysql_query($q, $this->connection);
}
$query = mysql_query("UPDATE user SET server='$serv' WHERE email='$subemail'");
return 0;
}
In your code u can't get directly this
$endplan_date=$dbarray['end_plan_date'];
for fetching this variable u have to use while loop like
while($dbarray = mysql_fetch_array($result);) {
$endplan_date=$dbarray['end_plan_date'];
}
There was an issue when calling this function.
Bit of a noob when it comes to PDO. We have a a table that needs to be updated with a cron job daily. The idea is to query the table and for each "active = 1" get an integer from a duration column convert that into days, get the "expiration" column and the duration to the expiration and update the expiration.
I am not even close to getting this to work, I am trying to just get the basics for now. Return the duration which is in hours divide by 24 add that to the date column as days and update EACH row with the appropriate new time.
The problem is I do not know how to update each row with its own information. I have the code below, it updates each row but the new "expires" date ends up being the same even though the expires values are different.
static function cronSetFeatured(){
try{
$db = new PDO('mysql:host=127.0.0.1;dbname=mydb', 'dbuser', 'pw' );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
$query = $db->query('SELECT * FROM featured_producers ');
$now = date('Y-m-d H:i:s');
while($r = $query->fetch(PDO::FETCH_OBJ)){
$duration = $r->duration;
$ddays = $duration / 24;
$date = $r->date;
$expires = date("Y-m-d", strtotime($date. " + ".$ddays." days"));
}
$sql = "UPDATE `featured_producers`
SET `expires`= ?
WHERE `id` != 0";
$q = $db->prepare($sql);
$q->execute(array($expires));
echo $expires.'<br>';
}
Well if you want to UPDATE each record on its own, that query need to be part of the loop body:
while($r = $query->fetch(PDO::FETCH_OBJ)){
//calculate expiration
$duration = $r->duration;
$ddays = $duration / 24;
$date = $r->date;
$expires = date("Y-m-d", strtotime($date. " + ".$ddays." days"));
//Get the id (needed for update)
$id = $r->id;
//updating
$sql = "UPDATE `featured_producers`
SET `expires`= ?
WHERE `id` != ?";
$q = $db->prepare($sql);
$q->execute(array($expires, $id));
echo $expires.'<br>';
}