peranha Posted December 29, 2007 Share Posted December 29, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 29, 2007 Share Posted December 29, 2007 example only this is to add 1 to advcount each time........... $query = "UPDATE " . $pre . "adveruse SET advcount=advcountadd+1 WHERE advname = '$advname'"; Quote Link to comment Share on other sites More sharing options...
peranha Posted December 29, 2007 Author Share Posted December 29, 2007 For some reason it only executes the queryadd part of the form, and doesnt go past that, any suggestions? Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 30, 2007 Share Posted December 30, 2007 Try this: $query = "UPDATE `".$pre."adveruse` SET `advcount` = (advcountadd+1) WHERE `advname` = '$advname'"; Quote Link to comment Share on other sites More sharing options...
peranha Posted December 30, 2007 Author Share Posted December 30, 2007 Thanks, That worked. Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 SEE IF THIS WORKS INTRESTING CHEERS mate...... PS. I NO IT WORKING JUST INTRESTED.. <?PHP $query="UPDATE `".$pre."` adveruse SET `advcount`=advcountadd+1 WHERE `advname` = '$advname'"; ?> Quote Link to comment Share on other sites More sharing options...
peranha Posted December 30, 2007 Author Share Posted December 30, 2007 No, it comes with table does not exist with your code. Quote Link to comment Share on other sites More sharing options...
peranha Posted December 30, 2007 Author Share Posted December 30, 2007 <?php $query="UPDATE `".$pre."` adveruse SET `advcount`=advcountadd+1 WHERE `advname` = '$advname'"; ?> Should be this instead. <?php $query="UPDATE `".$pre." adveruse` SET `advcount`=advcountadd+1 WHERE `advname` = '$advname'"; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 Thank u wasent that far off then m8.... Quote Link to comment 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.