Jump to content

Help please


Recommended Posts

Hey guys. I want that when you enter this page you'll get 1 point.But when I enter that page it just adds a new user.my code:

<?php session_start();$username = $_SESSION['username'];$points = $_SESSION['points'];$connect = mysql_connect("localhost","root","");mysql_select_db("phplogin");$pointsinsert = mysql_query("INSERT INTO users (points) VALUES ($points+1)" )  ;if($_SESSION['username']){echo "You've gained a point! <br>";echo "You know have". $pointsinsert."points";//$points +1;}elsedie("You must be logged in. Click <a href='index.php'>here</a> to login.");?>

Link to comment
Share on other sites

You should be using update, not insert. Your code will be inserting a new user and only assigning the value of points. You want something like:

UPDATE users SET points = $points+1 WHERE username = '$username'

Oh, and this:

echo "You know have". $pointsinsert."points";

Should be:

Echo('You now have '.$pointsinsert.' points');

So basically:

<?phpsession_start();$username=$_SESSION['username'];$points=$_SESSION['points']+1;$connect=mysql_connect('localhost', 'root', '');mysql_select_db('phplogin', $connect);$pointsinsert=mysql_query('UPDATE users SET points = '.$points.' WHERE username = \''.$username.'\'');If ($_SESSION['username']) {	 Echo('You've gained a point!<br />');	 Echo('You now have '.$points.' points.');}Else{	 Die('You must be logged in. Click <a href="index.php">here</a> to login.');};?>

Edited by RobberBaron
Link to comment
Share on other sites

Add a backslash to avoid a syntax error

Echo('You\'ve gained a point!<br />');

Also - coding styles are just that - styles, they are equivalent representations of the same command, and either approach is fine.

<?phpecho 'Test1'.PHP_EOL;echo('Test1'.PHP_EOL);Echo('Test1'.PHP_EOL);ECHO('Test1'.PHP_EOL);

All do the same thing.http://www.php.net/manual/en/function.echo.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...