Shoutbox returning date for both date and month field..

Hey guys.

Having a strange problem(at least I find it strange, you guys will probably pick it right up). I have a shoutbox running at my site, which is supposed to return time/date and time from PHPs

Here is the code for adding content to the box, as well as displaying it below.

[code]

Shoutbox

Name:    


Message:


<?php

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die(“Unable to select database”);

$query=“SELECT * FROM ShoutBox ORDER BY user-id DESC LIMIT 5”;
$result=mysql_query($query);
$num=mysql_numrows($result);

while($r=mysql_fetch_array($result))

{

$time=$r[“time”];

$id=$r[“user-id”];

$message=$r[“message”];

$name=$r[“user-name”];

?>







<?php $i++; } echo "

<?php echo $message;?>

<?php echo $name;?>
<?php echo $time;?>
"; ?>[/code]

And here is the PHP that’s supposed to work the magic.
[php]<?php

mysql_connect (“localhost”, “”, “”)
or die (‘I cannot connect to the database.’);
mysql_select_db ("")

or die(mysql_error(‘I can not select the table.’));
echo $name;
$name=$_POST[‘name’];
echo $message;
$message=$_POST[‘message’];

$time=date(“h:ia d/j/y”);
echo $time;
echo $result;

$result=MYSQL_QUERY(“INSERT INTO ShoutBox VALUES (‘NULL’,’$name’, ‘$message’,’$time’)”);

$result = mysql_query(“select * from ShoutBox order by user-id desc limit 5”);

?>[/php]

I’m not really sure how to further troubleshoot this problem as I can’t see a reason why PHPs date(“h:ia d/j/y”) doesn’t work. I am a bit puzzled at it saying d(for day?) j(not sure what this is) and y(for year?) though. Please advise if available :slight_smile:

You’ve echo’d out variables in several cases before the’ve been designed:

[php]echo $name;
$name=$_POST[‘name’];[/php]
And:
[php]echo $message;
$message=$_POST[‘message’];[/php]
And:
[php]$time=date(“h:ia d/j/y”);
echo $time;[/php]
And:
[php]echo $result;
$result=MYSQL_QUERY(“INSERT INTO ShoutBox VALUES (‘NULL’,’$name’, ‘$message’,’$time’)”);
$result = mysql_query(“select * from ShoutBox order by user-id desc limit 5”);[/php]

You need to define a variable before you use it. This is how the first example I put above should be:

[php]$name=$_POST[‘name’];
echo $name;[/php]

Hey jSherz!

Thank you for replying :slight_smile:

I removed the echoes completely(though I don’t think they modify the variables?) and the problem remains.

The way it’s now being parsed(as I’m not allowed to add images) is:

[message3]
[user3][04:11pm 31/31/11]

[message2]
[user2][07:32am 26/26/11]

[message1]
[user1][01:59pm 23/23/11]

echo doesn’t modify variables. Is the problem MySQL not storing the date? If you want to store date inside of a DATE field in MySQL, you need to use this syntax:

[php]date(“Y/m/d”)[/php]

If you want to know what the parts of date do, you can always look at the php manual.

There you go, the values of the date field said “day”/“Day without 0”/“2 digit year”.

Thank you for the help Jsherz :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service