Jump to content

[SOLVED] Parse error: syntax error, unexpected '}' in C:\wamp\www\test\login\insert2.php


Recommended Posts

insert2.php

 

<?php
//ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;
if(!$username) die('username not set');
if(!$password1) die('Password1 not set');
else $password1 = md5($password1);
if(!$password2) die('Password2 not set');
else $password2 = md5($password2);
$query = "SELECT * FROM users WHERE username='$username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());
$count=mysql_num_rows($result);
if($count==1){
session_start();
//session_register("username");
$_SESSION['username'] = $username;
$queryy = "SELECT gold FROM users WHERE username='$username'";
$result1 = mysql_query($queryy);
$row = mysql_fetch_assoc($result1);
$_SESSION['gold'] = $row['gold'];
//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['inventory'];

}
header("location:index2.php");
} 
else
{
echo "Wrong Username or Password";
}
?>

 

 

why is that showing up? my brackets are in the right places..  it says its on line 38 too..

it has something to do with

 

$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['inventory'];

 

because when i change that line to normal i dont get the error....but the reason why i change it is to try to get the inventory thing working but its not working and i dont know what to do about it...ive been worknig on the same thing for 2 days now....

 

check it out..

http://www.phpfreaks.com/forums/index.php/topic,230022.0.html

 

 

ive been working on it and working on it and i think this is it...

$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['inventory'];

 

 

but i get the else error...

but i get the else error...

 

Your getting a parse error. Indent your code so it is readable and you might find it. There is no parse errors in....

 

$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['inventory'];

 

There is a bug however. Your only selecting item in your query yet you expect inventory to be within your $Row array.

i found the bug and fixed it! thxs for noticing that! hopefully it will work once i find the darn parse error..i couldn't find anything wrong...do you think i have some php 4 codes in there? im using php 5 btw..

insert2.php

 

<?php
//ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;
if(!$username) die('username not set');
if(!$password1) die('Password1 not set');
else $password1 = md5($password1);
if(!$password2) die('Password2 not set');
else $password2 = md5($password2);
$query = "SELECT * FROM users WHERE username='$username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());
$count=mysql_num_rows($result);
if($count==1){
session_start();
//session_register("username");
$_SESSION['username'] = $username;
$queryy = "SELECT gold FROM users WHERE username='$username'";
$result1 = mysql_query($queryy);
$row = mysql_fetch_assoc($result1);
$_SESSION['gold'] = $row['gold'];
//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];
  
}

header("location:index2.php");
}
else {
echo "Wrong Username or Password";
}

?>

 

error: Parse error: syntax error, unexpected '}' in C:\wamp\www\test\login\insert2.php on line 39

It took me 5 seconds to find your problem, well the one you are talking about anyway.  Searching your piece of code, you have 2 opening braces '{' and 3 closing braces '}'.  Obviously you have a mismatch which would have been super easy for you to see (and probably wouldn't have done it in the first place) had you properly indented your code.

 

You also have

 

if(condition)

    ...

else ...

else ...

 

You can't have 2 elses like that.

try this code and let me know what happens:

 

<?php
//ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;

if(!$username)
{
echo "username not set";
}
elseif(!$password1)
{
echo "Password1 not set";
}
else
{
$password1 = md5($password1);
}

if(!$password2)
{
echo "Password2 not set";
}
else
{
$password2 = md5($password2);
$query = "SELECT * FROM users WHERE username='$username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());
$count=mysql_num_rows($result);

if($count==1)
{
session_start();
//session_register("username");
$_SESSION['username'] = $username;
$queryy = "SELECT gold FROM users WHERE username='$username'";
$result1 = mysql_query($queryy);
$row = mysql_fetch_assoc($result1);
$_SESSION['gold'] = $row['gold'];
//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item']; 
}
header("location:index2.php");
}
else
{
echo "Wrong Username or Password";
}

?>

You are better off to code in the method that I showed you because it is much more readable. I am sure there is an even better way but I would at least go this far for readability in the future. Makes problem solving easier.

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.