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 6 of 6

Thread: php syntax

  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php syntax

    please can you tell me what is wrong with this : : : : it doesnt display : -


    <?php
    session_start(); //start session
    // You just need to configure these 4 variables to match your server.
    $db_host = "localhost"; // mySQL database host
    $db_user = "***"; // mySQL database user
    $db_password = "*******"; // mySQL database password
    $db_name = "****"; // the name of your mySQL database
    if($_POST['submit']) {
    // 1. Validate it, by checking all the form inputs were filled in
    $users = $_SESSION['username'];
    $email = $_SESSION['email_address'];
    if(!$_POST['message']) {
    echo 'Error ! : No message entered';
    die;
    }
    // 2. Strip unwanted HTML

    $message = strip_tags($_POST['message'], '');

    $author = $users;


    // 3. Make sure messages and names aren't too long


    $message_length = strlen($message);
    $author_length = strlen($author);
    if($message_length > 500) {
    echo "Error ! : Your message was too long, messages must be less than 500 chars";
    die;
    }
    if($author_length > 500) {
    echo "Error ! : Your name was too long, names must be less than 500 chars";
    die;
    }


    // 4. Add it to our database.
    // If the script hasn't died yet due to an error in the inputted data
    // we need to add the data to the database
    // Lets connect to our database.



    mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());



    // Select the database.


    mysql_select_db($db_name) or die(mysql_error());



    // Lets define the date format we want to enter to our database
    // go here for more details
    // http://www.php.net/manual/en/function.date.php


    $date = date("h:i A dS M");


    // This will produce 11:02 25th Aug
    // Set the query as $query


    $query = "INSERT INTO shoutbox (message, author, email, date, ip)
    VALUES ('$message','$author','$email','$date','$_SERVER[REMOTE_ADDR]')";
    mysql_query($query);
    mysql_close();


    // Show thanks message and take them back to the main shoutbox
    ?>
    <script>
    window.location="shoutbox.php"
    </script>
    <?
    // If they haven't submitted a post, we want to :
    // 1. Show the latest shouts
    // 2. Show the shout post form
    } else {
    // 1. Show the latest shouts
    // Lets connect to our database.
    mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
    // Select the database.
    mysql_select_db($db_name) or die(mysql_error());
    // Set the query as $query, and get the last 6 posts.
    $query = "SELECT message, author, email, date, ip
    FROM shoutbox order by id DESC LIMIT 6";
    $result = mysql_query($query);
    echo "<table border='2' align='center' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#000000' width='100%'>";

    while($r=mysql_fetch_array($result))
    {
    // To modify the appearance, edit this :
    echo "
    <TR>

    <TD><font size='3'>
    <b><font face='Arial'> </font></font><font face='Arial'><font size='2'>
    <A HREF=\"mailto:$r[email]\">
    $r[author]</A> says:</b></font></font><font face='Arial' size='2'></font>
    <font size='2' color='red' face='Comic Sans MS'><b><p align='center'>$r[message]</p></b></font>
    </TR>";

    }
    echo "</TABLE>";
    // 2. Show the shout post form
    ?>

    <p align='center'><font face='Arial' color='#0000FF'><b>
    <?
    if(!$_SESSION['first_name']) {

    echo "You must be logged in to write a message!<br><a href=\"login_form.html\">Log In</a> | <a href=\"join_form.html\">Register</a>";

    } else {
    echo "
    <FORM METHOD=POST ACTION='shoutbox.php'><br>
    <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='142' align='center' id='AutoNumber1'>
    <TR>
    <TD width='51'><b><font size='1' face='Arial' color='#0000FF'>Message :</font></b></TD>
    <TD width='142'><INPUT TYPE='text' NAME='message' size='17'></TD>
    </TR>
    <TR>
    <TD width='51'>
    <p align='center'><font face='Comic Sans MS' color='#0000FF'><b>TiG</b></font></TD>
    <TD width='142'><p align='center'><INPUT TYPE='submit' name='submit' value='Post!'></p></TD>
    </TR>

    </TABLE>
    </FORM>
    <a href="logout.php">Log Out</a> | Change Password";

    }




    }
    ?>
    Last edited by aspdude2004; 05-02-2004 at 10:21 PM.
    ...not bad for a thirteen year old

  • #2
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Maybe there's an extra ; causing the MySQL command to terminate, and, put your PHP in &#91;PHP] and &#91;/PHP] tags next time, might help me to find your problem.

    ------------------------
    Not all to bad for a 12 year old
    Last edited by gsnedders; 05-02-2004 at 10:32 PM.

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok where is this extra ; ????

    PHP Code:

    <?php 
    session_start
    (); //start session
    // You just need to configure these 4 variables to match your server. 
    $db_host "localhost"// mySQL database host 
    $db_user "root"// mySQL database user 
    $db_password "triadpass"// mySQL database password 
    $db_name "mydb"// the name of your mySQL database 
    if($_POST['submit']) { 
    // 1. Validate it, by checking all the form inputs were filled in 
    $users $_SESSION['username'];
    $email $_SESSION['email_address'];
        if(!
    $_POST['message']) { 
            echo 
    'Error ! : No message entered'
            die; 
        }
    // 2. Strip unwanted HTML 

      
    $message strip_tags($_POST['message'], ''); 
        
        
    $author $users


    // 3. Make sure messages and names aren't too long 


        
    $message_length strlen($message); 
        
    $author_length strlen($author); 
        if(
    $message_length 500) { 
            echo 
    "Error ! : Your message was too long, messages must be less than 500 chars"
            die; 
        } 
        if(
    $author_length 500) { 
            echo 
    "Error ! : Your name was too long, names must be less than 500 chars"
            die; 
        } 


    // 4. Add it to our database. 
    // If the script hasn't died yet due to an error in the inputted data 
    // we need to add the data to the database 
    // Lets connect to our database. 



        
    mysql_connect($db_host,$db_user,$db_password) or die(mysql_error()); 



    // Select the database. 


        
    mysql_select_db($db_name) or die(mysql_error()); 



    // Lets define the date format we want to enter to our database 
    // go here for more details 
    // [url]http://www.php.net/manual/en/function.date.php[/url] 


        
    $date date("h:i A dS M"); 


    // This will produce 11:02 25th Aug 
    // Set the query as $query 


        
    $query "INSERT INTO shoutbox (message, author, email, date, ip) 
    VALUES ('$message','$author','$email','$date','$_SERVER[REMOTE_ADDR]')"

        
    mysql_query($query); 
        
    mysql_close(); 


        
    // Show thanks message and take them back to the main shoutbox 
    ?>
    <script>
    window.location="shoutbox.php"
    </script>
    <?
    // If they haven't submitted a post, we want to : 
    // 1. Show the latest shouts 
    // 2. Show the shout post form 
    } else { 
    // 1. Show the latest shouts 
    // Lets connect to our database. 
        
    mysql_connect($db_host,$db_user,$db_password) or die(mysql_error()); 
    // Select the database. 
        
    mysql_select_db($db_name) or die(mysql_error()); 
    // Set the query as $query, and get the last 6 posts. 
        
    $query "SELECT message, author, email, date, ip 
    FROM shoutbox order by id DESC LIMIT 6"

        
    $result mysql_query($query); 
        echo 
    "<table border='2' align='center' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#000000' width='100%'>"

        while(
    $r=mysql_fetch_array($result))     
        { 
    // To modify the appearance, edit this : 
            
    echo "
    <TR> 
             
    <TD><font size='3'> 
    <b><font face='Arial'> </font></font><font face='Arial'><font size='2'> 
    <A HREF=\"mailto:$r[email]\">
    $r[author]</A> says:</b></font></font><font face='Arial' size='2'></font> 
        <font size='2' color='red' face='Comic Sans MS'><b><p align='center'>$r[message]</p></b></font> 
            </TR>"

             
        } 
        echo 
    "</TABLE>"
    // 2. Show the shout post form 
    ?> 
        
    <p align='center'><font face='Arial' color='#0000FF'><b>
    <?
    if(!$_SESSION['first_name']) {

    echo 
    "You must be logged in to write a message!<br><a href=\"login_form.html\">Log In</a> | <a href=\"join_form.html\">Register</a>";
     
    } else { 
    echo 
    "
    <FORM METHOD=POST ACTION='shoutbox.php'><br>
    <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='142' align='center' id='AutoNumber1'>
           <TR> 
            <TD width='51'><b><font size='1' face='Arial' color='#0000FF'>Message :</font></b></TD> 
            <TD width='142'><INPUT TYPE='text' NAME='message' size='17'></TD> 
           </TR> 
        <TR> 
            <TD width='51'>
            <p align='center'><font face='Comic Sans MS' color='#0000FF'><b>TiG</b></font></TD> 
            <TD width='142'><p align='center'><INPUT TYPE='submit' name='submit' value='Post!'></p></TD> 
        </TR> 
        
        </TABLE> 
        </FORM>
    <a href="
    logout.php">Log Out</a> | Change Password";

    }


        


    ?>
    ...not bad for a thirteen year old

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah where is that extra ';' ?

    Anyways
    PHP Code:
      $message strip_tags($_POST['message'], ''); 
    the last part is kind unecessary. The second part of the function is for allowed tags ie
    PHP Code:
    $message strip_tags($_POST['message'],'<b><i><u><span><etc>'); 
    so, unless you're going to allow some HTML, just take out the last part to make
    PHP Code:
    $message strip_tags($_POST['message']); 
    Just thought I'd point that out. Good Luck!
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    o yea!!! :P

    o yea thanx he is talking bout the ';' at the end of the query string

    PHP Code:
     $query "SELECT message, author, email, date, ip
    FROM shoutbox order by id DESC LIMIT 6;"

    ...not bad for a thirteen year old

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    it's always a good idea to cover all bases when error checking, stick a die(mysql_error()) on that mysql_query() call! Will save time debugging in the future.

    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

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