Jump to content

Recommended Posts

So I was going through my headache of a script trying to figure out why it's not working.

 

My script is trying to grab a user_id from the counter table I created in my database, yet I don't hold any user_id's in my counter table they're held in my user table. Would it be possible to make it so this script just grabs the user's name from my user table and just get rid of the "counter" table altogether?

 

Also, can someone tell me where this script is holding the points...? that's what I still can't for the life of me understand. I didn't set anything up to actually store points nor did I run into anyone telling me I needed to throughout all the help I've gotten creating this but I can't imagine I don't have to.

 

Any help is greatly appreciated, I wouldn't be nearly as far along with this if it weren't for this forum.

 

Edit: Might help if I included the script... =P

 

<?php
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 = '<a href="?points=increase">Increase</a>'; //increase points link set to variable.
$decreasePoints = '<a href="?points=decrease">Decrease</a>'; //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 . '?
';
?>

Whats in the counter table? If its just a single "points" amount for each user, I'd just add the column to the user table. Maybe if you explain the goals of your scripts and table I could better understand.

 

There's nothing in the counter table except for something named "count" and "user_id" neither of which were set up to do anything except have the name so the script would have something to access.

 

I've been trying to create a point system for my users, so that if they recommend another person or do something they can gain points. This script was given to me and slightly modified with the help of other people, but I can't get it working nor do I know how to set the MySQL database for it.

 

If you know a better way of doing this then please feel free to let me know because based on responses when I try to get help with this I don't think this is the best way to be doing it. =P

Well if it is just one number for each user, i would add a column to the user table called 'points' or whatever.

 

If you want detailed information about points, then use another table. For example, why each point was given and when.

 

But if you just want to get it working, i did notice that your code doesn't specify which user to give or subtract points from. You have $user coming from $_GET[user], but your increase/decrease links don't have the 'user' in the url.

Well if it is just one number for each user, i would add a column to the user table called 'points' or whatever.

 

If you want detailed information about points, then use another table. For example, why each point was given and when.

 

But if you just want to get it working, i did notice that your code doesn't specify which user to give or subtract points from. You have $user coming from $_GET[user], but your increase/decrease links don't have the 'user' in the url.

 

It just needs to be numbers, every time a user is given points I want it to increase by 100 but I don't know how to go about doing this. The $_GET[user] was my attempt to display user points for the user logged in. I don't know what I'm doing at all, I think I'm going to try to figure out a different way to do this because the version I have is just extremely sloppy.

 

I just want three things right now.

 

1. Ability to store a number of points unique to the visiter (I imagine this is done in MySQL)

 

2. Ability to display the points of the logged in user on their user info page

 

