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
Related
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
column name = receipts (txtbox)
if user enter receipts = 5 then insert 5 rows in database.
data insert in table like this..
receipts
1
2
3
4
5
if user enter again receipts = 3 then again insert 3 rows in database.
6
7
8
like wise....
plz suggest me how to do this....
below is my code to save in php pdo...
i tried below code this is working but not inserting no in sequential order....
if(isset($_POST['save']))
{
$book_no = $_POST['book_no'];
$receipt= $_POST['receipt'];
for($row=1;$row<=$receipts ;$row++)
{
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
}
}
First you have to change the following code:
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
like this (the receipt number is the $row var, not the $receipt var)
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$row));
If you want that, the next time, it start from the last number inserted (that is, for example, start from 6, if the previous time you have inserted 5 receipts), you have to query the DB to get the the current max value, and then start from it. To get the max, a query like this should work:
SELECT MAX(receipt) FROM scheme_master where book_no = :book_no
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'";
}
?>
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
I have a file hosting site where I provide a point for every unique download to user.
Sample of my table
These points can be redeemed by user. So for example if a user redeems 100 points than what is the best query to reduce points available from each row till 100 points are reduced.
Thank You.
You should create two tables for this:
Table files
- id
- name
- size
Table points
- id
- file_id
(- user)
- points
Insert a new file:
INSERT INTO files (name, size) VALUES ('kat92a.jpg', 105544); // New file with ID 1
Now you can give points to a file, negative or positive:
INSERT INTO points (file_id, points) VALUES (1, 100); //Positive points
INSERT INTO points (file_id, points) VALUES (1, -10); //Negative points
And you can select the total number of points:
SELECT
files.name,
files.size,
(SELECT sum(points) FROM points WHERE file_id = 1) AS points
FROM files
WHERE id = 1
Alright, then, here's the SQL-dumb way I would do it. Hopefully an SQL guru will come around with a better solution. Note: This is pure pseudocode; write your own code based on this--it's not going to work out of the box.
$total_to_deduct = 100;
// Each time, get the row with the highest points
$top_points_query = "SELECT id, points FROM my_table ORDER BY points DESC LIMIT 1;"
do {
$result = do_query($top_points_query);
if($result) {
// I'm assuming you don't want to deduct more points from a row than it has
$num_to_deduct = min($result['points'], $total_to_deduct);
// Now deduct the points from the row we got earlier
$update_query = "UPDATE my_table SET points = points - $num_to_deduct
WHERE id = $result['id']";
if(do_query($update_query)) {
$total_to_deduct -= $num_to_deduct;
}
}
} while($total_to_deduct > 0); // If we still have points to deduct, do it again
Seems like you just need a simple update Statement and allows you to update the row and if it's more than 100 not update it.
update table set points = if( (points+<VALUE>) <= 100,points+<VALUE>,points) where id = <FILE ID>
This will check to see if the points is higher than 100, if it is then the update statement will just return no results. If the value is less than 100, then it will update the table and give you back the amount of rows that were updated.
Just add a column in your user table with the amount of redeemed points. Is that a viable solution for you?
Here is a pure SQL solution, but I warn you that (a) this is untested and (b) it's just a concept.
DECLARE curs CURSOR FOR
SELECT
id,
points,
FROM
points
WHERE
points > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET remPoints = 0;
OPEN curs;
SET remPoints = 100; /* modify this value, probably in your app */
REPEAT
FETCH curs INTO cId, cPoints;
IF remPoints >= cPoints THEN
UPDATE points SET points = 0 WHERE id = cId;
ELSE
UPDATE points SET points = points - remPoints WHERE id = cId;
END IF;
SET remPoints = remPoints - cPoints;
UNTIL remPoints <= 0;
CLOSE curs;