Log in

View Full Version : Comments box



keyboard
07-27-2011, 09:34 AM
<?php

mysql_connect("localhost", "****", "****") or die('could not connect to database');
mysql_select_db("****") or die('Could not select database');

if (isset ($_POST['submit']))
{
$comment = mysql_escape_string (trim ($_POST['comment']));
$sql = mysql_query ("INSERT INTO comments (id,comments) VALUES ('0','".$comment."')");

echo 'Your comment has been entered successfully!';
}
else
{
// POST data wasnt entered, so display the comments and comment form

// view comments from database
$sql = mysql_query ("SELECT * FROM comments");
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}

echo '<br /><br />
<form action="comments.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
</form>';

?>

I'm trying to set up a comments box on the members section of my website.
2 things-
1- is mysql_escape_string good enough secourity to prevent sql attacks.
2- when i run the script this error comes up

Parse error: syntax error, unexpected T_STRING in /home1/keyboard/public_html/Canberra Amatuer Productions/comments.php on line 6


Any help would be appreciated

bluewalrus
07-27-2011, 02:22 PM
You're missing a quote at
die('Could not select database);.

Should be
die('Could not select database');

The escaping should be good to prevent sql injections.

keyboard
07-28-2011, 12:07 AM
I fixed that up but now it comes up with this error

Parse error: syntax error, unexpected $end in /home1/keyboard/public_html/Canberra Amatuer Productions/comments.php on line 29

Doesn't that mean there is an unclosed {} section?

bluewalrus
07-28-2011, 04:07 AM
That's correct, sorry I missed the missing } after the while before.


else
{
// POST data wasnt entered, so display the comments and comment form

// view comments from database
$sql = mysql_query ("SELECT * FROM comments");
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}// this closes the loop

echo '<br /><br />
<form action="comments.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
</form>';
} // this closes the else
?>

keyboard
07-29-2011, 07:57 AM
<?php

mysql_connect("localhost", "****", "****") or die('could not connect to database');
mysql_select_db("****") or die('Could not select database');

if (isset ($_POST['submit']))
{
$comment = mysql_escape_string (trim ($_POST['comment']));
$sql = mysql_query ("INSERT INTO comments (id,comments) VALUES ('0','".$comment."')");

echo 'Your comment has been entered successfully!';
}
else
{
// POST data wasnt entered, so display the comments and comment form

// view comments from database
$sql = mysql_query ("SELECT * FROM comments");
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}

echo '<br /><br />
<form action="comments.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit">
</form>';
}
?>


I put a submit button in and I filled in the comments text box and cliked submit but nothing happened. I checked the sql database and nothing had been inserted. Any help please?

bluewalrus
07-29-2011, 03:15 PM
<input type="submit" value="Submit" name="submit">

http://php.net/manual/en/reserved.variables.post.php

Your POST will never get the value unless you give the input a name.

keyboard
07-29-2011, 08:39 PM
Thanks so much. I have a working comments box. Now i'm going to try putting some other things in their to. I would like the comments to be displayed with a line in between each of them. I think the code is /r/n but I'm not sure where to put it. any help would be great.

bluewalrus
07-29-2011, 09:06 PM
Change your $comment (line 5?) to this


$comment = nl2br(mysql_escape_string (trim ($_POST['comment'])));

keyboard
07-30-2011, 12:14 AM
Could you please explain to me how this works

JShor
07-30-2011, 12:33 AM
The ASCII code to break lines is \n. It may not look like it, but every line break in any string has an \n at the end of it.

So for example:
Hello, world!
Line 1
Line 2
Line 3

Line 5

Actually looks like:
Hello, world!\n
Line 1\n
Line 2\n
Line 3\n
\n
Line 5\n

The function nl2br() converts \n into the HTML tag, <br /> or <br>
The second argument in the function is a boolean value (true or false) of whether the standard is XHTML or not. XHTML uses <br />, while HTML > 4.01 uses <br>.
http://php.net/nl2br

