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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.