Newbie question - displaying form variables with form action=""

Hi all. I am new to PHP/MySQL and am working my way through Build your own database driven website using PHP and MSQL 4th edition. I have just finished chapter 4 and am implementing what I have learnt with a basic site to submit data through a form to a database and display it (different to the example in the book). I have written code that first checks for values (domain and author) posted by a form using $_REQUEST, second lists all the values stored in the database and third includes the actual form for submitting new values (domain and author). The form has action=“” and so is submitting to the current page. My thinking with this is that when the form is submitted the page will be reloaded and the first part of the code will capture the variables and the second will display them. But when I submit new values with the form the new values are not listed on the page until the page is manually reloaded (which then causes the values to be added to the database twice). Does submitting a form with action=“” not cause the current page to be reloaded (and therefore display the values on the page)?

I’ve included my relevant code below. Thanks very much for any advice.

if (isset($_REQUEST['domain']))  // check to see if form has been submitted
	{
		
	$domain = mysqli_real_escape_string($link, $_REQUEST['domain']);
	$author = mysqli_real_escape_string($link, $_REQUEST['author']);
	$sql = 'INSERT INTO domainsuggestions SET
	domain="' . $domain .'",
	author="' . $author . '",
	submitdate=CURDATE()';
	
	if (!mysqli_query($link, $sql))  // insert domain suggestion into table
		{
			$output = 'Unable to add suggestion to table. Error '. mysqli_error($link);
			echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
		}
	}
	
while ($row = mysqli_fetch_array($result))
{
$domains[] = array('domain' => $row['domain'], 'author' => $row['author'], 'id' => $row['id']);
}

<table>
    	<tr>
            <td>ID</td>
            <td>Domain Suggestions</td>
            <td>Author</td>
        </tr>	
<?php foreach ($domains as $item) { ?>
	<tr>
	    <td><?php echo htmlspecialchars($item['id'], ENT_QUOTES, 'UTF-8'); ?></td>
            <td><?php echo htmlspecialchars($item['domain'], ENT_QUOTES, 'UTF-8'); ?></td>
            <td><?php echo htmlspecialchars($item['author'], ENT_QUOTES, 'UTF-8'); ?></td>
        </tr>
<?php } ?>
</table>

<form method="post" action="">
<div>
	<label for="domain name">Suggest a Domain:</label> <input type="text" name="domain" /><br />
	<label for="author name">Your Name:</label> <input type="text" name="author" /><br />
    <input type="submit" />
</div>
</form>

I modified your script a bit. Take a look and if this helps:

<?php
   include("domain_config.php");
   $link = @mysqli_connect($db_host, $db_username, $db_password,$db_name);

   if($_REQUEST['domain'] && $_REQUEST['author'])
   {
	   $domain = mysqli_real_escape_string($link, $_REQUEST['domain']);
	   $author = mysqli_real_escape_string($link, $_REQUEST['author']);
	   $sql = "INSERT INTO domainsuggestions SET domain='$domain',author='$author',submitdate=CURDATE()";
	   if (!mysqli_query($link, $sql))  // insert domain suggestion into table
	   {
		   $output = 'Unable to add suggestion to table. Error '. mysqli_error($link);
		   echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
	   }
   }
   $result = mysqli_query($link, "SELECT * FROM domainsuggestions");
?>
<table>
   <tr>
      <td>ID</td>
      <td>Domain Suggestions</td>
      <td>Author</td>
      <td>Date</td>
   </tr>	
   <?php while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { ?>
   <tr>
      <td><?php echo $row['id']; ?></td>
      <td><?php echo $row['domain']; ?></td>
      <td><?php echo $row['author']; ?></td>
      <td><?php echo $row['submitdate']; ?></td>
   </tr>
   <?php } ?>
</table>
<h3>Your suggestion:</h3>
<form method="post" action="">
<div>
	<label for="domain name">Suggest a Domain:</label> <input
	type="text" name="domain" value="<?php echo $_REQUEST['domain']; ?>"/><br />
	<label for="author name">Your Name:</label> <input
	type="text" name="author" value = "<?php echo $_REQUEST['author']; ?>"/><br />
    <input type="submit" />
</div>
</form>

You can test it out here:
http://www.itsawebthing.org/test/domain.php

Thanks - the main thing I had wrong was having the code to pull the info out of the db before the code to add the new suggestion. Pretty simple!

PS. What’s the vbcode to insert PHP parsed code into posts, as opposed to general code?

Click on Go Advanced and you will see a new editor with more options and one of which will allow you to input PHP code. I’m new here to so I’m still finding my way around.

[ PHP ] code here [ /PHP ] (no spaces)