keyboard
07-31-2011, 09:16 AM
I changed the code but the comments are still displaying like this

Comment1
Comment2
Comment3
Comment4
Comment5

when I would like them to display like this

Comment1

Comment2

Comment3

Comment4

Comment5


Any help would be great

bluewalrus
07-31-2011, 04:10 PM
OO opss, trim takes out line breaks as well. Try it like this


$comment = mysql_escape_string (trim (nl2br($_POST['comment'])));

keyboard
08-04-2011, 07:59 AM
Thankyou, it all works now. I also added in an extra feature so that it puts their username before their post. I have a question. Can you use <b> </b> tags inside php code?

bluewalrus
08-04-2011, 12:54 PM
You can use any html in PHP you just need to enclose it correctly. PHP generates the code (HTML, css, js, etc.) you want. So for example say you wanted your comments bold...


$comment = mysql_escape_string ( '<b>' . trim (nl2br($_POST['comment'])) . '</b>' );

keyboard
08-05-2011, 09:24 AM
Thanks Blue Walrus, One question, why do you have to put in the dots?

keyboard
08-05-2011, 09:28 AM
Also if I wanted to make $username bold how would I put in the <b> tags into this code


$cheese = "$username - $comment ";

bluewalrus
08-05-2011, 10:32 AM
The periods concatenates it together.

I think this should do it...



$cheese = "<b>$username</b> - $comment ";

keyboard
08-05-2011, 10:39 AM
Thanks you blue walrus. I added in the bold tags but it is making the entire thing bold not just $username

keyboard
08-05-2011, 10:42 AM
And one other question. When you enter a new comment into the box it is displayed right down the bottom of all the comments. Is their a way to make the newest comment display at the top? Thankyou everyone, for all your help.

keyboard
08-05-2011, 11:07 AM
Oou, and one more,


$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','$_SERVER['REMOTE_ADDR']';


it's causing this error


( ! ) Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 59

bluewalrus
08-05-2011, 11:47 AM
You need to end the statement


$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')";

order the comments by the date descending


order by date desc

I'd need to see the html output to tell you what is wrong with the bold username.

keyboard
08-05-2011, 09:54 PM
Somewhere on this page its coming up with this error

( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 59



<?php

mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{


}
}
}
else


{
header("Location: login.php");
}



$links = "link2.php";




?>

<?php


if (isset ($_POST['submit']))
{
$comment = mysql_escape_string ( '<b>' . trim (nl2br($_POST['comment'])) . '</b>' );

$quantity = $_POST['quantity'];
$item = $_POST['item'];

$cheese = "<b>$username</b> - $comment ";




$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')";



echo 'Your comment has been entered successfully!';
echo '<a href="echo time.php">Please click here</a>';

}

