multiply data from database to value from textbox - php

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

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

checkbox inputs and DB search

I'm tagging Movies and store this into a Database.
a tag_id could be a car, train, boat and details could be color or specific types.
DB
movie_id | tag_id | tag_detailsid
2612 | 75 | 1
2612 | 10 | 3
2612 | 12 | 2
The tags are submitted via a form with checkboxes (all checkboxes checked are added to the db)
Now..
How do I keep track with checkboxes I uncheck..at a later stage
So looking at the above example.. for movie_id 2612, I uncheck tag 12 with id 2 as details.
So $_POST holds only 75-1 and 10-3....12-2 should be deleted from dB.
So I thought.. I simply go through the dB with a while loop and compare the db value with the values I get from the checkboxes (via the $_Post method)..
I count the values in the $_Post and it shows 2 (checkboxes checked).
Database however holds 3 entries ($numberofrecords) for that specific Movie.
My script so far...
$sql_query = "Select tag_id, tag_details_id FROM movie_tags WHERE movie_id = '2612";
$report = MYSQL_QUERY($sql_query);
$numberofrecords = mysql_num_rows ($report);
while ($report_row = mysql_fetch_array($report))
{
$thistag_id = $report_row['tag_id'];
$tag_details_id = $report_row['tag_details_id'];
foreach($_POST as $checkbox_id => $value)
{
if ($thistag_id == $checkbox_id) // check if DB tag equals checkbox value
{
echo "checkbox value is checked equals value in DB";
}
}
}
How do I track the record I need to delete from the database?
Sometimes the easiest solution is the way to go.
Unless you have a compelling reason NOT to, you can simply delete all of the tag records before saving, then insert only those that were checked:
sample code using OP's selected db driver
$sql = 'DELETE FROM movie_tags WHERE movie_id=2612';
mysql_query($sql);
foreach($_POST as $checkbox_id => $value) {
// save your records here
// To show you the code I'd need to see the HTML for the checkboxes.
}
NOTE: You should not be using mysql_. Use PDO / mysqli instead: Why shouldn't I use mysql_* functions in PHP?
A simple solution would be to delete every rows based on your primary key such as movie_id in your case and do insert in loop for every checked checkboxes. That is:
Delete rows based on movie_id
Loop through POST data and do insert

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'";
}
?>

UPDATE command based on radio button selection

ok so I have a table called voting with 3 columns (idnum, name, numvotes). I created radio buttons in an array based on the number of rows in this table, all with the same name (name="preference") I need to update the numvotes field ( add 1) depending on the radio button selected. Cant seem to get this working with the following code...
<?php
$uquery = "update voting set numvotes='". ($_POST['preference'] + 1) ."' where idnum=" .$_POST['idnum'];
$uresults = mysql_query( $uquery);
?>
You are adding 1 to the value returned for $_POST['preference'], not +1 on the value in the database. You also are setting the value as a string, not a number due to the single quotes.
Assuming $_POST['preference'] is the current vote count:
$uquery = "update voting set numvotes=". ($_POST['preference'] + 1) ." where idnum=".$_POST['idnum'];

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