Forum Moderators: coopster

Message Too Old, No Replies

Problems with form adding blank row when loading page

php, mysql, insert, blank row on refresh

         

s4rkx

9:36 am on Jul 29, 2004 (gmt 0)

10+ Year Member



Blow is my code, evertime i refresh the page, a blank row gets inserted, i can then insert manually but entering fields and pressing submit. I WANT TO STOP THE BLANK ROW INSERT, i know this can be dopne with a submit function (if $submit == ) but am not sure...

Can someone please help.....

<form action="<?php echo ''.$_SERVER['PHP_SELF'].'';?>" method="post">
<table width="95%" border="0">
<tr>
<td width="14%">&nbsp;</td>
<td width="27%">First Name: </td>
<td width="37%"><input type="text" name="first"></td>
<td width="22%">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Last Name: </td>
<td><input type="text" name="last"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Phone: </td>
<td><input type="text" name="phone"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Mobile: </td>
<td><input type="text" name="mobile"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Fax: </td>
<td><input type="text" name="fax"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input name="Submit" type="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
<?

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax')";
mysql_query($query);

echo "Record $first Inserted";

mysql_close();

?>
<p><br>
<a href="insert.php">insert another</a></p>
<p><br>
<a href="index.php">view table</a></p>

RonPK

10:07 am on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi & welcome,

You should indeed use a condition in the PHP script. In this case, try:

if (isset($_POST['Submit'])) {
// your code here
}

s4rkx

1:55 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



Hello RonPK,

Thanks for taking time to look at my code, have tried inserting the code you offered as below:
</form>
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if (isset($_POST['Submit']))
{
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$...')";
mysql_query($query);

echo "Record $first Inserted";
}
else {
echo "Insert a file";
}
mysql_close();
?>

I can see the else statement on the page, which is good, as it confirms not insert has been done, but when entering details and clicking submit, there is no action on db.

Thanks in adv

RonPK

6:53 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah yes. That's probably because your submit button has no value. Never mind, you can also check whether any of your input fields has been sent:
if (isset($_POST['first'])) {
// code here
}

I recommend using the so called superglobal variables $_POST, $_GET et cetera, as you may one day find yourself on a server with register_globals set to off. In that case you can't simply call $first, $last anymore. More on that topic in the manual [nl2.php.net].