...

View Full Version : php syntax



aspdude2004
05-02-2004, 10:16 PM
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";

}




}
?>

gsnedders
05-02-2004, 10:30 PM
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

aspdude2004
05-02-2004, 10:37 PM
ok where is this extra ; ????




<?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
// 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";

}




}
?>

SDP2006
05-03-2004, 01:53 AM
Yeah where is that extra ';' ?

Anyways
$message = strip_tags($_POST['message'], ''); the last part is kind unecessary. The second part of the function is for allowed tags ie
$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
$message = strip_tags($_POST['message']);Just thought I'd point that out. Good Luck! :thumbsup:

aspdude2004
05-03-2004, 10:14 PM
o yea thanx he is talking bout the ';' at the end of the query string



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

Mhtml
05-03-2004, 10:32 PM
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.

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum