UrbanWolf81
UrbanWolf81

Reputation: 33

add one (+1) to a field in mysql database using php

I have a php script that displays records from a database. It's probably not the best script, as I'm very new to php.

I've added an additional column in my table and would like to keep a count in that column to show me how many times each of the records have been viewed.

Heres the part of the code I think i need to add the code to... if i need to post the entire page i will, but i just figured i could add the line to this part.

//Get the details from previous page
$SelectedCounty = $_POST["result"];

//set variable for next SEARCH
$option = '';

// Get the county names from database - no duplicates - Order A-Z 
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";

// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);


 while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";

}
    }

the new column name is 'views' and i just want to add 1 to it each time a record from the database is viewed.

any help greatly appreciated.

Upvotes: 3

Views: 3808

Answers (5)

Shubham Soin
Shubham Soin

Reputation: 138

As far as i have analysed your code it brings out the following case.

There are different records for tradingConty, and whenever a user views that particular record(one of the tradingCounty record) by clicking that or any other action specified, the php script is set to increament the view count for that particular entry(we can get that by id) in the database.

If thats the scenario, we can easily generate a code accordingly.

Upvotes: 0

Shubham Soin
Shubham Soin

Reputation: 138

You can change your code with this rewritten code assuming that your Table has a column views (datatype int).

//Get the details from previous page
$SelectedCounty = $_POST["result"];

//set variable for next SEARCH
$option = '';

// Get the county names from database - no duplicates - Order A-Z 
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";

// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);

if($result){
$query2 = "UPDATE offers SET views=views+1;
mysqli_query($con,$query2); 
}



 while($row = mysqli_fetch_array($result)) {
 $option .="<option>" . $row['tradingCounty'] . "</option>";

 }

Or if you need to track the view counts for individual records, you need to modify your code a bit. And probably you need to add one more field in the database for eg. id (datatype int) which can distinguish between different records.

Please clear your problem properly.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270011

If you have added the column, it probably has a NULL value. Either set the value to 0, by doing:

update offers
    set views = 0;

Or use:

update offers
    set views = coalesce(views, 0) + 1;

Upvotes: 0

Bobby Tables
Bobby Tables

Reputation: 866

mysqli_query($con,"update offers set views = views + 1");

Upvotes: 2

Pupil
Pupil

Reputation: 23958

Add a new field views to the table.

When, user views the page, fire the SQL.

$query = "UPDATE offers SET views = views + 1";

Upvotes: 6

Related Questions