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
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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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*

Link to comment
Share on other sites

ok I tried to change count col to views, had no effect when I added the error checking it comes back

 

UPDATE members SET views = views + 1 WHERE pagename='tjverge'

Rows Affected: 0

 

so the information is right just not updating.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

I would personally think you'd have to set a variable first?

 

$views = mysql_query("SELECT views FROM members WHERE pagename='tjverge'";

$views++;

$query = mysql_query("UPDATE members SET views='$views' WHERE pagename='tjverge'";

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

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.

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.