Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    unexpected T_string

    What is wrong with this insert. This is the error:
    Parse error: syntax error, unexpected T_STRING in /home3/simplic5/public_html/php/index.php on line 48

    This is the code:
    PHP Code:
            <?php
            
    if (isset($_POST['submit'])){
                if (
    $_POST['submit'] == 'save'){
                
    query ('INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, \'$_POST['name'] \', \'$_POST['age']\', \'$_POST['email']\', \'$_POST['comment']\'');
        }
        }
            
                    
            
    ?>
    I know that you guys hear this all the time. I got ahold of some video tuts and I typed this as instructed but I can not find the problem, I have double checked a thousand times. I read that it could be missing a line terminator( or double quotes.

    So you will know what I have learned. This statement should be saying that if the form isset then if submit is save then insert the arrays into the table called james. The line that starts with query if line 48.
    Last edited by jmr3460; 03-04-2009 at 02:12 AM.

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Your query is "terminating" prematurely as you're not escaping the single quotes within the $_POST[]s

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I thought that that was what the backslashes were for.

  • #4
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I tried something.

    PHP Code:

            <?php
            
    if (isset($_POST['submit'])){
                if (
    $_POST['submit'] == 'save'){
                
    query ('INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, '$_POST['name\']''$_POST[\'age\']''$_POST[\'email\']''$_POST[\'comment\']')
        }
        }
            
                    
            
    ?>
    And I got a different error:

    Parse error: syntax error, unexpected T_VARIABLE in /home3/simplic5/public_html/php/index.php on line 48

  • #5
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Try this:

    PHP Code:
    <?php
            
    if (isset($_POST['submit'])){
                if (
    $_POST['submit'] == 'save'){
                
    query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, '$_POST['name']', '$_POST['age']', '$_POST['email']', '$_POST['comment']");
        }
        }       
    ?>
    Using " is the same as ' and since you have ' where post is, you should use " for the outer one (sorry if I'm confusing)
    Your also forgot the ; after the query()

    BTW: query is function right?
    Not mysql_query right?
    Last edited by sea4me; 03-04-2009 at 02:39 AM.

  • #6
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes query is been made a function with a statement that goes like this:
    PHP Code:
    function query($mysql_query){


    I added the double quotes and the semicolon and now I get this error.
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/simplic5/public_html/php/index.php on line 48

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    What I meant in my post above is something like this:
    PHP Code:
    query ('INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, \'$_POST[\'name\'] \', \'$_POST[\'age\']\', \'$_POST[\'email\']\', \'$_POST[\'comment\']\''); 
    Besides the opening and closing quote for the whole query, you have to escape every other quotes in-between. Though honestly, I've never written a query like that so I'm not even sure if it works or not :P

    My preferred way of writing queries:
    PHP Code:
    query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, 
    '"
    .$_POST['name']."', 
    '"
    .$_POST['age']."', 
    '"
    .$_POST['email']."', 
    '"
    .$_POST['comment']."'); 
    Broke the query into multiple lines so you can see the quotes properly; gets kinda confusing sometimes.

  • #8
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    shadowmaniac: you forgot to end your " before )

    PHP Code:
    query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL,  
    '"
    .$_POST['name']."',  
    '"
    .$_POST['age']."',  
    '"
    .$_POST['email']."',  
    '"
    .$_POST['comment']."'"); 

  • #9
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Quote Originally Posted by jmr3460 View Post
    Yes query is been made a function with a statement that goes like this:
    PHP Code:
    function query($mysql_query){


    I added the double quotes and the semicolon and now I get this error.
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/simplic5/public_html/php/index.php on line 48
    Did you just add the " and the ; ?

    Replace the whole code with
    PHP Code:
    <?php
            
    if (isset($_POST['submit'])){
                if (
    $_POST['submit'] == 'save'){
                
    query ("INSERT INTO james (uid, name, age, email, comment) VALUES (NULL, '$_POST['name']', '$_POST['age']', '$_POST['email']', '$_POST['comment']");
        }
        }       
    ?>

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by sea4me View Post
    shadowmaniac: you forgot to end your " before )
    Indeed I did; glad I mentioned it gets confusing too *whistles*

  • Users who have thanked shadowmaniac for this post:

    jmr3460 (03-04-2009)

  • #11
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hey thanks I had to add a double at the end and that took care of my error. I now need to find out why I can't add to my table. Thank you for the help!!!!!!!!!!!!!!! Thanks to both.
    Last edited by jmr3460; 03-04-2009 at 03:20 AM.

  • #12
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Your welcome!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •