Jump to content

new to php, please help


elginwick

Recommended Posts

I am trying to create a simple mailing list that the user can subscribe and unsubscribe to. I can get the new subscriptions and it works, but when i try to unsubscribe, i get this error

query was empty

i don't know what is going wrong.

Thanks.

here is the code:

<?php
function doDB() {
global $conn;
$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("db",$conn) or die(mysql_error());
}

function emailChecker($email) {
global $conn, $check_result;
$check = "select id from subscribers where email = '$email'";
$check_result = mysql_query($check,$conn) or die (mysql_error());
}

if ($_POST[op] != "ds") {
$display_block = "
<form method=POST action=\"$_SERVER[PHP_SELF]\">

<p><font color=\"#164059\">Your E-Mail Address:<br>
<input type=text name=\"email\" size=40 maxlength=150>

<p>Action:<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")) {
if ($_POST[email] == "") {
header("Location: manage.php");
exit;
}
doDB();
emailChecker($_POST[email]);


if (mysql_num_rows($check_result) < 1) {
$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 {
$display_block = "<p>Your're already subscribed!</p>";
}
} else if (($_POST[op] == "ds") && ($_POST[action] == "unsub")) {
if ($_POST[email] == "") {
header("Location: manage.php");
exit;
}
doDB();
emailChecker($_POST[email]);

if (mysql_num_rows($check_result) < 1) {
$display_block = "<p>Couldn't find your address!</p>
<P> No action was taken.</p>";
} else {
$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>Newsletter Subscription</title>
<style type="text/css">
<!--
.style5 { font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #164059;
}
body,td,th {
font-family: Arial, Helvetica, sans-serif;
color: #164059;
}
body {
background-color: #FFFFFF;
}
a:link {
color: #164059;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #164059;
}
a:hover {
text-decoration: none;
color: #164059;
}
a:active {
text-decoration: none;
color: #164059;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<div align="center"><span class="style5"><?php echo "$display_block"; ?></span></div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/7985-new-to-php-please-help/
Share on other sites

try to change these lines
[code]} else {
$id = mysql_result($check_result, 0, "id");
$sql = "delete from subscribers where id = '$id'";[/code]
for these ones
[code]} else {
$arr_result = mysql_fetch_array ( $check_result );
$sql = "delete from subscribers where id = '$arr_result['id']'";[/code]
I fast-read your code and can't see anything wrong..

D.Soul
Link to comment
https://forums.phpfreaks.com/topic/7985-new-to-php-please-help/#findComment-29131
Share on other sites

I just change little structure... but, never know.. try it..

I need to go home, so, good luck with your code =)

Bye, D.Soul

[code]<?php
function doDB()
{
    global $conn;
    $conn = mysql_connect("localhost", "user", "pass");
    mysql_select_db("db",$conn) or die(mysql_error());
}
function emailChecker($email)
{
    global $conn, $check_result;
    $check = "select id from subscribers where email = '$email'";
    $check_result = mysql_query($check,$conn) or die (mysql_error());
}
if ($_POST[op] != "ds")
{
    $display_block = "
<form method=POST action=\"$_SERVER[PHP_SELF]\">
    <p>
        <font color=\"#164059\">Your E-Mail Address:<br>
        <input type=text name=\"email\" size=40 maxlength=150>
    </p>
    <p>
        Action:<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[action] == "sub")
{
    if ($_POST[email] == "")
    {
        header("Location: manage.php");
        exit;
    }
    doDB();
    emailChecker($_POST[email]);
    if (mysql_num_rows($check_result) < 1)
    {
        $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 {
        $display_block = "<p>Your're already subscribed!</p>";
    }
} else if ($_POST[action] == "unsub")
{
    if ($_POST[email] == "")
    {
        header("Location: manage.php");
        exit;
    }
    doDB();
    emailChecker($_POST[email]);
    if (mysql_num_rows($check_result) < 1)
    {
        $display_block = "<p>Couldn't find your address!</p>
        <P> No action was taken.</p>";
    } else {
        $id = mysql_result($check_result, 0, "id");
        $sql = "delete from subscribers where id = '$id' limit 1";
        $result = mysql_query($sql,$conn) or die(mysql_error());
        $display_block = "<p>You're Unsubscribed!</p>";
    }
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/7985-new-to-php-please-help/#findComment-29142
Share on other sites

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.