Inserting MySQL Data Twice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Inserting MySQL Data Twice

Post 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++;
}
}
?>
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post 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.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Yeah, thanks for that :) .
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is there a reason why $time is not a native date format?
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Not meaning to be rude but, could you explain yourself?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

I prefer my dates in a different format. I think it looks nicer. No other reasons.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Thanks for suggestion.
Post Reply