Hi.
I'm learning PHP and got this code from Sam's teach yourself PHP, MYSQL and APACHE. I can connect to the MYSQL server fine from terminal or PHPMyAdmin. The first part of this e-mail form works fine. It will subscribe and add the emails to the database. But it won't delete them, in fact it doesn't even seem to know they are there.
I get the impression it's losing it's connection to the database for some reason. Does anyone know if this could be something misconfigured somewhere?
Here is the code: (I took out the password/user info.) The part with the confused smiley is where it breaks down.
I'm learning PHP and got this code from Sam's teach yourself PHP, MYSQL and APACHE. I can connect to the MYSQL server fine from terminal or PHPMyAdmin. The first part of this e-mail form works fine. It will subscribe and add the emails to the database. But it won't delete them, in fact it doesn't even seem to know they are there.
I get the impression it's losing it's connection to the database for some reason. Does anyone know if this could be something misconfigured somewhere?
Here is the code: (I took out the password/user info.) The part with the confused smiley is where it breaks down.
PHP:
<?php
function doDB() {
global $mysqli;
//connect to server and select database; you may need it
$mysqli = mysqli_connect("localhost", "user", "password", "newsletter");
//if connection fails, stop script execution
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
function emailChecker($email) {
global $mysqli, $check_res;
//check that email is not already in list
$check_sql = "SELECT id FROM SUBSCRIBERS
WHERE email = ' ".$email."'";
$check_res = mysqli_query($mysqli, $check_sql)
or die(mysqli_error($mysqli));
}
?>
<?php
include("ch19_include.php");
//determine if they need to see the form or not
if (!$_POST) {
//they need to see the form, so create form 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")) {
//trying to subscribe; validate email address
if ($_POST["email"] == "") {
header("Location: subscriberform.php");
exit;
} else {
//connect to database
doDB();
//check that email is in list
emailChecker($_POST["email"]);
//get number of results and do action
if (mysqli_num_rows($check_res) < 1) {
//free result
mysqli_free_result($check_res);
//add record
$add_sql = "INSERT INTO Subscribers(email) VALUES('".$_POST["email"]."')";
$add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli));
$display_block = "<p>Thanks for signing up!</p>";
//close connection to MySQL
mysqli_close($mysqli);
} else {
//print failure message
$display_block = "<p>You're already subscribed!</p>";
}
}
} else if (($_POST) && ($_POST["action"] == "unsub")) {
//trying to unsubscribe; validate email adress
if ($_POST["email"] == "") {
header("Location: subscriberform.php");
//exit;
} else {
:confused://connect to database :confused:
doDB();
//check that email is in list
emailChecker($_POST["email"]);
//get number of results and do action altered from < 1 to < 0
if (mysqli_num_rows($check_res) < 0) {
//free result
mysqli_free_result($check_res);
//print failure message
$display_block = "<p>Couldn't find your address!</p>
<p>No action was taken.</p>";
} else {
//get value of ID from result
$result = mysql_query("SELECT id, name FROM Subscribers");
while ($row = mysql_fetch_array($Result, MYSQL_ASSOC)) {
printf ($result);
//$id = $row["id"];
}
//$res = mysql_query($sql) or exit( mysql_error() );
//unsubscribe the address
$del_sql = "DELETE FROM Subscribers WHERE ('".$_POST["email"]."')";/*id = '".$id."'";*/
$del_res = mysqli_query($mysqli, $del_sql) or die(mysqli_error($mysqli));
$display_block = "<p>You're unsubscribed!</p>";
}
mysqli_close($mysqli);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Subscribe/Unsubscribe</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Subscribe/Unsubscribe to Mailing List</h1>
<?php echo "$display_block"; ?>
</body>
</html>
Last edited by a moderator: