I have been working with PHP for a few months now and I wanted to make a simple mailing list script to add to a website I am helping redesign. I think that a mailing list will be a welcome upgrade. Anyway the error I am getting is

PHP Parse error:  syntax error, unexpected T_VARIABLE in F:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\manage.php on line 22

Not sure what is causing this. My code is below.

<?php
//functions
function doDB() {
	global $conn;
	//connect to the server and select databases; you may need it
	$conn = mysql_connect("localhost","root","jakpot")
		or die(mysql_error());
	mysql_select_db("test","$conn")	or die(mysql_error());
}

function emailChecker($email) {
	global $conn, $check_result;
	//check that the email 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 Email Address:</strong><br>
	<input type=text name=\"email\" size=40 maxlength=150>
	
	<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>input type=submit name=\"submit\" value=\"Submit Form\"></p>
	</form>";
	
}	else if (($_POST[op] == "ds") && ($_POST[action] == "sub")) {
	//trying to subscribe so validate email address
	if ($_POST[email] == "" {
		header("Location: manage.php");
		
	}
	//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 adress
	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) {
		//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>

Thanks in advance to anybody who can point me in the right direction.

Hello,

Your code on line 22: <form method=POST action=\ "$_SERVER[PHP_SELF]\"> Updated code : <form method=POST action=\"$_SERVER[PHP_SELF]\"> --- Just remove space between 'action=\ ' and ' " '.

Thanks
Ranjeet :)

im having problem on running my 1st php prog...i already install xammp successfully....error 404

m having problem on running my 1st php prog...i already install xammp successfully....error 404

Thanks that got rid of that error. However now I have the same error on line 15.

syntax error, unexpected T_STRING in F:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\manage.php on line 15

Anybody have any clue on this? I am still kinda stuck.

The book I got some of the code from was written for PHP 4.3, but I don't think that should be a problem.

mysql_query($check,$conn) or die mysql_error());

replace by

mysql_query($check,$conn) or die (mysql_error());

Opening braces were misg for die

!! thanks idk how I missed that. But now a syntax error is show up on line 38 for { but I am pretty sure it has to be there.

Exact error:

PHP Parse error:  syntax error, unexpected '{' in F:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\manage.php on line 38
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.