randy, thank you - I used the code at badblue and integrated it with my code. It works now. I don't know the exact reason why it works now though. Maybe because my form was not part of the php code? I am going to include the original and the new working code. If someone knows why the original didn't work, please let me know so I don't repeat my mistake:
original(did not work):
<head>
<title>Insert Email address</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="email_admin" method="get" action="<?php echo $PHP_SELF;?>">
<p>first name:<input type="text" name="f_name"><br>
last name: <input type="text" name="l_name"><br>
e-mail: <input type="text" name="email"><br>
e-mail id: <input type="text" name="email_id"><br>
<input type="submit" value="Insert"></p>
</form>
<?php
$db = mysql_connect("localhost", "", "");
mysql_select_db("MyFirstDatabase",$db);
if ((isset($f_name)) AND (isset($l_name))AND (isset($email))AND (isset($email_id))) {
$sql="INSERT INTO test_email VALUES('$f_name','$l_name','$email','$email_id')";
$result=mysql_query($sql,$db);
$f_name = str_replace("\'","'",$f_name);
$l_name = str_replace("\'","'",$l_name);
echo "<h1>Success!</h1><p>";
echo "$f_name was inserted into the database. Here are the details:<p>";
echo "name: $f_name $l_name<br>email: $email<p>";
}
?>
</body>
new (working code):
<head>
<title>Insert Email address</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?php
if ((!isset($f_name)) OR (!isset($l_name))OR (!isset($email))or (!isset($email_id))) {
echo('<form action="insert2.php" method="POST" >');
echo('first name:<input type="text" name="f_name"><br>');
echo('last name: <input type="text" name="l_name"><br>');
echo('e-mail: <input type="text" name="email"><br>');
echo('e-mail id: <input type="text" name="email_id"><br>');
echo('<input type="submit" value="Insert">');
echo('</form>');
}else{
$db = mysql_connect("localhost", "", "");
mysql_select_db("MyFirstDatabase",$db);
$sql="INSERT INTO test_email VALUES('$f_name','$l_name','$email','$email_id')";
$result=mysql_query($sql,$db);
$f_name = str_replace("\'","'",$f_name);
$l_name = str_replace("\'","'",$l_name);
echo "<h1>Success!</h1><p>";
echo "$f_name was inserted into the database. Here are the details:<p>";
echo "name: $f_name $l_name<br>email: $email<p>";
}
?>
</body>