3. Ability to have that number increase based on the submission of a form when their username is entered into the textbox (I'll worry about subtracting points later)

 

But creating this system seems a lot more complicated than I would've suspected. =\

Alright so first of all, you want to use sessions for user information. At the top of each script, you should use session_start().

<?php 
session_start(); 

// the rest of your code below....
?>

 

When a user logs in, store their user id and probably their username in the session

<?php
//after your login script...
$fetch = mysql_fetch_row($query);
$_SESSION[user_id] = $fetch[user_id];
$_SESSION[username] = $fetch[username];
?>

 

This will work for the user who is logged in..... Now I'm assuming you dont want users to give themselves points, so you have to have some way of selecting a user on the form that only you see. I'd put this in a form with the points you want to add.

<?php
$sql = "SELECT * FROM user_table";
$query = mysql_query($sql);
while ($fetch = mysql_fetch_array($query)) {
    $users[] = "<option value=\"$fetch[user_id]\">$fetch[username]</option>\n";
}
$options = implode('',$users);

echo "
<form method=\"get\">
    User:
    <select name=\"user\">
        $options
    </select><br />
    
    Points:
    <input type=\"text\" name=\"points\" /><br />

    <input type=\"submit\" value=\"Change Points\">
</form>
";
?>

 

 

then have a script that recieves this information and sends it to the database

<?php
if ($_GET[user_id]) {
$sql = "UPDATE user_table SET points = (points + $_GET[points]) WHERE user_id = '$_GET[user_id]";
mysql_query($sql);
}
?>

 

 

I haven't actually tested this code but I hope it can point you in the right direction.

Alright so first of all, you want to use sessions for user information. At the top of each script, you should use session_start().

<?php 
session_start(); 

// the rest of your code below....
?>

 

When a user logs in, store their user id and probably their username in the session

<?php
//after your login script...
$fetch = mysql_fetch_row($query);
$_SESSION[user_id] = $fetch[user_id];
$_SESSION[username] = $fetch[username];
?>

 

This will work for the user who is logged in..... Now I'm assuming you dont want users to give themselves points, so you have to have some way of selecting a user on the form that only you see. I'd put this in a form with the points you want to add.

<?php
$sql = "SELECT * FROM user_table";
$query = mysql_query($sql);
while ($fetch = mysql_fetch_array($query)) {
    $users[] = "<option value=\"$fetch[user_id]\">$fetch[username]</option>\n";
}
$options = implode('',$users);

echo "
<form method=\"get\">
    User:
    <select name=\"user\">
        $options
    </select><br />
    
    Points:
    <input type=\"text\" name=\"points\" /><br />

    <input type=\"submit\" value=\"Change Points\">
</form>
";
?>

 

 

then have a script that recieves this information and sends it to the database

<?php
if ($_GET[user_id]) {
$sql = "UPDATE user_table SET points = (points + $_GET[points]) WHERE user_id = '$_GET[user_id]";
mysql_query($sql);
}
?>

 

 

I haven't actually tested this code but I hope it can point you in the right direction.

 

Yes, this is a point in the right direction but I'm still confused as to what my MySQL database would need to have in it for this script.

 

This is how my database is set up.

 

webvilla_users (5) <-- database name

 

active_guests <-- database tables

active_users

banned_users

counter

users

 

The "users" holds all the user names. 

 

"Counter" just has a field named "count" and one named "user_id" both of which are just int(11) with nothing else added to them.

 

Do I need to adjust my database to fit this script?

add a column 'count' to the users table. Then in the script I gave, put 'count wherever I put 'points'. Or you could just name the new column 'points'.

 

Thanks for all your help ejaboneta, I'm pretty sure I'm making progress but still having tons of trouble.

 

I added "points" to the table "users" in database "users". Now I tried plopping all that script together but it doesn't seem to be working.

 

Here's the code:

<?php
//after your login script...
$fetch = mysql_fetch_row($query);
$_SESSION[user_id] = $fetch[user_id];
$_SESSION[username] = $fetch[username];
?>

<?php
$sql = "SELECT * FROM user_table";
$query = mysql_query($sql);
while ($fetch = mysql_fetch_array($query)) {
    $users[] = "<option value=\"$fetch[user_id]\">$fetch[username]</option>\n";
}
$options = implode('',$users);

echo "
<form method=\"get\">
    User:
    <select name=\"user\">
        $options
    </select><br />
    
    Points:
    <input type=\"text\" name=\"points\" /><br />

    <input type=\"submit\" value=\"Change Points\">
</form>
";
?>

<?php
if ($_GET[user_id]) {
$sql = "UPDATE user_table SET points = (points + $_GET[points]) WHERE user_id = '$_GET[user_id]";
mysql_query($sql);
}
?>

 

And these are the errors I'm getting

 

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/webvilla/public_html/test/logger/tinker2.php on line 9

 

Parse error: syntax error, unexpected T_VARIABLE in /home/webvilla/public_html/test/logger/tinker2.php on line 10

 

In my users table the user id is set up as "userid" instead of "user_id" should I change everything that says "user_id"? the "user_id" is in the table "counter" which I created to work with the script.

Whenever you see an error that contains a line number, then you should look at that line for a problem. Most of the time, in my experience at least, it can be a misplaced quotation mark or some other character.  Can you tell me what that line says?

And yes, you can change the column or table names to fit your script.

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.