Forum Moderators: coopster
<?php
//set up functions
function doDB() {
global $mysql;
//connect to server and select database
$mysql = mysql_connect("localhost","user","pass","db");
//if connection connection fails
if (myqsl_connect_errno()) {
echo "connect failed: %s\n", mysql_connect_error();
exit();
}
}
function emailChecker($email) {
global $mysql, $check_res;
//checking that email is not in list
$check_sql = "select id from subscribers where email ='".$email."'";
$check_res = $mysql_query($mysql, $check_sql)
or die(mysql_error($mysql));
}
//determine if they need to see the form
if (!$_POST) {
//they need to see the form, create block
$display_block = "
<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Your E-Mail Address:</strong><br>
<input type=\"text\" name=\"email\" size=\"40\">
<p><strong>Action:</strong><br>
<input type=\"radio\" name=\"action\" value=\"sub\" checked> Subscribe
<input type=\"radio\" name=\"action\" value=\"unsub\"> Unsubscribe
<p><input type=\"submit\" name=\"submit\" value=\"\submit form\"></p>
</form>";
} else if (($_POST) && ($_POST["action"] == "sub")) {
//try to subscribe; validate email address
if ($_POST["email"]== "")
header("Location: manage.php");
exit;
} else
//connect to database
doDB();
//check that email is in the list
emailChecker($_POST["email"]);
//get number of results and do action
if (mysql_num_rows($check_res) <1)
//free results
mysql_free_result($check_res);
//add record
$add_sql ="insert into subscribers (email)
values('".$_POST["email"]."')";
$add_res =mysql_query($mysql, $add_sql)
or die(mysql_error($mysql))
$display_block ="<p>Thanks for signing up!</p>";
//close connection to SQL
mysql_close($mysql)
} else {
//print failure message
$display_block = "<p>You're already subscribed</p>";
}
}
} else if (($_POST) && ($_POST["action"] == "unsub")) {
//trying to unsubscribe ; validate email
if ($_POST["email"] == "") {
header("location:manage.php");
exit;
} else {
//connect to database
doDB();
//check email is in list
emailChecker($_POST["email"]);
//get number of results and do action
if (mysql_num_rows($check_res) < 1 ; {
mysql_free_result ($check_res);
//print failure message
$display_block = "<p>Could't find your address</p>
<p>No action was taken.</p>";
} else {
//get value of ID from result
while ($row = mysql_fetch_array($check_res)) {
$id = $row["id"];
}
//unsubscribe the address
$del_sql = "delete from subscribers where id ='".id."'";
$del_res = mysql_query (msql, $del_sql)
or die(mysql_error(mysql));
$display_block = "<p>You're unsubscribed</p>";
}
mysql_close($mysql);
}
}
?>
<html>
<head>
<title>Subscribe mailing list</title>
</head>
<body>
<h1>Subscribe / unsubscribe to Mailing List</h1>
<?php echo "$display_block";?>
</body>
</html>
The error message i have at the moment is
unexpected T_VARIABLE in line 54
The others that i have are
unexpected T_ELSE in line 57
unexpected '}' in line 56
I know that there is a closing brace or semi-colon, i just cant see where,
anyone help.
Cheers