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?

Recommended Answers

All 6 Replies

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' ");

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();
else
   print_r(mysql_fetch_assoc($result));

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.