Jump to content

Double amount value insertion


nitation

Recommended Posts

Hi Gents,

 

I am trying to insert an amount value for a user using an HTML form that will be stored in a database. How do i insert another amount value to that same user in the database and it will be summed up.

The reason am asking this is because, if i will be using the same HTML form to add the 1st and second value, how do i query the database to do the addition.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/
Share on other sites

hello,

 

This query you provided

"UPDATE table_name SET field_name = field_name + {$new_value} WHERE id_field = {$key}"

is for the second amount value. If i add this query alongside my Insert Query in the HTML form, then it will not be possible for me to retrieve the 1st value added. I tried what you gave me before. The reason is because. After adding the 1st and 2nd value, an error might occurred in the second value whereby there will be a need to change. But with the code you provided, i am not gonna be able to get the initial value.

 

I hope you get me right

 

Thanks in advance

Bro,

 

My question is very straight. Let me break it apart. I have an amount field in my database. I wanna insert two records into the amount field using an HTML form. I want the information inserted to be calculated and added together and be outputted for the user to view.

 

How do i go about it.

 

Regards

Are the two values posted at the same time, or is the form re-loaded?

 

In the former case:

settype ($_POST['a'], 'int');
settype ($_POST['b'], 'int');
INSERT INTO table_name set field_name = {$_POST['a']};
INSERT INTO table_name set field_name = {$_POST['b']};
$result = $_POST['a'] + $_POST['b'];
echo $result;

 

Of course, it's bad practice to echo on a POST form result page, it should just redirect somewhere.

 

In the latter case:

 

settype ($_POST['a'], 'int');
INSERT INTO table_name set field_name = {$_POST['a']};
SELECT SUM(field_name) AS Total FROM table_name;
// fetch $row
echo $row['Total'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.