Jump to content

[SOLVED] Parse Error/Undefined Variable


twilitegxa

Recommended Posts

I have a newsletter subscription code I am writing, but I am receiving the following error:

 

Parse error: parse error in C:\wamp\www\manage.php on line 80

 

And the code will not run at all. But, if I take out the code that contains the error ( a } on line 80), most of the code works. It allows an e-mail address to subscribe, it prints a message if a duplicate e-mail tries to subscribe, it prints a message if the e-mail address doesn't exist upon attempting to unsubscribe, but it produces this error if someone tries to unsubscribe:

 

Notice: Undefined variable: display_block in C:\wamp\www\manage.php on line 88

 

How can I fix this problem? It appears I have defined the variable, but I also would like to know why I received the parse error before, because I was under the impression that the } was needed. Can anyone help? Here is the code without the { (it is supposed to go right above the ending php tag [?>]):

 

<?php
//set up a couple of functions
function doDB() {
global $conn;
//connect to server and select database; you may need it
$conn = mysql_connect("localhost", "root", "")
	or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());
}

function emailChecker($email) {
global $conn, $check_result;
//checl that e-mail is not already in list
$check = "select id from subscribers where email = '$email'";
$check_result = mysql_query($check,$conn) or die(mysql_error());
}

//determine if they need to see the form or not
if ($_POST['op'] !="ds") {
//they do, 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 maxlength=150></p>

<p><strong>Action:</strong><br>
<input type=radio name=\"action\" value=\"sub\" checked> subscribe
<input type=radio name=\"action\" value=\"unsub\"> unsubscribe

<input type=\"hidden\" name=\"op\" value=\"ds\"></p>

<p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
</form>";

} else if (($_POST['op'] == "ds") && ($_POST['action'] == "sub")) {
//trying to subscribe; validate email address
if ($_POST['email'] == "") {
	header("Location: manage.php");
	exit;
}
//connect to database
doDB();
//check that email is in list
emailChecker($_POST['email']);

//get number of results and do action
if (mysql_num_rows($check_result) < 1) {
	//add record
	$sql = "insert into subscribers values('', '$_POST[email]')";
	$result = mysql_query($sql,$conn) or die(mysql_error());
	$display_block = "<p>Thanks for signing up!</p>";
} else {
	//print failure message
	$display_block = "<p>You're already subscribed!</p>";
}
} else if (($_POST['op'] == "ds") && ($_POST['action'] == "unsub")) {
//trying to unsubscribe; validate email address
if ($_POST['email'] == "") {
header("Location: manage.php");
	exit;
}
//connect to database
doDB();
//check that email is in list
emailChecker($_POST['email']);

//get number of results ad do action
if (mysql_num_rows($check_result) < 1) 
	//print failure message
	$display_block = "<p>Couldn't find your address!</p>
	<p>No action was taken.</p>";
} else {
	//unsubscribe the address
	$id = mysql_result($check_result, 0, "id");
	$sql = "delete from subscribers where id = '$id'";
	$result = mysql_query($sql,$conn) or die(mysql_error());
	$display_block = "<p>You're unsubscribed!</p>";
}

?>
<html>
<head>
<title>Subscribe/Unsubscribe</title>
</head>
<body>
<h1>Subscribe/Unsubscribe</h1>
<?php echo "$display_block"; ?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/165020-solved-parse-errorundefined-variable/
Share on other sites

This is the beginning of line 80:

 

}
?>
<html>
<head>
<title>Subscribe/Unsubscribe</title>
</head>
<body>
<h1>Subscribe/Unsubscribe</h1>
<?php echo "$display_block"; ?>
</body>
</html>

 

But I think the error is above, somewhere maybe in these lines:

 

73-79

} else {
	//unsubscribe the address
	$id = mysql_result($check_result, 0, "id");
	$sql = "delete from subscribers where id = '$id'";
	$result = mysql_query($sql,$conn) or die(mysql_error());
	$display_block = "<p>You're unsubscribed!</p>";
}

Because you mentioned my missing {'s, I was able to find where I was missing one. I added a { to line 69:

 

if (mysql_num_rows($check_result) < 1) {

 

It was missing there, and then when I added the } back to line 80, the problem was solved. Thanks for the help!

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.