Jump to content

[SOLVED] Unexpected T_Variable on line 1?


sam06

Recommended Posts

I haven't a clue why this is doing this:

 

My code:

<?php

$id = $_GET['id']; 
$username = $_GET['user']; 

mysql_connect("*************") or die(mysql_error());
mysql_select_db("****************") or die(mysql_error());
//gets sweet points
$check = mysql_query("SELECT points FROM sweetshop WHERE id='$id'") or die(mysql_error());
$sweetpoints = mysql_result ($check, 0, 'points');
// gets sweet name
$check1 = mysql_query("SELECT name FROM sweetshop WHERE id='$id'") or die(mysql_error());
$sweetname = mysql_result ($check1, 0, 'name');
//gets points of user
$check = mysql_query("SELECT totalpoints FROM usertable WHERE username='$username'") or die(mysql_error());
$userpoints = mysql_result ($check, 0, 'totalpoints');
//works out new point total
$difference = $userpoints - $sweetpoints;

if ( $userpoints >= $sweetpoints ) {

mysql_query("INSERT INTO sweets
(name, username) VALUES('$sweetname', '$username' ) ") 
or die(mysql_error());  
mysql_query("UPDATE usertable SET totalpoints='$difference' WHERE username='$username'") 
or die(mysql_error());  
echo '<p><font size="4" face="Arial">Thanks for adding a sweet.</font></p>
<p><font size="4" face="Arial"><a href="javascript:history.go(-1)">Click here to
go back.</a></font></p>';
} else {
echo "Not enough points";
echo '<p><font size="4" face="Arial"><a href="javascript:history.go(-1)">Click here to
go back.</a></font></p>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/114127-solved-unexpected-t_variable-on-line-1/
Share on other sites

I cannot see anything wrong with that code. However this is bad programming practice:

$check = mysql_query("SELECT points FROM sweetshop WHERE id='$id'") or die(mysql_error());
$sweetpoints = mysql_result ($check, 0, 'points');
// gets sweet name
$check1 = mysql_query("SELECT name FROM sweetshop WHERE id='$id'") or die(mysql_error());
$sweetname = mysql_result ($check1, 0, 'name');
//gets points of user
$check = mysql_query("SELECT totalpoints FROM usertable WHERE username='$username'") or die(mysql_error());
$userpoints = mysql_result ($check, 0, 'totalpoints');

You are running 3 queries to return the points, name and totalpoints from the sweetshop and usertable tables. MySQL can return multiple columns (which are in different tables) from a single SQL query.

Archived

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

×
×
  • 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.