Good evening. I usally construct forms using include html forms but i want to do a page where the processing and the form appear on the same page. The problem with the script down below is that when it renders in the browser it automatically runs the queries and posts Null values into the database, it displays the message "Thank you for submitting a link, Check back soon!" on the page every time. This should only appear when the user submits the form. I know i have a problem with the layout but my knowledge of PHP wouldn't be the best to rectify the problem.
<?
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$me = $_SERVER['PHP_SELF'];
?>
<form name="form1" method="POST" action="submit.php">
Your Name:
<input name="name" type="text" id="name" class="formerror" size="35" maxlength="25" />
<br />
Email Address:
<input name="email" type="text" id="email" class="formerror" size="35" maxlength="25" />
<br />
Website Name:
<input name="sitename" type="text" id="sitename" class="formerror" size="35" maxlength="25" />
<br />
Link: www.
<input name="sitelink" type="text" id="sitelink" class="formerror" size="35" maxlength="25" />
<br />
Category:
<select name="category" id="category">
<option value="Music">Music</option>
<option value="Waterford">Wateford</option>
<option value="Irish">Irish</option>
</select>
<br />
Description:
<textarea name="des" cols="35" rows="6" class="des" id="address1"></textarea>
<input name="submit" type="submit" value="Submit"></input></form>
<?
$name = $_POST['name'];
$email = $_POST['email'];
$sitename = $_POST['sitename'];
$sitelink = $_POST['sitelink'];
$category = $_POST['category'];
$des = $_POST['des'];
$adm = mysql_num_rows(mysql_query("SELECT id FROM links"));
if ($adm == 0) {
$sql = mysql_query("INSERT INTO links (name, email, sitename, sitelink, category, des, level, date)
VALUES('$name', '$email', '$sitename', '$sitelink', '$category', '$des', '1', now())") or die ( mysql_error());
}
else {
$sql = mysql_query("INSERT INTO links (name, email, sitename, sitelink, category, des, level, date)
VALUES('$name', '$email', '$sitename', '$sitelink', '$category', '$des', '1', now())") or die ( mysql_error());
}
if(!$sql){
echo 'There has been an error';
} else {
$id = mysql_insert_id();
// mailer
$subject = "New Link Submited!";
$email_address = "cormacmoylan@gmail.com";
$message = "Hello, a new link has been added by $name using the email address $email
$sitename
$sitelink
$des
$category
To validate the link click here<br />
http://www.blaa-blaa.com/link/validate.php?id=$id
To remove the link or edit it please login to the link management system at ....
";
mail($email_address, $subject, $message, "From: Link sumbmisson<cormac@blaa-blaa.com>\nX-Mailer: PHP/" . phpversion());
echo 'Thank you for submitting a link, Check back soon!';
}
}
?>
Cheers