Jump to content

[SOLVED] How to add to count


peranha

Recommended Posts

I have a table with 3 fields in it id, advname, advcount.  The id is autoincrement, the advname is taken from the website, and the advcount is also taken.  When I run my script instead of adding to the the count, it inserts a new row with the number 1 in it.

 

<?php
//open connection
$connection = mysql_connect($server, $user, $pass) or die ("Unable to connect!");

//select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM " . $pre . "adver WHERE advuse = '1' ORDER BY rand() LIMIT 1";

// get results of query above
$result = mysql_fetch_array(mysql_query($query));

$advadd = $result['advadd'];
$advurl = $result['advurl'];
$advname = $result['advname'];

echo "<a href='".$advurl."'>" . $advadd . "</a>";

// close connection
mysql_close($connection);
?>

<?php

//open connection
$connection = mysql_connect($server, $user, $pass) or die ("Unable to connect!");

//select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM " . $pre . "adveruse WHERE advname = '$advname'";

// get results of query above
$result = mysql_fetch_array(mysql_query($query));

if (mysql_num_rows($result) < 1 ) {
//add advertisement to database for counting
$queryadd = "INSERT INTO " . $pre . "adveruse (advname, advcount) VALUES ('$advname', '1')";

//add advertisement to database for counting
$resultadd = mysql_query($queryadd) or die ("Error in query: $queryadd. ".mysql_error());
}

else {
// Enter count into a variable
$advcount = $result['advcount'];

//add 1 to the count
$advcountadd = $advcount[0]++;

// create query
$query = "UPDATE " . $pre . "adveruse SET advcount='$advcountadd' WHERE advname = '$advname'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
}

// close connection
mysql_close($connection1);
?>

Link to comment
https://forums.phpfreaks.com/topic/83653-solved-how-to-add-to-count/
Share on other sites

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.