Page 1 of 1

Inserting MySQL Data Twice

Posted: Sun Jan 28, 2007 10:16 am
by tail
I'm trying to create a one file shoutbox that can show and post shouts. However, I'm having some difficulties inserting the data. The data gets inserted twice. Also, when I view the shouts, only the newest row from the table is displayed. Heres the code:

Code: Select all

<?php
$username="**";
$password="**";
$database="**";
$host="**";
$name = $_POST['name'];
$message = $_POST['message'];
$time = date("n/d/y h:ia");
$ip = $_SERVER['REMOTE_ADDR'];

if(isset($_POST['posted'])) {
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO shoutbox VALUES ('','$ip','$name','$time','$message')";
mysql_query($query);
mysql_close();

echo "<br><br><center>Your shout has been posted, <a href=\"index.php?id=shouts&page=shoutbox&act=posted\">click here</a> to view it.</center>";
} elseif(!isset($_POST['posted'])) {
echo "<center><form method='POST' action='index.php?id=shouts&page=shoutbox'>
Name: <input type='text' name='name' class='input' style=\"width:110px;\"><br>
Message: <br><textarea name='message' class='input' cols='10' rows='3'>Put your shout here</textarea><br><br>
<input type='hidden' name='posted' value='true'>
<input type='submit' value='Shout!' class='input' style=\"width:110px;\"></center>";
} elseif($_GET["act"] == "posted") {
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM shoutbox";
$result=mysql_query($query2);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$message=mysql_result($result,$i,"message");
$time=mysql_result($result,$i,"time");
echo "<h2><center>Shouts</center></h2><br>";
echo "<center>$num shouts</center>";
echo "<center><table align=\"center\" border=\"1\" style=\"width:115px;\"><tr><td><span class=\"shout\"><b>Name:</b> $name</span></td><tr><td><span class=\"shout\">$message</span></td></tr><tr><td><span class=\"shout\">Posted at $time</span></td></tr></table><br><br>";

$i++;
}
}
?>

Posted: Sun Jan 28, 2007 10:33 am
by louie35
The code seems right here. Check and make sure you don't have an include file that might do the insert as well.

Posted: Sun Jan 28, 2007 11:08 am
by tail
Yeah, thanks for that :) .

Posted: Sun Jan 28, 2007 5:33 pm
by feyd
Is there a reason why $time is not a native date format?

Posted: Sun Jan 28, 2007 10:20 pm
by tail
Not meaning to be rude but, could you explain yourself?

Posted: Sun Jan 28, 2007 10:59 pm
by feyd
Database native date-time format is YYYY-MM-DD HH:MM:SS. This is for several reasons, mostly, simple sorting and uniformity. Your code shows that you are storing in another format. I would like to understand why you chose to not use the native format (and field type.)

Posted: Sun Jan 28, 2007 11:15 pm
by superdezign
feyd wrote:Database native date-time format is YYYY-MM-DD HH:MM:SS. This is for several reasons, mostly, simple sorting and uniformity. Your code shows that you are storing in another format. I would like to understand why you chose to not use the native format (and field type.)
You know, I never quite understood that (or cared to look for it) until you said sorting. I thought it was annoying for print dates, but that makes perfect sense.

And I can definitely see passing a date() in a different format than your database deals with causing a problem

Posted: Sun Jan 28, 2007 11:27 pm
by tail
I prefer my dates in a different format. I think it looks nicer. No other reasons.

Posted: Mon Jan 29, 2007 12:10 am
by feyd
An added problem with using your own format for dates is that you more than likely will not be able to extract information in given date ranges easily. Because of the natural ability to sort perfectly in native format, it's VERY simple to find entries between two dates.

Posted: Mon Jan 29, 2007 6:22 am
by tail
Thanks for suggestion.