else
{


?>


<html>
<head>
<body style="background-color:lightgreen">
</head>
<body>
<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
</body>
</html>


I'm pretty sure it's on this line of code

("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')";

But not 100% sure.


How do I organise the comments by date?




This is the comments box which is making the entire line bold



</body>

</html>Hey ----
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<b>----</b> - <b>----</b> <br /><b>----</b> - <b>----</b> <br /><b>----</b> - <b>----</b> <br /><br /><br />
<form action="member.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form></body>
</html>


That's the html output

JShor
08-06-2011, 03:10 AM
Somewhere on this page its coming up with this error

( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 59


I'm pretty sure it's on this line of code

("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')";

But not 100% sure.



Yes, it is. You didn't close the function with a ")" (I know, it can get confusing sometimes if you have multiple parentheses/brackets/curly brackets).


mysql_query("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')");




How do I organise the comments by date?


Use the ORDER BY in your SQL selection query. For example:


SELECT * FROM `comments` WHERE id = 1 ORDER BY date DESC


You can change DESC (descending order) to ASC (ascending order).




This is the comments box which is making the entire line bold



</body>

</html>Hey ----
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<b>----</b> - <b>----</b> <br /><b>----</b> - <b>----</b> <br /><b>----</b> - <b>----</b> <br /><br /><br />
<form action="member.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form></body>
</html>


That's the html output

You already ended your <body> and <html> tags. Legally, you're not supposed to write any HTML after that. You also did this earlier in your code.

Try this:


<?php

mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{


}
}
}
else


{
header("Location: login.php");
}



$links = "link2.php";

if (isset ($_POST['submit']))
{
$comment = mysql_escape_string ( '<b>' . trim (nl2br($_POST['comment'])) . '</b>' );

$quantity = $_POST['quantity'];
$item = $_POST['item'];

$cheese = "<b>$username</b> - $comment ";




$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')";



echo 'Your comment has been entered successfully!';
echo '<a href="echo time.php">Please click here</a>';

}

else
{


?>
<html>
<head>
</head>
<body style="background-color:lightgreen">

<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
</body>
</html>

JShor
08-06-2011, 03:12 AM
Also, not to be picky, but I should mention that you need a doctype. From the looks of it, you're using XHTML standards.

Add this to the very beginning of your HTML code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">

keyboard
08-06-2011, 05:23 AM
I've got multiple </body> </html> because i'm using php include and the page i'm including has the tags on it. I'll fix that up. Also, thanks for the doctype.

JShor
08-06-2011, 03:50 PM
No problem, and regardless of how many includes you have, your final output should be valid HTML, or you may experience display problems.

keyboard
08-06-2011, 09:12 PM
I just changed the bit which is like this

// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
to this



// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM `comments` WHERE id = 1 ORDER BY date DESC") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';


I don't think I did it right because when I ran the edited code the comments didn't show.

keyboard
08-06-2011, 09:17 PM
Try this:


<?php

mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{


}
}
}
else


{
header("Location: login.php");
}



$links = "link2.php";

if (isset ($_POST['submit']))
{
$comment = mysql_escape_string ( '<b>' . trim (nl2br($_POST['comment'])) . '</b>' );

$quantity = $_POST['quantity'];
$item = $_POST['item'];

$cheese = "<b>$username</b> - $comment ";




$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')";



echo 'Your comment has been entered successfully!';
echo '<a href="echo time.php">Please click here</a>';

}

else
{


?>
<html>
<head>
</head>
<body style="background-color:lightgreen">

<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
</body>
</html>


Just tried it and it ran this error

( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\member.php on line 51

keyboard
08-06-2011, 09:36 PM
The comment is now not displaying in bold while their name is, so that's all working. However, now the comments are not displaing with a line inbetween each. I think I might have accidentally changed something. Here is the full code.


<?php

mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{

$links = "link2.php";
}
}
}
else


{
header("Location: login.php");
}








?>

<?php


if (isset ($_POST['submit']))
{
$comment = mysql_escape_string (trim (nl2br($_POST['comment'])));


$quantity = $_POST['quantity'];
$item = $_POST['item'];

$cheese = "<b>$username</b> - $comment ";




$sql = mysql_query ("INSERT INTO comments (id,comments) VALUES ('0','".$cheese."')");



echo '<center>Your comment has been entered successfully!</center>';
echo '<center><a href="member.php"><br />Please click here</a></center>';

}

else
{


?>


<html>
<head>
<body style="background-color:lightgreen">
</head>
<body>
<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}



echo '<br /><br />
<form action="member.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
</body>
</html>




I also have a question. When you use php echo to write something on the page the rest of the page dissapears like in this bit of code


echo '<center>Your comment has been entered successfully!</center>';
echo '<center><a href="member.php"><br />Please click here</a></center>';


What I was wondering, is their a way to keep the background light green while still replacing all of the old content?

JShor
08-06-2011, 10:59 PM
Just tried it and it ran this error

( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\member.php on line 51

You didn't modify the code with the piece I gave you, which is why you're still receiving that syntax error.

Use this:


mysql_query("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')");




However, now the comments are not displaing with a line inbetween each. I think I might have accidentally changed something


I didn't find any code that formats the output of the comments... only the comments that were input into the database.

This should work for separating spaces though:


// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo str_replace("\n", "<br />", $row['comments']).'<br />';
}




I also have a question. When you use php echo to write something on the page the rest of the page dissapears like in this bit of code


What disappears?



What I was wondering, is their a way to keep the background light green while still replacing all of the old content?


The reason that your background is no longer light green is because you defined that in the <body> tag, which you closed. Then you opened a new <body> tag with no background definition. You cannot have multiple <body>, <head> or <html> tags. Also, <body> cannot be in the <head> tags.

Try replacing this:


<html>
<head>
<body style="background-color:lightgreen">
</head>
<body>


With this:


<html>
<head>
</head>

<body style="background-color:lightgreen">


Hope this helps.

keyboard
08-07-2011, 08:06 AM
I think I changed the right bit


<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("keyboard_test") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{


}
}
}
else


