Hello, I'm trying to let users know how many times there personal page is viewed. Here is some code.

$colname_Recordset1 = "-1";
if (isset($_GET['user_name'])) {
  $colname_Recordset1 = $_GET['user_name'];

  mysql_query("UPDATE content SET counter = counter + 1 WHERE user_name = $colname_Recordset1");

It works if I remove the WHERE clause but it adds 1 to all rows so the WHERE clause should specify the particular row but it does not work at all. Any thoughts?

7 Years
Discussion Span
Last Post by phpDave

Is the user_name column in your table a number or a varchar? If it is a varchar, you need to surround it with single quotes in your SQL query, like so:

mysql_query("UPDATE content SET counter = counter + 1 WHERE user_name = '$colname_Recordset1' ");

Edited by darkagn: n/a


Thank you, I tried it and still not working, any other suggestions?


Can you try this code and let us know the result:

$colname_Recordset1 = "-1";
if (isset($_GET['user_name']))
   $colname_Recordset1 = $_GET['user_name'];
$result = mysql_query("SELECT * FROM content WHERE user_name = '$colname_Recordset1' ");
if (!$result)
   echo mysql_error();

Hi, error is: Unknown column 'user_name' in 'where clause'

The user_name is in a table named users not the content table but I used LEFT JOIN in the page I want counted.


In that case you need to left join in your update query too. Basically the where clause needs to be able to identify the column that you are filtering on. Try a select statement first to select the exact row you want, then adjust it to an update query without changing your joins and where clause and you should have no trouble.


Thank you! I actually just tried something similar and it worked. Been stuck all day, thanks again! greatly appreciated.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.