PHP/mySQL forms POST issue
Posted: Sat Jul 18, 2009 6:31 pm
Hi,
I have put together a snippet of code to 1) connect to the mySQL db, 2) SELECT * from specified table and output data to site and 3) get some input from a user visiting to post to the same site. It all works, however, it may be that the order of the actions is out of whack, because when the table is generated and some data is displayed above on the page (the form to get data from user is on the bottom), when the user enters a comment to submit and hits SUBMIT button on the form, they post a blank submission, however, if (without typing anything again) the user hits SUBMIT button again, the posting shows up then. Is the variable shadowed, not cleared out or buffered somewhere/somehow? Below is the code:
What is happening here, to produce 'shadowed' blank submissions?
Thanks.
jwl
I have put together a snippet of code to 1) connect to the mySQL db, 2) SELECT * from specified table and output data to site and 3) get some input from a user visiting to post to the same site. It all works, however, it may be that the order of the actions is out of whack, because when the table is generated and some data is displayed above on the page (the form to get data from user is on the bottom), when the user enters a comment to submit and hits SUBMIT button on the form, they post a blank submission, however, if (without typing anything again) the user hits SUBMIT button again, the posting shows up then. Is the variable shadowed, not cleared out or buffered somewhere/somehow? Below is the code:
Code: Select all
<?php
#Include the SQL config info
require './.sqlconfig.php';
#Connect to mysql db
$SQLConnection = mysql_connect ($dbhost,$dbusername,$dbuserpass);
mysql_select_db($dbname) or die('Cannot connect to db!');
#Search variable = data in search box or URL
$query = "SELECT PostDate,Post FROM TABLE1 ORDER BY id DESC";
#Run the query above and store it; otherwise fail and display error
$result = mysql_query($query);
#Colors for alternation of row color on results table
$color1 = "#d5d5d5";
$color2 = "#e5e5e5";
#While there are rows, print it.
while($row = mysql_fetch_array($result))
{
#Row color alternates for each row
$row_color = ($row_count % 2) ? $color1 : $color2;
#Table background color = row_color variable
echo "<center><table border=0 width=527>";
echo "<tr>";
echo "<font face=\"arial\" size='2'>";
echo "<td width=77 align='left'><font face=\"arial\" size='1' color=\"Blue\">".$row['PostDate']."</font></td>";
echo "<td width=450 align='left'><font face=\"arial\" size='1'>".$row['Post']."</font></td>";
echo "</font>";
echo "</tr>";
echo "</table></center>";
$row_count++;
#End While
} //end if
echo "<br><form method=\"POST\" action=\"post.php\">";
echo "<center>";
echo "<tr>";
echo "<td><input type=\"text\" name=\"Post\" size=\"60\"></td>";
echo "<td><input type=\"submit\" name=\"Submit\" Value=\"Submit\"></td>";
echo "</tr>";
echo "</table></center>";
echo "</form>";
#Assign our form names above to variables with data, assign the SQL statement,
#...and run the queury
$PostIt=$_POST["Post"];
mysql_query("INSERT INTO TABLE1(PostDate,Post) VALUES(CURDATE(),'$_POST[Post]')");
unset($PostIt);
#Clear memory
mysql_free_result($result);
mysql_close($SQLConnection);
session_unset();
?>Thanks.
jwl