marksie1988 Posted October 28, 2007 Share Posted October 28, 2007 i have a script (below) when a user clicks on a link i want it to update a field in a database +1 so that i can see how many times it is clicked. but i cant get it to update the field what am i doing wrong? <?php $query = "SELECT * FROM directory ORDER BY pop"; $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { $id = html_entity_decode($row['id']); $title = html_entity_decode($row['title']); $www = html_entity_decode($row['www']); $pop = html_entity_decode($row['pop']); $desc = html_entity_decode($row['description']); echo"<table width='100%' border='0' class='panel'> <tr> <td width='50%'><A HREF=\"$www\" onClick=\"UPDATE directory SET pop = pop+1 WHERE www = '$www' \" target='_blank'>$title</A> </td> <td width='50%' align='right'>$pop </td> </tr> </table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/ Share on other sites More sharing options...
GingerRobot Posted October 28, 2007 Share Posted October 28, 2007 Things don't work like that. PHP is a server side language. By the time things get to the browser, it has been parsed. You cannot perform an onlick event in that way; you must make another request. That request can either be in the form of another page load, or an AJAX request, which would work in the way you want it to. The first method is much easier however; you would link to another page, upate the database, and forward the user onto the page in the URL. Quote Link to comment https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/#findComment-379762 Share on other sites More sharing options...
marksie1988 Posted October 28, 2007 Author Share Posted October 28, 2007 ok then thanks ill give this a go and get back to tell you if it worked Quote Link to comment https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/#findComment-379763 Share on other sites More sharing options...
marksie1988 Posted October 28, 2007 Author Share Posted October 28, 2007 i have changed it so that onclick it will load dirupdate.php?link=$title but when the link is clicked the script still doesnt work i think it is the dirupdate.php page here is the script <table width='100%' border='0' class='panel'> <tr> <td>Website</td> <td align='right'>Popularity</td </tr> <?php $query = "SELECT * FROM directory ORDER BY pop"; $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { $id = html_entity_decode($row['id']); $title = html_entity_decode($row['title']); $www = html_entity_decode($row['www']); $pop = html_entity_decode($row['pop']); $desc = html_entity_decode($row['description']); echo" <tr> <td width='50%'><A HREF=\"$www\" onClick=\"dirupdate.php?link=$title \" target='_blank'>$title</A></td> <td width='50%' align='right'>$pop</td> </tr> "; } ?></table> dirupdate.php <?php include("../login/include/session.blc"); if(ctype_digit($_GET['link'])) $link_id = $_GET['link']; else $link_id = 0; $query = "UPDATE `directory` SET `pop` = (pop +1) WHERE `title` = '" . $link_id . "'"; $result = mysql_query($query); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/#findComment-379774 Share on other sites More sharing options...
GingerRobot Posted October 28, 2007 Share Posted October 28, 2007 First off, you actually need to send the user to dirupdate.php - not have it as onclick attribute. Second, you seem to pass the title of the website in the url, but then use the ctype_digit() function on it. Surely the title is not numeric? You would be better off using the ID - something which should be unique. Try this as your first page: <table width='100%' border='0' class='panel'> <tr> <td>Website</td> <td align='right'>Popularity</td </tr> <?php $query = "SELECT * FROM directory ORDER BY pop"; $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { $id = html_entity_decode($row['id']); $title = html_entity_decode($row['title']); $www = html_entity_decode($row['www']); $pop = html_entity_decode($row['pop']); $desc = html_entity_decode($row['description']); echo '<tr><td width="50%"><A HREF="dirupate.php?id='.$id.'&www='.urlencode($www).'" target="_blank">'.$title.'</A></td><td width="50%" align="right">'.$pop.'</td></tr>'; } ?> </table> And this as dirupdate.php: <?php include("../login/include/session.blc");//i assume this is your database connection? $id = (int)$_GET['id']; //type casting to ensure it is an integer $www = urldecode($_GET['www']); $query = "UPDATE `directory` SET `pop` = (pop +1) WHERE `id` = $id"; $result = mysql_query($query) or die(mysql_error()); header("location:$www");//forward the user to the destination ?> Oh, and finally, any reason for all the html_entity_decodes? Quote Link to comment https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/#findComment-379778 Share on other sites More sharing options...
marksie1988 Posted October 28, 2007 Author Share Posted October 28, 2007 yes the session.blc is the db connection and the reason for all the html_entity_decode's is because thats the only way that i know how to display the data, i dont know if it is actually needed or not but i am fairly new to php Quote Link to comment https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/#findComment-379780 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.