Jump to content

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.

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.