{
header("Location: login.php");
}



$links = "link2.php";




?>

<?php


if (isset ($_POST['submit']))
{
$comment = mysql_escape_string ( '<b>' . trim (nl2br($_POST['comment'])) . '</b>' );

$quantity = $_POST['quantity'];
$item = $_POST['item'];

$cheese = "<b>$username</b><br /> $comment ";



$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']"')");





echo 'Your comment has been entered successfully!';
echo '<a href="echo time.php">Please click here</a>';

}

else
{


?>


<html>
<head>
<body style="background-color:lightgreen">
</head>
<body>
<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
</body>
</html>


but it's still coming up with the same error

( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 58

keyboard
08-07-2011, 08:13 AM
I changed one of the bits of code


$cheese = "<b>$username</b><br /> $comment ";
So that it displays their name then on the next line their comment. Like this

Name
Their comment
Name
Their comment
Name
Their comment
Name
Their comment

When I would like it to look like this

Name
Their comment

Name
Their comment

Name
Their comment

Name
Their comment


Any help?

JShor
08-07-2011, 04:01 PM
I think I changed the right bit

but it's still coming up with the same error

( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\echo time.php on line 58

This time you had a period missing in that same line of code.

Use this:

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("keyboard_test") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{


}
}
}
else


{
header("Location: login.php");
}



$links = "link2.php";




?>

<?php


if (isset ($_POST['submit']))
{
$comment = mysql_escape_string ( '<b>' . trim (nl2br($_POST['comment'])) . '</b>' );

$quantity = $_POST['quantity'];
$item = $_POST['item'];

$cheese = "<b>$username</b><br /> $comment ";



$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']."')");





echo 'Your comment has been entered successfully!';
echo '<a href="echo time.php">Please click here</a>';

}

else
{


?>


<html>
<head>
<body style="background-color:lightgreen">
</head>
<body>
<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';
}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
</body>
</html>

JShor
08-07-2011, 04:02 PM
I changed one of the bits of code


$cheese = "<b>$username</b><br /> $comment ";
So that it displays their name then on the next line their comment. Like this

Name
Their comment
Name
Their comment
Name
Their comment
Name
Their comment

When I would like it to look like this

Name
Their comment

Name
Their comment

Name
Their comment

Name
Their comment


Any help?

Can't you just add another line-breaker tag?



$cheese = "<b>$username</b><br /><br /> $comment ";


That should work for you.

keyboard
08-08-2011, 06:39 AM
Yep, that all works now. Thanks for all the help.

keyboard
08-08-2011, 06:48 AM
I ran the code and into the date column it inserted this



2007-08-11


$date = date("d/m/y");
That's what it should be inserting but the date is wrong. it should be 08/08/2011.
Any help?

JShor
08-08-2011, 06:35 PM
What do you mean by inserting? Inserting into MySQL? YYYY-MM-DD is the typical date format for a field that has the type set to DATE.

