nitation Posted July 3, 2008 Share Posted July 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/ Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 Something like the following? "UPDATE table_name SET field_name = field_name + {$new_value} WHERE id_field = {$key}" Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580809 Share on other sites More sharing options...
nitation Posted July 3, 2008 Author Share Posted July 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580829 Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 I'm not sure I get you, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580835 Share on other sites More sharing options...
nitation Posted July 3, 2008 Author Share Posted July 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580838 Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 You already have a thread open for this, why'd you create another one? And if addition is this hard, you really need to get back to basics. Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580844 Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580846 Share on other sites More sharing options...
.josh Posted July 3, 2008 Share Posted July 3, 2008 What you need is 2 tables with a 1 to many relationship. http://www.phpfreaks.com/tutorial/data-joins-unions Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580855 Share on other sites More sharing options...
nitation Posted July 3, 2008 Author Share Posted July 3, 2008 @TransmogriBenno The form is reloaded whenever i need to add the second value. Quote Link to comment https://forums.phpfreaks.com/topic/113076-double-amount-value-insertion/#findComment-580863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.