Question:
PHP/MySQL What do I need to change to get this code working?
2010-06-29 18:17:55 UTC
I'm trying to set up a very basic point system for my site and this is the code I'm working with.

require_once('connect.php'); //require a database connection, to include the selection of the database.
$user = trim($_GET['user']);
if(isset($user)) { //if we have a user.
$user = mysql_real_escape_string($user); //escape them for mysql interaction.
if(isset($_GET['points']) && ($_GET['points'] == 'increase' || $_GET['points'] == 'decrease')) { //if the url has 'points' set, and the = one of two things.
}
if($_GET['points'] == 'increase') { //increase the count.
$update = 'count + 1';
}
elseif($_GET['points'] == 'decrease') { //decrease the count.
$update = 'count - 1';
}
$sql = "UPDATE counter SET count = $update WHERE user_id = '$user'"; //finalize query.
mysql_query($sql); //execute query.
}
$increasePoints = 'Increase'; //increase points link set to variable.
$decreasePoints = 'Decrease'; //decrease points link set to variable.
//Call the data from the db.
$sql = "SELECT count FROM counter WHERE user_id = '$user'";
$re = mysql_query($sql);
echo mysql_errno() . ": " . mysql_error() . "\n";
$r = mysql_fetch_row($re);
$count = $r[0];
//echo the count and the variables. You can do this on the current page, or any page this code is included in.
echo 'User has ' . $count . ' points!
Do you want to ' . $increasePoints . ' or ' . $decreasePoints . '?
';
?>

However, I keep getting this message.

1054: Unknown column 'user_id' in 'where clause'
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/webvilla/public_html/test/logger/count.php on line 23

Does anybody know what I need to do in order to get this code operating?
Three answers:
2010-06-29 18:25:01 UTC
Change user_id in



$sql = "UPDATE counter SET count = $update WHERE user_id = '$user'";



to the real name of the field. There's no field in counter named user_id.
RiggsFolly
2010-06-29 18:29:33 UTC
This error is saying that there is no column in the count table called user_id.



Check the definition of the count table to get the correct column name and change it on both the SQL statements.
crady
2016-10-17 06:50:23 UTC
Ah, I see what the subject may well be, you're making use of the "@" blunders suppressing image yet you're no longer telling the script what to do if there is an blunders along with your connection, the 'internet site' must be the database call quite additionally. the link I positioned below will help you, it has the line of code you're lacking. that's what you ought to have @ $db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); if (mysqli_connect_errno()) { echo 'would desire to no longer connect'; go out; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...