Parse error: syntax error, unexpected T_VARIABLE

Discussion in 'PHP' started by neil_12s, Aug 5, 2009.

?

Can anyone teach me php programing free of charge.

  1. 6

    0 vote(s)
    0.0%
  2. 7

    0 vote(s)
    0.0%
  1. #1
    Dear All I need you help. I would be very grateful if you can help me. When I try run the following PHP program I get the above error message.

    ie. Parse error: syntax error, unexpected T_VARIABLE

    My program is

    <html>
    <head>
    <title>Adding User Input to a Database</title>
    </head>
    <body>
    <?php
    if ( isset ( $fname ) && isset ($lname) && isset ($email) )
    {
    // check user iput here !
    $dberror = " ";
    $ret= ad_to_database ( $fname, $lname, $email, $dberror );
    if ( ! $ret )
    print "Error: $dberror<BR> ";
    else
    print "thank you very much ";
    }
    else {
    write_form() ;
    }

    function add_to_database ( $fname, $lname, $email, &$dberror )
    {
    $user = "jmbecoo4" ;
    $pass = "PaZpjqRj" ;
    $db = "jmbecoo4_mail" ;
    $link = mysql_connect ( "localhost", $user , $pass );
    if (! $link )
    {
    $dberror="Couldn't connect to MySQL server ";
    return false ;
    }
    if ( ! mysql_select_db ( $db , $link ) )
    {
    $dberror =mysql_error () ;
    return false;
    }
    $query ="INSERT INTO person (fname, lname, email )
    values ('$fname', '$lanme', '$email ' ) ";
    if ( ! mysql_query ( $query, $link ) )
    {
    $dberror = mysql_error () ;
    return false;
    }
    return true;
    }

    function write_form()
    {
    global $PHP_SELF;
    print "<form action=\ "$PHP_SELF \"method=\ " POST \ "> \ n";
    print "Your First Name <p> \ n" ;
    print "<input TYPE=\ "text \ " name= \ " lname \ ">";
    print "Your Last Name <p> \ n" ;
    print "<input TYPE=\ "varchar \ " name =\ "email \ ">";
    Print "your email address<p> \ n ";
    print "<input TYPE=\ "submit \ " value=\ "submit ! \ " >\ n</form>\ n ";
    print "<input type=\"text\ " name=\ " fname\ ">";
    }

    ?>
    </body>
    </html>

    Please help me to find the error.

    Wilfred
     
    neil_12s, Aug 5, 2009 IP
  2. 4chp

    4chp Peon

    Messages:
    163
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If I were you I'd remove your personal database pass info, regardless if you haven't gave the site link yet.

    Other than that I'd try placing an ; just after
    $query ="INSERT INTO person (fname, lname, email )
    Code (markup):
    so that is reads
    $query ="INSERT INTO person (fname, lname, email );
    Code (markup):
     
    Last edited: Aug 5, 2009
    4chp, Aug 5, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Damn, where did you learn PHP? :) Lots of accolades missing, and other small errors..

    
    <html>
    <head>
    <title>Adding User Input to a Database</title>
    </head>
    <body>
    <?php
    if (isset($fname) && isset ($lname) && isset ($email)) {
    	// check user iput here !
    	$dberror = " ";
    	$ret = add_to_database ( $fname, $lname, $email, $dberror );
    	if (!$ret) {
    		print "Error: $dberror<BR> ";
    	} else {
    		print "thank you very much ";
    	}
    } else {
    	write_form() ;
    }
    
    function add_to_database ( $fname, $lname, $email, &$dberror ) {
    	$user = "xxx" ;
    	$pass = "xxx" ;
    	$db = "jmbecoo4_mail" ;
    	$link = mysql_connect ( "localhost", $user , $pass );
    	if (! $link ) {
    		$dberror="Couldn't connect to MySQL server ";
    		return false ;
    	}
    	if (!mysql_select_db ( $db , $link)) {
    		$dberror =mysql_error () ;
    		return false;
    	}
    
    	$query = 'INSERT INTO person (fname, lname, email ) values ("' . mysql_real_escape_string($fname) . '", "' . mysql_real_escape_string($lanme) . '", "' . mysql_real_escape_string($email) . '")';
    	if ( ! mysql_query ( $query, $link ) ) {
    		$dberror = mysql_error () ;
    		return false;	
    	}
    	return true;
    }
    
    function write_form() {
    	global $PHP_SELF;
    	print '<form action="' . $PHP_SELF . '" method="POST">' . "\n";
    	print 'Your First Name <p>' . "\n" ;
    	print '<input TYPE="text" name="lname">';
    	print 'Your Last Name <p>' . "\n" ;
    	print '<input TYPE="varchar" name="email">';
    	Print 'your email address<p>' . "\n";
    	print '<input TYPE="submit" value="submit!">' . "\n" . '</form>' . "\n";
    	print '<input type="text " name="fname">';
    }
    ?>
    </body>
    </html>
    PHP:
     
    premiumscripts, Aug 5, 2009 IP
  4. neil_12s

    neil_12s Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Dear Friends,

    Thanks a lot for replying to my querry. I have sent you two private mails. Hope you will receive them. If not please mail me back.

    Thanks.

    Wilfred.
     
    neil_12s, Aug 5, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this code instead, but really, we are not here to do all of your work. You really should start learning PHP yourself..

    <html>
    <head>
    <title>Adding User Input to a Database</title>
    </head>
    <body>
    <?php
    if (!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email'])) {
        $dberror = " ";
        $ret = add_to_database ( $_POST['fname'], $_POST['lname'], $_POST['email'], $dberror );
        if (!$ret) {
            print "Error: $dberror<BR> ";
        } else {
            print "thank you very much ";
        }
    } else {
        write_form() ;
    }
    
    function add_to_database ( $fname, $lname, $email, &$dberror ) {
        $user = "xxx" ;
        $pass = "xxx" ;
        $db = "jmbecoo4_mail" ;
        $link = mysql_connect ( "localhost", $user , $pass );
        if (! $link ) {
            $dberror="Couldn't connect to MySQL server ";
            return false ;
        }
        if (!mysql_select_db ( $db , $link)) {
            $dberror =mysql_error () ;
            return false;
        }
    
        $query = 'INSERT INTO person (fname, lname, email ) values ("' . mysql_real_escape_string($fname) . '", "' . mysql_real_escape_string($lanme) . '", "' . mysql_real_escape_string($email) . '")';
        if ( ! mysql_query ( $query, $link ) ) {
            $dberror = mysql_error () ;
            return false;   
        }
        return true;
    }
    
    function write_form() {
        global $PHP_SELF;
        print '<form action="' . $PHP_SELF . '" method="POST">' . "\n";
        print '<p>Your First Name </p><p><input type="text " name="fname"></p>' . "\n";
        print '<p>Your Last Name </p><p><input TYPE="text" name="lname"></p>' . "\n";
        Print '<p>your email address</p><p><input TYPE="varchar" name="email"></p>' . "\n";
        print '<p><input TYPE="submit" value="submit!"></p>' . "\n" . '</form>' . "\n";
    }
    ?>
    </body>
    </html>
    PHP:
     
    premiumscripts, Aug 6, 2009 IP
  6. neil_12s

    neil_12s Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Dear Sir,

    I very much appreciate your kindness in sending a reply to my mail. The edited php code worked well. There was small error. I was able to corect it. Sorry if I disturbed your busy schedule.

    Please allow me to relate you my side of the story. I am using the book name "SAMS TEACH YOURSELF - PHP4 in 24 hours" By Matt Zandstra Published by Techmedia.

    What I tried to implement was the Listing 12.3 at Page 200. What I sent you in my Thread was this program.

    So now thanks to you I will find a better book to learn PHP than what I am using now.

    So Thank you again. Please pardon me for any inconveniency caused.

    Thanks

    Wilfred.
     
    neil_12s, Aug 6, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No problem :) I was a little harsh there. Just trying to get people to learn PHP instead of posting all their problems here :)

    However, you are learning PHP so that's a good thing. I hope you get a better book and one that is updated for PHP5. I believe there are a few posts in this forum with book recommendations. You will not learn PHP in 24 hours as your book suggests :)
     
    premiumscripts, Aug 6, 2009 IP
  8. neil_12s

    neil_12s Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    Thanks for the prompt reply. I was little bit worried about arousing your feelings. Now I am relieved as you are happy.

    I will go through and find a suitable book. I hope to contact you later for any difficult problem.

    Thanks.

    Wilfred Cooray from Sri Lanka.
     
    neil_12s, Aug 6, 2009 IP
  9. 4chp

    4chp Peon

    Messages:
    163
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Glad you got it sorted. :D
     
    4chp, Aug 6, 2009 IP