TheStalker Posted February 14, 2008 Share Posted February 14, 2008 Hi i want to know how to add a set value to an existing value sotred in an sql table when a button is clicked on a php website. Can anyone point me in the right direction at all with a bit of code? many thanks. Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/ Share on other sites More sharing options...
php_dave Posted February 14, 2008 Share Posted February 14, 2008 Sorry - I dont understand.. Do you mean you have a value stored in a table for example Value 20 and want to be able to increment this value or make and adition to this value in the table.. If so - i think the way I would do it would be to pull the current value and then add the change in PHP then update the field in mysql but you may be able to do this directly with something like Update my_table SET value = value + addition This is not tested though HTH Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/#findComment-466849 Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 Also, if the filed you wish to add to contains text you can use mysql's COCAT function. UPDATE tbl SET fld = CONCAT(fld,'more text'); Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/#findComment-466861 Share on other sites More sharing options...
TheStalker Posted February 14, 2008 Author Share Posted February 14, 2008 yes i have a value in a table for example 0.00 and want to add say 0.45 to the value in the table each time a button is clicked Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/#findComment-466864 Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 A simple example. <form method="post"> <input type="text" name="row"> <input type="submit" name="submit" value="add"> </form> <?php if (isset($_POST['submit'])) { // connect to db. $row = mysql_real_escape_string($_POST['row']); $sql = "UPDATE tbl SET fld = fld+1 WHERE id = $row"; if (mysql_query($sql)) { if (mysql_affected_rows()) { echo "$row updated</br >"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/#findComment-466867 Share on other sites More sharing options...
TheStalker Posted February 14, 2008 Author Share Posted February 14, 2008 thanks for all the replys ill explain in more detail ( i prob should have from the start) i have a table called accounts that has customerID, Surname, newspaper and AmountOwing when i click a button i want to have the value of a newspaper added to AmountOwing e.g if newspaper = "sun" +0.35 AmountOwing else if newspaper ="Times" +0.45 AmountOwing etc.......... Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/#findComment-466944 Share on other sites More sharing options...
TheStalker Posted February 14, 2008 Author Share Posted February 14, 2008 <?php $result = mysql_query("SELECT * FROM accounts"); while($row = mysql_fetch_array($result)) $value = $row['AmountOwing']; echo $value; if (['newspaper']="Sun"); { +0.35 $value; } else if (['newspaper']="Times"); { +0.45 $value; } echo $value; ?> Link to comment https://forums.phpfreaks.com/topic/91089-adding-a-vaule-to-an-existing-value-in-a-sql-table/#findComment-466972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.