Jump to content

Help please


bartje01

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.');};?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...