Icewolf Posted April 3, 2013 Share Posted April 3, 2013 Hi I want to say thank you all for your help. I have one more question. How do I add a field from a form to a field in mysql database. What I want is if the field on the form has 200 and the field in the database has 200 then the field updates to 400 when I hit the submit button. What I am not sure of do I need to query the database first and then add them together? Here is what I have for now. $query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = ('$reward1' + Reward_1), Reward_2 = ('$reward2' + Reward_2), Reward_3 = ('$reward3' + Reward_3) WHERE Member_ID = '$memid'"; $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/ Share on other sites More sharing options...
Jessica Posted April 3, 2013 Share Posted April 3, 2013 Set Field = field+200 is a valid update. Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422767 Share on other sites More sharing options...
Icewolf Posted April 4, 2013 Author Share Posted April 4, 2013 What happens is a person enters a value to a text box on a form and then what I want to do is just add that value to what is already in the database. Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422789 Share on other sites More sharing options...
Jessica Posted April 4, 2013 Share Posted April 4, 2013 And as I said. The query "UPDATE `table` SET `column`=`column`+200 WHERE `id`=1" is a perfectly good query. You don't need to select it first. Just update it. Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422792 Share on other sites More sharing options...
Yohanne Posted April 4, 2013 Share Posted April 4, 2013 use variable to catch-up your 200 value Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422793 Share on other sites More sharing options...
Icewolf Posted April 4, 2013 Author Share Posted April 4, 2013 Thank you for your response I understand what you are saying. Maybe I didn't say what I need to correct way. Right now when the person goes to this webpage I have for text boxes where the user can put in any number in. From that I want to take the value from the webpage and add it to the value that is already in the database. I was just saying the 200 as an example. Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422806 Share on other sites More sharing options...
topcat Posted April 4, 2013 Share Posted April 4, 2013 (edited) The above response is still the way to go just replace the 200 with the value from the form field:$value = $_POST['the_id_of_the_formfield']; or however you retrieve the value from the form. I would do some checking here to make sure that the value really is an integer to avoid sql injection attacks. such as: if(!is_int($value)){ show a messsage to user saying it must be an integer and exit" } otherwise add it to the existing database field$sql = "UPDATE `table` SET `column`=`column`+ $value WHERE `id`=1"; (obviously you would have to dynamically set the id.hope that helps Edited April 4, 2013 by topcat Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422813 Share on other sites More sharing options...
Jessica Posted April 4, 2013 Share Posted April 4, 2013 Thank you for your response I understand what you are saying. Maybe I didn't say what I need to correct way. Right now when the person goes to this webpage I have for text boxes where the user can put in any number in. From that I want to take the value from the webpage and add it to the value that is already in the database. I was just saying the 200 as an example.Did you try using a little logic to look at what I posted and what you posted, and figure out how to USE what I posted? Quote Link to comment https://forums.phpfreaks.com/topic/276507-how-to-add-values/#findComment-1422862 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.