These really helped me so much, but still have some other problems, here is the code after editting it:
<?php
/// Connects to your Database
mysql_connect("localhost", "my username", "my password") or die(mysql_error());
mysql_select_db("my db name") or die(mysql_error());
///This code runs if the form has been submitted
if (isset($_POST['submit'])) {
///This makes sure they did not leave any fields blank
if (!$_POST['email'])
{
[COLOR=Red]die[/COLOR]"<font color=red><b><i>No e-mail added</i></b>";
}
/// checks if that e-mail is in use
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$mailcheck = $_POST['email'];
$check = mysql_query("SELECT email FROM maillist WHERE email = '$mailcheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
///if e-mail exists it gives an error
if ($check2 != 0) {
echo"<font color=red><b><i>Already subscribed</i></b>";
}
else
{
$insert = "INSERT INTO maillist (id, email) VALUES ('0', '".$_POST['email']."')";
$add_email = mysql_query($insert);
echo"<font color=blue><b><i>Subscribed successfully</i></b>";
}
///now we insert it into the database
?>
<?php
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr bgcolor="#6652D8">
<td width="140"><span class="style1"><span class="style1">Get our Newsletter</span>: </span></td>
</tr>
<tr><td>
<input type="text" name="email" maxlength="100">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Subscribe"></th></tr>
<tr><td></td></tr>
<tr><td bgcolor="#6652D8"></td>
</tr>
</table>
</form>
<?php
}
?>
the code for valediating e-mail addresses doesn't work as we did't assign $mail for any value, also it will like that red die above in the code stop loading of the whole page, and if i use print or echo, it will tell the error message but will not prevent inserting to database, the above red die in :
///This makes sure they did not leave any fields blank
if (!$_POST['email'])
{
[COLOR=Red]die[/COLOR]"<font color=red><b><i>No e-mail added</i></b>";
}
stops all the page from continue loading, when i used echo instead, it didn't prevent inserting a blank row to database.
the second problem is, i have another form in the same page, when i press submit, it excute both scripts, what should i do?