Jump to content

[SOLVED] Update code not updating


tjverge

Recommended Posts

$pagename1=$_GET['member'];
$result = mysql_query("SELECT * FROM members"); 
mysql_query ("UPDATE members SET count = count + 1 WHERE pagename='$pagename1'");

 

This code should update the visit count for the members page but does not

ex: http://url.com/index.php?member=X

 

The member X should go from 0 to 1, but is staying at 0

 

Any help would be great.

Link to comment
https://forums.phpfreaks.com/topic/109634-solved-update-code-not-updating/
Share on other sites

not sure if it will help but here is the who code for the page in question.

 

<code>        <?php

$db_host = "localhost";

$db_user = "xxx";

$db_pwd = "xxx";

$db_name = "viral";

mysql_connect($db_host, $db_user, $db_pwd);

mysql_select_db($db_name);

 

$pagename1=$_GET['member'];

$result = mysql_query("SELECT * FROM members");

mysql_query ("UPDATE members SET count = count + 1 WHERE pagename='$pagename1'");

 

$countgame = mysql_num_rows($result);

echo 'there are, ', $countgame, ' pages in our system';

 

?>

      </p>

      <div align="center">

        <table>

          <td>Name</td>

        <td>Visits</td>

      <?php

$sql = "SELECT * FROM `members` ORDER BY `count` DESC LIMIT 10 ";

$query = mysql_query($sql);

 

while($row = mysql_fetch_array($query)) {

 

$db_link_result = $row['url'];

$db_link_text = $row['pagename'];

$final_link = "<a href=\"" . $db_link_result . "\" >" . $db_link_text . "</a>";

 

 

echo "<tr>";

echo "<td> ",$final_link,"</td>";

echo "<td>".$row['count']."</td>";

echo "</tr>";

}

?></code>

Make sure $_GET['member'] holds the value that you expect. Print it out to test the current value. Also, it is always a good idea to look at mysql_error when a query doesn't work as expected. Change your query to include this at the end:

...pagename1'") or die(mysql_error());

You should have proper error handling. Try this:

 

<?php

$pagename1=mysql_real_escape_string($_GET['member']);

$query = "UPDATE members SET count = count + 1 WHERE pagename='$pagename1'";
mysql_query ($query) or die ("Error:<br>".mysql_error()."<br>Query:<br>$query");

//For testing purposes
echo "Query:<br>$query<br>Rows Affected: ".mysql_affected_rows();

?>

No, don't use $count.  And count is a mysql reserved word I think, because it's a function.  So you need to do `count` = `count` +1 or you can just rename the column.

 

You'd have known this if you just put OR die(mysql_error()) after query....*Sigh*

yes the code now looks like this

 

$pagename1=mysql_real_escape_string($_GET['member']);

 

$query = "UPDATE members SET views = views + 1 WHERE pagename='$pagename1'";

mysql_query ($query) or die ("Error:<br>".mysql_error()."<br>Query:<br>$query");

 

//For testing purposes

echo "Query:<br>$query<br>Rows Affected: ".mysql_affected_rows();

Resource ID is the Result of a mysql_query, are you re-using the $query variable somewhere else in your code?

 

Echoing $query should return "UPDATE members SET views = views + 1 WHERE pagename='$pagename1'"

 

With $pagename1 replaced with $_GET['member']

Darkwater that is the problem, I feel like a fool for missing it thank you so much for finding it :) I did not have the right column name, it works now

 

Thank you to everyone for he help.

 

Heh, simple errors are usually the hardest to find.  Glad I could help.

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.