Jump to content

Recommended Posts

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>";
	}
?>

Link to comment
https://forums.phpfreaks.com/topic/75088-solved-onclck-update-mysql-field/
Share on other sites

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.

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);
?>

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?

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

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.