I have a bit of a problem with my newsletter
here is the error I get:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/happy/public_html/newsletter/subscribe.php on line 64
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/happy/public_html/newsletter/sindex.php on line 244
Warning: extract(): First argument should be an array in /home/happy/public_html/newsletter/nindex.php on line 175
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/happy/public_html/newsletter/nindex.php on line 191
Subscribe.php
<?php
// subscribe newsletter
// subscribe.php
// database connection script here
// MySQL Connection Variables
$hostname='localhost';
$user='user';
$pass='pass';
$dbase='user_newsletter';
$connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to
MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
if(!isset($mode))
{
$mode = 'index';
}
switch ($mode)
{
case 'index':
// display default form
?><p><strong>Subscribe/ UnSubscribe Mailing List</strong></p>
<form action="<?php echo "$PHP_SELF?mode=process"; ?>" method="post">
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td>Name:*</td>
<td><input type="text" name="name" size="20"></td>
</tr>
<tr>
<td>Email:*</td>
<td><input type="text" name="email" size="20"></td>
</tr>
<tr>
<td>Format Preferred:*</td>
<td><input type="radio" name="format" value="h"
checked>HTML <input type="radio" name="format" value="t">Plain Text</td>
</tr>
<tr>
<td>Mode:*</td>
<td><input type="radio" name="smode" value="subscribe"
checked>Subscribe <input type="radio" name="smode" value="unsubscribe">UnSubscribe</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit"
value="Submit"></td>
</tr>
</table>
</form>
<?php
break;
case 'process':
// check that all fields are filled in
if (empty($name) || empty($email))
{
die ("Error! Please fill in all required fields.");
}
// check for duplicate entry in database
$qCheck = "select * from subscribers where name='$name' and
email='$email' ";
$rsCheck = mysql_query($qCheck);
$countCheck = mysql_num_rows($rsCheck);
// determine whether user wants to subscribe or unsubscribe
if ($smode=='subscribe')
{
if ($countCheck != 0) // if entry already exists in database,
do not add subscriber
{
die ("Error. Record already exist.");
}
else {
$query = "insert into subscribers VALUES
('','$name','$email','$format')";
$successmsg = "Thank you $name. Your email, $email has
been recorded.";
}
}
else { // smode= unsubscribe
if ($countCheck == 0) // entry does not exist, cannot
unsubscriber user
{
die ("No such record, cannot unsubscribe user.");
}
else {
$query = "delete from subscribers where name='$name' and
email='$email' ";
$successmsg = "Thank you $name. Your email, $email has
been unsubscribed.";
}
}
// now execute the query
$result = mysql_query($query);
if ($result)
{
echo $successmsg;
}
break;
}
?>
sindex.php
<?php
// Subscriber Administration
// sindex.php
// MySQL Connection Variables (Enter the values)
$hostname='localhost';
$user='user';
$pass='pass';
$dbase='user_newsletter';
$connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
if(!isset($mode))
{
$mode = 'list'; // set the default mode to list subscribers
}
?><p><strong>Subscriber Administration</strong></p><?php
switch ($mode)
{
case 'list':
$q = "select * from subscriber order by sid desc limit 35 ";
$rs = mysql_query($q);
?>
<table cellpadding="1" cellspacing="1" border="1" >
<tr><td><strong>Name</strong></td><td>Email</td><td>Commands</td></tr>
<?php
while ($row = mysql_fetch_array($rs))
{
extract ($row);
// Display records into table rows.
echo "<tr><td><strong>$name</strong></td><td>$email</td>
<td><a href=\"$PHP_SELF?mode=edit&sid=$sid\">Edit</a> |
<a href=\"$PHP_SELF?mode=delete&sid=$sid\">Delete</a></td></tr>";
}
?></table><?php
break;
case 'add':
// Display Adding Form.
?><form action="sindex.php?mode=insert" method="post">
<table cellpadding="1" border="1">
<tr>
<td><strong>Name:</strong></td>
<td><input type="text" name="name" size="30" maxlength="25"></td>
</tr>
<tr>
<td><strong>Email:</strong></td>
<td><input type="text" name="email" size="30" maxlength="200"></td>
</tr>
<tr>
<td><strong>Format:</strong></td>
<td><input type="radio" name="format" value="h" checked>HTML
<input type="radio" name="format" value="t">Plain Text</td></tr>
<tr>
<td>Mode:*</td>
<td><input type="radio" name="smode" value="subscribe" checked>Subscribe <input type="radio" name="smode" value="unsubscribe">UnSubscribe</td>
</tr>
<tr><td colspan="2"><input type="submit" name="submit"></td></tr></table>
</form>
<?php
break;
case 'insert':
// check that all fields are filled in
if (empty($name) || empty($email))
{
die ("Error! Please fill in all required fields.");
}
// check for duplicate entry in database
$qCheck = "select * from subscribers where name='$name' and email='$email' ";
$rsCheck = mysql_query($qCheck);
$countCheck = mysql_num_rows($rsCheck);
// determine whether user wants to subscribe or unsubscribe
if ($smode=='subscribe')
{
if ($countCheck != 0) // if entry already exists in database, do not add subscriber
{
die ("Error. Record already exist.");
}
else {
$query = "insert into subscribers VALUES ('','$name','$email','$format')";
$successmsg = "Thank you $name. Your email, $email has been recorded.";
}
}
else { // smode= unsubscribe
if ($countCheck == 0) // entry does not exist, cannot unsubscriber user
{
die ("No such record, cannot unsubscribe user.");
}
else {
$query = "delete from subscribers where name='$name' and email='$email' ";
$successmsg = "Thank you $name. Your email, $email has been unsubscribed.";
}
}
// now execute the query
$result = mysql_query($query);
if ($result)
{
echo $successmsg;
}
break;
case 'edit':
$q = "select * from subscribers where sid=$sid ";
$rs = mysql_query($q);
while ($row = mysql_fetch_array($rs))
{
extract($row);
// Display Editing Form.
?>
<form action="<?php echo "sindex.php?mode=update&sid=$sid"; ?>" method="post">
<table cellpadding="1" border="1">
<tr>
<td><strong>Name:</strong></td>
<td><input type="text" name="name" size="30" maxlength="25" value="<?php echo "$name"; ?>"></td>
</tr>
<tr>
<td><strong>Email:</strong></td>
<td><input type="text" name="email" size="30" maxlength="200" value="<?php echo "$email"; ?>"></td>
</tr>
<tr>
<td><strong>Format:</strong></td>
<td><input type="radio" name="format" value="h"
<?php if ($format == "h") { echo "checked"; } ?>>HTML
<input type="radio" name="format" value="t" <?php if ($format == "t") { echo "checked"; } ?>>Plain Text</td></tr>
<tr>
<td>Mode:*</td>
<td><input type="radio" name="smode" value="subscribe" checked>Subscribe <input type="radio" name="smode" value="unsubscribe">UnSubscribe</td>
</tr>
<tr><td colspan="2"><input type="submit" name="Update"></td></tr></table>
</form>
<?php
}
break;
case 'update':
// check that all fields are filled in
if (empty($name) || empty($email))
{
die ("Error! Please fill in all required fields.");
}
// check for duplicate entry in database
$qCheck = "select * from subscribers where name='$name' and email='$email' ";
$rsCheck = mysql_query($qCheck);
$countCheck = mysql_num_rows($rsCheck);
// determine whether user wants to subscribe or unsubscribe
if ($smode=='subscribe')
{
if ($countCheck != 0) // if entry already exists in database, do not add subscriber
{
die ("Error. Record already exist.");
}
else {
$query = "update subscribers set name='$name', email='$email', format='$format' where sid='$sid' ";
$successmsg = "Success! Subscriber record has been updated.";
}
}
else { // smode= unsubscribe
if ($countCheck == 0) // entry does not exist, cannot unsubscriber user
{
die ("No such record, cannot unsubscribe user.");
}
else {
$query = "delete from subscribers where name='$name' and email='$email' ";
$successmsg = "Success. $email has been unsubscribed.";
}
}
// now execute the query
$result = mysql_query($query);
if ($result)
{
echo $successmsg;
}
break;
case 'delete':
$q = "delete from subscribers where sid='$sid' ";
$rs = mysql_query($q);
if ($rs)
{
echo "Success. Record deleted.";
}
break;
case 'search':
// display search box.
?>
<form action="sindex.php?mode=results" method="post">
<strong>Name:</strong> <input type="text" name="name" size="30" maxlength="20"><br>
<strong>Email:</strong> <input type="text" name="email" size="30" maxlength="200"><br>
<input type="submit" value="Search">
</form>
<?php
break;
case 'results':
if (empty($name) && empty($email))
{
die ("Error. There is no search criteria.");
}
// if user enter both name & email search
if ($name !="" && $email !="")
{
$q = "select * from subscribers where name like '$name%' and email like '$email%' ";
}
else {
if (empty($name))
{
// user entered email option
$q = "select * from subscribers where email like '$email$' ";
}
else {
// user chose name option
$q = "select * from subscribers where name like '$name%' ";
}
}
$rs = mysql_query($q);
$rscount = mysql_num_rows($rs);
// display results
?>
<table cellpadding="1" cellspacing="1" border="1" >
<tr><td><strong>Name</strong></td><td>Email</td><td>Commands</td></tr>
<?php
if ($rscount != 0)
{
while ($row = mysql_fetch_array($rs))
{
extract ($row);
// Display records into table rows.
echo "<tr><td><strong>$name</strong></td><td>$email</td>
<td><a href=\"$PHP_SELF?mode=edit&sid=$sid\">Edit</a> |
<a href=\"$PHP_SELF?mode=delete&sid=$sid\">Delete</a></td></tr>";
}
}
else {
echo "<tr><td colspan =3>No Results returned.</td></tr>";
}
?></table><?php
break;
}
?>
nindex.php
<?php
// Newsletter Administration
// nindex.php
// MySQL Connection Variables (Enter the values)
$hostname='localhost';
$user='user';
$pass='pass';
$dbase='user_newsletter';
$connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
$mode = $_GET['mode']; // for globals off
if(!isset($mode) || empty($mode))
{
$mode = 'list'; // set the default mode to list subscribers
}
?><p><strong>Newsletter Administration</strong></p><?php
switch ($mode)
{
case 'list':
$q = "select * from newsletter order by postdate desc ";
$rs = mysql_query($q);
?>
<table cellpadding="1" cellspacing="1" border="1" >
<tr><td><strong>Newsletter Title</strong></td><td>Post Date</td><td>Commands</td></tr>
<?php
while ($row = mysql_fetch_array($rs))
{
extract ($row);
// Display records into table rows.
echo "<tr><td><strong>$title</strong></td><td>$postdate</td>
<td><a href=\"$PHP_SELF?mode=edit&news_id=$news_id\">Edit</a> |
<a href=\"$PHP_SELF?mode=delete&news_id=$news_id\">Delete</a></td></tr>";
}
?></table><?php
break;
case 'add':
// Display Adding Form.
?><form action="nindex.php?mode=insert" method="post">
<table cellpadding="1" border="1">
<tr>
<td><strong>Newsletter Title:*</strong></td>
<td><input type="text" name="title" size="30" maxlength="250"></td>
</tr>
<tr>
<td><strong>Content:*</strong><br>HTML Code okay.</td>
<td><textarea name="content" rows="10" cols="35"></textarea></td>
</tr>
<tr><td colspan="2">
Dispatch Newsletter?<input type="checkbox" name="dispatch" value="yes">
<p><input type="submit" name="submit"></p></td></tr>
</table>
</form>
<?php
break;
case 'insert':
$title = $_POST['title'];
$content = $_POST['content'];
$dispatch = $_POST['dispatch'];
// check that all fields are filled in
if (empty($title) || empty($content))
{
die ("Error! Please fill in all required fields.");
}
// insert into database
$newRecord = "insert into newsletter (news_id, title, content, postdate)
VALUES ('','$title','$content', now()) ";
$rsRecord = mysql_query($newRecord);
if ($rsRecord && $dispatch == 'yes') // administrator wants to send newsletter out
{
$lastid= mysql_insert_id();
// redirect administrator to dispatch mode
header("Location: nindex.php?mode=dispatch&news_id=$lastid");
}
else {
echo "Success! Newsletter recorded.";
}
break;
case 'delete':
$news_id = $_GET['news_id'];
$remove = "delete from newsletter where news_id='$news_id' ";
$rs = mysql_query($remove);
if ($rs)
{
echo "Success, newsletter deleted.";
}
break;
case 'edit':
$news_id = $_GET['news_id'];
$q = "select * from newsletter where news_id=$news_id ";
$rs = mysql_query($q);
while ($row = mysql_fetch_array($rs))
{
extract($row);
// Display Editing Form.
?>
<form action="<?php echo "nindex.php?mode=update&news_id=$news_id"; ?>" method="post">
<table cellpadding="1" border="1">
<tr>
<td><strong>Title:</strong></td>
<td><input type="text" name="title" size="30" maxlength="250" value="<?php echo "$title"; ?>"></td>
</tr>
<tr>
<td><strong>Content:*</strong><br>HTML Code okay.</td>
<td><textarea name="content" rows="10" cols="35"><?php echo "$content"; ?></textarea></td>
</tr>
<tr><td colspan="2">
Dispatch Newsletter?<input type="checkbox" name="dispatch" value="yes">
<p><input type="submit" name="submit"></p></td></tr>
</table>
</form>
<?php
}
break;
case 'update':
$title = $_POST['title'];
$content = $_POST['content'];
$dispatch = $_POST['dispatch'];
// check that all fields are filled in
if (empty($title) || empty($content))
{
die ("Error! Please fill in all required fields.");
}
// Update query
$update = "update newsletter set title='$title', content='$content' where news_id='$news_id' ";
$rsUpdate = mysql_query($update);
if ($rsUpdate && ($dispatch=='yes'))
{
// Administrator wants to dispatch newsletter, redirect to dispatch mode
header("Location: nindex.php?mode=dispatch&news_id=$news_id");
}
else {
echo "Success, Newsletter updated.";
}
break;
case 'dispatch':
$news_id = $_GET['news_id'];
// dispatch newsletter to subscribers
// Obtain newsletter details
$select = "select * from newsletter where news_id='$news_id' ";
$result = mysql_query($select);
$row = mysql_fetch_array($result);
extract($row);
$plaintext = str_replace("<br>","\r\n",$content);
$plaintext = str_replace("<p>","\r\n",$content);
$plaintext = strip_tags($content);
// obtain subscriber list
$getList = "select * from subscribers ";
$rsList = mysql_query($getList);
$headers = "MIME-Version: 1.0\r \n";
// edit the following 3 variables
$from = "Your Name Here";
$fromemail = "abc@yyy.com";
$subject = "Newsletter from ABC website";
while ($subrow = mysql_fetch_array($rsList))
{
extract($subrow);
if ($format == 't')
{
// plain text format
$headers .= "Content-type: text/plain; charset=iso-8859-1\r \n";
$headers .= "From: \"$from\" <$fromemail>\r \n";
$headers .= "Reply-To: \"$from\" <$fromemail>\r \n";
$headers .= "X-Mailer: Just My Server";
$message = $plaintext;
}
else {
// HTML format
$headers .= "Content-type: text/html; charset=iso-8859-1\r \n";
$headers .= "From: \"$from\" <$fromemail>\r \n";
$headers .= "Reply-To: \"$from\" <$fromemail>\r \n";
$headers .= "X-Mailer: Just My Server";
$message = $content;
}
// mail it out
mail($email,$subject,$message,$headers);
}
echo "Newsletter sent out.";
break;
}
?>
Can somebody tell me my errors?