Page 1 of 1

PHP/mySQL forms POST issue

Posted: Sat Jul 18, 2009 6:31 pm
by jwl
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:

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();
?>
What is happening here, to produce 'shadowed' blank submissions?
Thanks.

jwl

Re: PHP/mySQL forms POST issue

Posted: Sat Jul 18, 2009 7:14 pm
by califdon
I can't spot any specific reason for that behavior. What happens when you first view the page, fill in the form and click the Submit button? Does it submit a blank record, then display the form again with nothing in the input elements? Then if you click on the Submit button, with no visible values in the form, it submits a proper record? I think the problem may be in your script post.php, which you haven't shown to us. That's the one that inserts new rows in your table.

Re: PHP/mySQL forms POST issue

Posted: Sat Jul 18, 2009 8:52 pm
by jwl
Hi califdon,

The code I'd posted is the post.php file itself; it posts to itself (is that allowable?). And to answer your question(s), yes, blank at first, then Submit again and the value shows, even when the text field is empty on the second submission.

Thanks.
jwl

Re: PHP/mySQL forms POST issue

Posted: Sat Jul 18, 2009 9:16 pm
by califdon
Yes, posting to the same script is perfectly allowable, but you have to have logic that determines whether to display data or a data entry form. Your script doesn't do that, and it is very difficult to even read your script because it has comments that don't apply to the code it's near, it creates variables that it never uses, and it uses very poor names for variables. I will guess that you found a script and tried to modify it. To do that, you really need to understand the principles on which the script was based. It even looks like there may have been a conditional branch in the original script that has been removed, judging from the comment "//end if", when there isn't an if in the script. There should be! That's the cause of the behavior. Without a conditional branch, every time the script is executed, it writes a record to the database, whether there's anything in the form or not. You only want it to write a record if the Submit button has been clicked and there's data in the form.

Re: PHP/mySQL forms POST issue

Posted: Sun Jul 19, 2009 9:01 am
by jwl
Thanks