If that's the case, try changing it to VARCHAR or TEXT or something. You could also insert the date in UNIX time using BIGINT, which is actually a more elegant solution.

keyboard
08-08-2011, 10:55 PM
How do I use unix time?

JShor
08-09-2011, 12:09 AM
http://php.net/time

UNIX time is the number of seconds since the beginning of the UNIX epoch (January 1st, 1970 at midnight Greenwich time).

The time() function returns the time in UNIX time. Store that large number in a BIGINT column in MySQL and when you go to fetch the date, you can convert it with the date() function.

Let's suppose you have the number 1312848370. That is the UNIX time for August 8th, 2011 8:06:24 PM EST. The second argument of the date() function accepts a UNIX timestamp and converts it into a string format.

For example, the following would return 08/08/11


echo date("d/m/y", 1312848370);


For more information, see:
http://php.net/time
http://php.net/date

I should write programming tutorials for a living. ;)

ajfmrf
08-09-2011, 08:22 PM
have seen you would be very good at it.

You explain things quite well

Bud

bluewalrus
08-09-2011, 08:36 PM
I've recently started using strtotime, it's a lot easier to work with I find.

http://php.net/manual/en/function.strtotime.php


$ts = strtotime('1999-11-40');
echo date('Y-m-d', $ts);

keyboard
08-09-2011, 10:07 PM
Thanks Blue Walrus and Jshor. And yes I agree, you should write coding tutorials for a living (but it would be a bit boring don't you think).:cool:

keyboard
08-10-2011, 09:01 AM
<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("keyboard_test") or die(mysql_error());


if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


if ($pass != $info['password'])
{ header("Location: login.php");
}


else
{

$links = "link2.php";
}
}
}
else


{
header("Location: login.php");
}








?>

<?php


if (isset ($_POST['submit']))
{
$comment = mysql_escape_string (trim (nl2br($_POST['comment'])));


$date = time();
$cheese = "<b>$username</b> <br /><br /> $comment <br /><tr /> ";




$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']."')");







echo '<center>Your comment has been entered successfully!</center>';
echo '<center><a href="echo time.php"><br />Please click here</a></center>';

}

else
{


?>


<html>
<head>

</head>
<body>
<?php include("$links");
echo "Hey ".$username;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<p><b>COMMENTS</b></p>
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['comments'].'<br />';

}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
<body style="background-color:lightgreen">
</body>
</html>

That's the entire code for the comments box/ member page. I am trying to display their comments like this.


NAME
DATECOMMENT


but I'm not sure how to. Any help?

JShor
08-10-2011, 04:54 PM
<?php
// POST data wasnt entered, so display the comments and comment form
// view comments from database
$sql = mysql_query ("SELECT * FROM comments") or die(mysql_error());;
while ($row = mysql_fetch_array ($sql)) {
echo $row['name'].'<br />';
echo $row['comments'].'<br />';

}



echo '<br /><br />
<form action="echo time.php" method="post">
Comments:<br />
<textarea name="comment" cols="40" rows="7"></textarea>
<input type="submit" value="Submit" name="submit">
</form>';
}
?>
<body style="background-color:lightgreen">
</body>
</html>


Although I'm not sure what you named the field that contains 'name' in MySQL.

bluewalrus
08-10-2011, 07:42 PM
You'll need to double line break after the name if you want an empty line.


echo $row['name'].'<br /><br />';

keyboard
08-10-2011, 10:09 PM
What about the DATE, on the right hand side?
And I'm pretty sure , with the edited script it's still relying on this code to display the comment.



$date = time();
$cheese = "<b>$username</b> <br /><br /> $comment <br /><tr /> ";




$sql = mysql_query ("INSERT INTO comments (id,comments,date,ip) VALUES ('0','".$cheese."','".$date."','" . $_SERVER['REMOTE_ADDR']."')");

I because when I removed the $comment from $cheese, the comments stopped displaying.

So I need to make a new column in the sql database called name and inserted them sepretly?