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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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