update mysql database where current value is greater then old value codeigniter - php

we have a table "products" with two columns name code and price , i update this table with where code = "something" condition ,
$sql = "UPDATE products SET price='20' WHERE code=2";
and easily update price . but we want our query check , if our uploaded data is lower than old data , then our value will not update , and if our uploaded data is greater than old value of price , then price updated ., means we want always price value is maximum ??what is way...

You can specify the condition in the filter:
UPDATE products SET price = 20 WHERE code = 2 AND price < 20
Or else, you can use MySQL's GREATEST() function:
UPDATE products SET price = GREATEST(price, 20) WHERE code = 2

Related

Subtract a value from a field on database

i have a problem where i want so subtract 1 value from a field on my table when i add a regestry.
I have 2 tables
Livros
CodLivro
Nome
Total
and
Vendidos
CodVenda
CodLivro
Nome
Lets imagine the total is 20, when i sell a book i want to remove one value from that field.
the current code is
case "vender_l":
{
$stmt = $conn->prepare("INSERT INTO Vendidos (CodLivro, Nome) VALUES (:CodLivro, :Nome)");
$stmt->bindParam(':CodLivro', $CodLivro);
$stmt->bindParam(':Nome', $Nome);
break;
}
Use this code, it should work.
UPDATE Livros SET Total = Total-1 WHERE Livros.CodLivro = :CodLivro;
I think the comment from Masivuye Cokile is right...
Let's put the names of your tables and columns in the example:
update Livros
set total = total - 1
where codLivro = 10 -- 10 is an example of a book id
and total > 0 -- prevents the number of books from being negative

Subtraction in SQL statement for stock

A database lists items and their stock count. When someone buys items I wish to update the stock count in the database.
Regardless of the following subtraction f_item_number = f_item_number - $mugAmount, my update statement isn't working.
If I have 200 mugs in the database. When I run the following statement the new mug amount is listed incorrectly afterwards as -1. Rather than the expected value of 199. Why is this?
DB::update('UPDATE `shop_items` SET `f_item_number` = ? WHERE `f_item_name` = ?', array(`f_item_number` - $mugAmount, "mug"));
You can't pass in database column text in the parameters. Try this
DB::update('UPDATE `shop_items` SET `f_item_number` = `f_item_number` - ? WHERE `f_item_name` = ?', array($mugAmount, "mug"));

php adding amount in rows

I want to add the rows that is being query in database. There is an amount paid, previous balance, new balance.. I want the previous balance to be added by paid amount.
Num|pay|old|new
1 |100|500|600
2 |120|600|720
3 |200|720|920
4 |300|720|920
5 |350|720|920
6 |500|720|920
The database query has data of amountPaid (pay), previous balance (old), new balance (new), but the data in column(new) is wrong.I want to correct it in forloop.Is there a way to help me in this? to get the column(new) value is
column(pay)100 + column(old)500 = column(new)600.
the column(new) in number two will be put in column(old)600 then added by column(pay)120 is equal to column(new)720 and so on.
Hope you can help me in this...
I suppose you are adding the amounts when user submits it
1) Use mysql UPDATE clause in order to change the value in the coloumns
2) use while loop to get the previous value selecting it with a specific id which I suppose you have in your table
3) add it with a new value and update the new amount coloumn
PHP
<?php
$id=$_POST['id'];
$amount=$_POST['amount'];
include("connection.php");
$sql="SELECT * FROM `tablename` WHERE `user_id` ='$id'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res){
$t2 = $row['previous_amount'];
$t3=$t2+$amount;
$sql="UPDATE `bank`.`tablename` SET `new_amount' = '$t3'";
}
?>

multiply data from database to value from textbox

i have 1 table named tbl_sales. the ff data in tbl_sales are
id | total |
1 | 100 |
my question is this. i want to update "total" by multiplying it to whatever values in textbox.
this is my code so far to display textbox. i echo it so that everytime i add order it will appear on every table row
echo '<td>'.$vats_tot.'</td>'; //- the value display in this row is from database.
echo 'input type = "text" name = "ds"/>;
my problem is this. i want to multiply this the value from this textbox to "$vats_tot" which is from database value. can it be possible to multiply this?
Try like
$txt_val = $_POST['ds'];
$sql = "UPDATE tbl_sales SET total = total * $txt_val";
sql query for your problem
$textbox_value=$_post['textbox_id'];//To get TextBox Value
update **[table_name]** set total=(total*$textbox_value) where id=1;//To update the data into the table

incrementing a table column's data by one || mySql

iam having a table with columns like
id || counter
if i do something (some event) i want the counter's value(at a particular id) to increase by one , currently iam doing this :
//get current value
current_value = select counter from myTable where id='someValue'
// increase value
current_value++
//update table with current value
update myTable set counter=current_value where id='someValue';
currently iam running 2 queries for this, please suggest me some way do it in one step.
Just run the math in the database:
update myTable set counter = counter + 1 where id = 'someValue';

Categories