Forum Moderators: coopster

Message Too Old, No Replies

PHP Beginner

script problems

         

shaundunne

4:21 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



I've just started doing tutorials online. This one is for a subscription.

<?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

cameraman

4:27 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, shaundunne!

Could you point out which line is #54?

shaundunne

4:34 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



Thank you - line 54
$display_block ="<p>Thanks for signing up!</p>";

mooger35

4:45 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



The error is actually from the line above

or die(mysql_error($mysql))

you need a ; at the end

like this:

or die(mysql_error($mysql));

shaundunne

4:48 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



thanks i did that

i now get

unexpected '}' in line 57

Line 57 is the else

//close connection to SQL
mysql_close($mysql)
} else {

if i remove the brace from before the else i get

unexpected T_ELSE

in line 57.

cameraman

5:10 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's the same again, you need a semicolon:
mysql_close($mysql);

shaundunne

8:39 pm on Dec 23, 2007 (gmt 0)

10+ Year Member



Thanks for your help.

Im now getting

parse error, unexpected '}' in line 57.

eelixduppy

9:36 pm on Dec 23, 2007 (gmt 0)



What's line 56 and 57 now? Did you actually add the semicolon like it was suggested?