Jump to content

+1 to row in database


michytttt

Recommended Posts

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 . . .

Link to comment
https://forums.phpfreaks.com/topic/239809-1-to-row-in-database/
Share on other sites

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?

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.

 

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'];

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.