1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Error in php code...help me

Discussion in 'PHP' started by swiminsoda, Aug 24, 2011.

  1. #1
    <?php
    $con = mysql_connect("host","username","paswd")…
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("users",$con);
    $sql = 'INSERT INTO `reg` (`name`, `email`, `state`, `city`, `bg`, `age`, `ph`) VALUES (`$_POST[name]`,`$_POST`,`$_POST[…

    if (!mysql_query($sql,$con))
    {
    echo "Error : " . mysql_error();
    }
    $n=$_POST[name];
    $e=$_POST[email];
    $st=$_POST[state];
    $bg=$_POST[bg];
    $age=$_POST[age];
    $ph=$_POST[ph];
    echo "Ur details are: \n";
    echo "name: ".$n."\n";
    echo "email: ".$e."\n";
    echo "state: ".$st."\n";
    echo "blood group : "+$bg+"\n";
    echo "age: ".$age."\n";
    echo "ph: ".$ph."\n";
    echo "Thanku for registering here.....we will contact you when somebody is in need.";
    mysql_close($con);
    ?>
    <html>
    <head></head>
    <body>
    </body>
    </html>


    i wrote this code in php...but its showing error like this..?
    Parse error: syntax error, unexpected $end in /home/www/host.com/thanku2.php on line 37

    ........anybody help me..what is $end...i have never used %end in my code...bt y is it happening?
     
    swiminsoda, Aug 24, 2011 IP
  2. Chuckun

    Chuckun Well-Known Member

    Messages:
    1,161
    Likes Received:
    60
    Best Answers:
    2
    Trophy Points:
    150
    #2
    The line
    $sql = 'INSERT INTO `reg` (`name`, `email`, `state`, `city`, `bg`, `age`, `ph`) VALUES (`$_POST[name]`,`$_POST[email]`,`$_POST[…
    Code (markup):
    is incomplete.. That would definitely throw an error..
     
    Chuckun, Aug 24, 2011 IP
  3. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #3
    Think the ... means he snipped some code. Does the same on the mysql_connect() line as well. Pretty much has to be one of those two lines since the rest of the code looks ok.

    Just wondering why the extra typing? And really need to use mysql_real_escape_string. And little point in outputing the HTML tags since text is already being sent. Plus need to an else from the mysql error check.

    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("demos",$con);
    
    
    
    $n=$_POST[name];
    $e=$_POST[email];
    $st=$_POST[state];
    $bg=$_POST[bg];
    $age=$_POST[age];
    $ph=$_POST[ph];
    
    $sql = "INSERT INTO users (name, email, state, city, bg, age, phone) VALUES ('$n','$e','$st','','$bg','$age','$ph')";
    
    if (!mysql_query($sql,$con))
    {
    echo "Error : " . mysql_error();
    }
    echo "Your details are: \n";
    echo "name: ".$n."\n";
    echo "email: ".$e."\n";
    echo "state: ".$st."\n";
    echo "blood group : "+$bg+"\n";
    echo "age: ".$age."\n";
    echo "ph: ".$ph."\n";
    echo "Thanks for registering here.....we will contact you when somebody is in need.";
    
    mysql_close($con);
    ?>
    PHP:
     
    shallowink, Aug 24, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Use a text editor with brace matching ('{' and '}' are braces). Almost every time, unexpected end means that you forgot to close a pair of braces (or parentheses), so the PHP parser fell off the end of the file looking for it.
     
    Rukbat, Aug 24, 2011 IP
  5. shaun.php1208

    shaun.php1208 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    $sql = 'INSERT INTO `reg` (`name`, `email`, `state`, `city`, `bg`, `age`, `ph`) VALUES ('".$_POST[name]."','".$_POST."','".$_POST[…


    shaun
     
    shaun.php1208, Aug 25, 2011 IP
  6. Junioreality

    Junioreality Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    It means that, somewhere, you have forgotten to close a bracket }.
    example:
    function test ()
    {
    ....
    function fox ()
    {
    }
    ==> the first function is not closed, hence function fox (and all the rest of the code) is parsed as being PART of function test... right up to the last line of code (</html>)!
    (your line 37 is the END OF FILE)

    But there are a few other "errors" in your code:
    1. $n=$_POST[name];
    => $n=$_POST['name']; (you must enclose the "name" in single quotes,
    2. All your php is before the headers:
    Make the lot as FUNCTIONS in the header, ie:
    <header>
    <?php
    function reply($_POST)
    {
    $con = mysql_connect...
    ...
    mysql_close($con);
    }
    ?>
    </header>
    Then call that function in your <body>
    <body>
    <?php
    echo (reply($_POST));
    ?>
    </body>

    3. if (!mysql_query($sql,$con))
    {
    echo "Error : " . mysql_error();
    ... is not a secure form.
    Use
    $res = mysql_query ($sql) or die (mysql_error());
    or
    mysql_query($sql);
    if (mysql_error())
    return/echo (mysql_error());

    4. $sql = 'INSERT INTO `reg` (`name`, `email`, `state`, `city`, `bg`, `age`, `ph`) VALUES (`$_POST[name]`,`$_POST`,`$_POST[…
    (we do not have the end, so I do not know if it has been correctly closed).
    The correct syntax is:
    $sql = " insert into `reg` (`name`,`... )
    values ( ' " . $_POST [ ' name ' ] . " ' , ' ... ) ";
    (Spaces added for clarity)
     
    Junioreality, Aug 25, 2011 IP