michytttt Posted June 19, 2011 Share Posted June 19, 2011 Hello everyone, I think what I am hoping to do should be fairly simple, although I a struggling getting the code correct. I have a row in a database table called stats and another one called id - as well as a load of other rows which are irrelevant to what I am trying to achieve here. What I am hoping to do is that when a specific id is selected from the database the stats number is what ever it was orginally +1. Any suggestions? The part of the code I am refering to is below: $sql = "SELECT email, title FROM emails WHERE id='$id'"; $result = mysql_query($sql);while ($row = mysql_fetch_assoc($result)) { $to = $row['email']; $job = $row['title']; $row['stats'] == + 1; //this is the part that I know is completely wrong! everything else works fine. Just need this to say plus one onto the orginal number.... } MOD EDIT: corrected BBCode tags . . . Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/ Share on other sites More sharing options...
Pikachu2000 Posted June 19, 2011 Share Posted June 19, 2011 Are you trying to update the record, or just echo the value + 1? Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231844 Share on other sites More sharing options...
michytttt Posted June 19, 2011 Author Share Posted June 19, 2011 Hi, I am trying to update the record in the database Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231846 Share on other sites More sharing options...
Pikachu2000 Posted June 19, 2011 Share Posted June 19, 2011 Then you need to run an UPDATE query to do so. UPDATE table SET field = field + 1 WHERE . . . Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231849 Share on other sites More sharing options...
michytttt Posted June 19, 2011 Author Share Posted June 19, 2011 Sorry I am a bit confused...would I add this to the same bit of code? Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231852 Share on other sites More sharing options...
Pikachu2000 Posted June 19, 2011 Share Posted June 19, 2011 The query you currently have is a SELECT query, and as such it won't change anything in the table. What you need is an UPDATE query using the syntax above. Can you explain more about what you're trying to accomplish overall? Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231853 Share on other sites More sharing options...
michytttt Posted June 19, 2011 Author Share Posted June 19, 2011 Oh right I see. yes I currently have a form that is sent to a specific email address. The email address is selected from the database - depending on what the id is. This all works fine. What I would like to now add is everytime an email is sent to one of the email address from the database the stats is +1 so that I can track how many emails have been sent to each address. Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231855 Share on other sites More sharing options...
Pikachu2000 Posted June 19, 2011 Share Posted June 19, 2011 OK. You'll need to run both a SELECT and an UPDATE query in that case. After the mail has been successfully sent to the MTA, run the UPDATE query to increment the value of the field in the DB. Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231857 Share on other sites More sharing options...
michytttt Posted June 19, 2011 Author Share Posted June 19, 2011 Ok I think this is starting to make sense to me I have this so far: if($_SERVER['REQUEST_METHOD']== 'POST'){ $sql2 = UPDATE emails SET stats = stats + 1 WHERE id='$id'"; Is this looking right? Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231860 Share on other sites More sharing options...
michytttt Posted June 19, 2011 Author Share Posted June 19, 2011 It seems to be working! The database is adding one however I get this error now when I submit the form: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/60/7908160/html/apply-for-job5.php on line 280 The code is as follows: if($_SERVER['REQUEST_METHOD']== 'POST'){ $sql2 = "UPDATE emails SET stats = (stats + 1) WHERE id='$id'"; $result2 = mysql_query($sql2);while ($row = mysql_fetch_assoc($result2)) { $stats = $row['stats']; } Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231870 Share on other sites More sharing options...
michytttt Posted June 19, 2011 Author Share Posted June 19, 2011 Ok got it! $sql2 = "UPDATE emails SET stats = (stats + 1) WHERE id='$id'"; $result2 = mysql_query($sql2); Thanks for your help it would have taken me ages otherwise! Quote Link to comment https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/#findComment-1231872 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.