User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 422,963 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,927 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting

Php newsletter error

Join Date: May 2005
Posts: 26
Reputation: Yuki H. is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Yuki H.'s Avatar
Yuki H. Yuki H. is offline Offline
Light Poster

Re: Php newsletter error

  #3  
Jun 14th, 2005
That helped a bit, thank you.
But I still have errors and since I am new at php I do not know how to
correct them. (is there a such thing as a php validator?)

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/newsletter/sindex.php on line 92

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/user/public_html/newsletter/sindex.php on line 33
[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 if email is valid
if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $email))
{

echo 'That does not look like a valid email address.'; }


// 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) or die(mysql_error());

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);


//here
$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) or die(mysql_error());

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) or die(mysql_error());
$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;
}

?>

[/PHP]
Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/newsletter/nindex.php:23) in /home/user/public_html/newsletter/nindex.php on line 159

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/newsletter/nindex.php:23) in /home/user/public_html/newsletter/nindex.php on line 89

Warning: extract(): First argument should be an array in /home/user/public_html/newsletter/nindex.php on line 177


[PHP]<?php

// Newsletter Administration
// nindex.php

// MySQL Connection Variables (Enter the values)
$hostname='localhost';
$user='user';
$pass='password';
$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) or die(mysql_error());

?>
<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: newsletter/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) or die(mysql_error());

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;
}
?>
[/PHP]
Reply With Quote  
All times are GMT -4. The time now is 2:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC