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. Quote Link to comment 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 Quote Link to comment 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'); Quote Link to comment 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 Quote Link to comment 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 >"; } } } ?> Quote Link to comment 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.......... Quote Link to comment 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; ?> Quote Link to comment 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.