bartje01 Posted September 12, 2010 Share Posted September 12, 2010 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 More sharing options...
thescientist Posted September 12, 2010 Share Posted September 12, 2010 shouldn't you be using a WHERE clause to find the user to assign the points to? Link to comment Share on other sites More sharing options...
RobberBaron Posted September 12, 2010 Share Posted September 12, 2010 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.');};?> Link to comment Share on other sites More sharing options...
wirehopper Posted September 12, 2010 Share Posted September 12, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.