Twinkie
Banned
- Messages
- 1,389
- Reaction score
- 12
- Points
- 0
Trying some simple testing to form the backbone of a subscription system. Why am I getting this error when I make a subscription?
As far as I can tell the global declaration should solve this problem?
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mail Subscription</title>
</head>
<body>
<?php
function DBCon() {
global $con;
$con = mysqli_connect("localhost", "user", "nopass","mtwinkie_testdb");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
function Validate($email) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
die("<h1 align=\"center\">Invalid Email.</h1>");
else if (strlen(email) > 150)
die("<h1 align=\"center\">Email too long.</h1>");
$query = "SELECT id FROM Newsletter WHERE email = '" . email ."';";
$res = mysqli_query($con,$query) or
die(mysqli_error($con));
if (mysqli_num_rows($res) < 1)
die("<h1 align=\"center\">Your already subscribed.</h1>");
return true;
}
if (!isset($_REQUEST["do"])) {
?>
<h1>Mail Subscription</h1>
<form action="#" method="post">
<b>Your Email Address:</b><br />
<input type="text" name="email" /><br /><br />
<b>Action:</b><br />
<input type="radio" name="do" value="sub" /> Subscribe
<input type="radio" name="do" value="unsub" /> Unsubscribe
<p><input type="submit" value="Submit Form" /></p>
<?php
} else if ($_REQUEST["do"]=="sub") {
DBCon();
if (Validate($_REQUEST["email"])) {
$query = "INSERT INTO Newsletter (ID, Email) VALUES (\"" . $email . "\");";
$res = mysqli_query($con,$query) or
die(mysqli_error($con));
mysql_close($con);
die("<h1 align=\"center\">Thank you for signing up!</h1>");
}
} else if ($_REQUEST["do"]=="unsub") {
die("<h1 align=\"center\">Not available yet =(</h1>");
} else die("<h1 align=\"center\">Logical Script Error.</h1>");
?>
</body>
</html>