Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 7th, 2004, 11:21 AM
Registered User
 
Join Date: Apr 2004
Location: , , United Kingdom.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mailing Lists - Chapter 13

Whenever I try to subscribe to a new newsletter I get the error:
" Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/jeremy/public_html/mailing_lists/user_transact.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /home/jeremy/public_html/mailing_lists/user_transact.php:37) in /home/jeremy/public_html/mailing_lists/user_transact.php on line 136"

My code for the page user_transact.php is:

<?php
require('config.php');

$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
 or die('Could not connect to MySQL database. ' . mysql_error());

mysql_select_db(SQL_DB,$conn);

if (isset($_REQUEST['action']))
{
 $headers = "From: " . ADMIN_E-MAIL . "\r\n";
 switch ($_REQUEST['action'])
 {
  case 'Remove':
   $sql = "SELECT user_id FROM ml_users " .
       "WHERE e-mail='" . $_POST['e-mail'] . "';";
   $result = mysql_query($sql,$conn);

   if (mysql_num_rows($result))
   {
    $row = mysql_fetch_array($result);
    $user_id = $row['user_id'];
    $url = "http://" . $_SERVER['HTTP_HOST'] .
       dirname($_SERVER['PHP_SELF']) .
       "/remove.php?u=" . $user_id .
       "&ml=" . $_POST['ml_id'];
    header("Location: $url");
    exit();
   }
   $redirect = 'user.php';
   break;
  case 'Subscribe':
   $sql = "SELECT user_id FROM ml_users " .
       "WHERE e-mail='" . $_POST['e-mail'] . "';";
   $result = mysql_query($sql,$conn);

   if (!mysql_num_rows($result))
   {
    $sql = "INSERT INTO ml_users " .
        "(firstname,lastname,e-mail) ".
        "VALUES ('" . $_POST['firstname'] . "'," .
        "'" . $_POST['lastname'] . "'," .
        "'" . $_POST['e-mail'] . "');";
    $result = mysql_query($sql, $conn);
    $user_id = mysql_insert_id($conn);
   }
   else
   {
    $row = mysql_fetch_array($result);
    $user_id = $row['user_id'];
   }

   $sql = "INSERT INTO ml_subscriptions (user_id,ml_id) " .
       "VALUES ('" . $user_id . "','" . $_POST['ml_id'] . "')";
   mysql_query($sql,$conn);

   $sql = "SELECT listname FROM ml_lists " .
       "WHERE ml_id=" . $_POST['ml_id'];
   $result = mysql_query($sql,$conn);
   $row = mysql_fetch_array($result);
   $listname = $row['listname'];

   $url = "http://" . $_SERVER['HTTP_HOST'] .
      dirname($_SERVER['PHP_SELF']) .
      "/user_transact.php?u=" . $user_id .
      "&ml=" . $_POST['ml_id'] . "&action=confirm";

   $subject = 'Mailing list confirmation';
   $body = "Hello " . $_POST['firstname'] . "\n" .
      "Our records indicate that you have subscribed to the " .
      $listname . " mailing list.\n\n" .
      "If you did not subscribe, please accept our apologies. " .
      "You will not be subscribed if you do not visit the " .
      "confirmation URL.\n\n" .
      "If you subscribed, please confirm this by visiting the " .
      "following URL:\n" . $url;

   mail($_POST['e-mail'],$subject,$body,$headers);

   $redirect = "thanks.php?u=" . $user_id . "&ml=" .
      $_POST['ml_id'] . "&t=s";
   break;


  case 'confirm':

   if (isset($_GET['u']) & isset($_GET['ml']))
   {
    $sql = "UPDATE ml_subscriptions SET pending=0 " .
        "WHERE user_id=" . $_GET['u'] .
        " AND ml_id=" . $_GET['ml'];
    mysql_query($sql, $conn);

    $sql = "SELECT listname FROM ml_lists " .
        "WHERE ml_id=" . $_GET['ml'];
    $result = mysql_query($sql,$conn);

    $row = mysql_fetch_array($result);
    $listname = $row['listname'];

    $sql = "SELECT * FROM ml_users " .
        "WHERE user_id='" . $_GET['u'] . "';";
    $result = mysql_query($sql,$conn);
    $row = mysql_fetch_array($result);
    $firstname = $row['firstname'];
    $e-mail = $row['e-mail'];

    $url = "http://" . $_SERVER['HTTP_HOST'] .
        dirname($_SERVER['PHP_SELF']) .
        "/remove.php?u=" . $_GET['u'] .
        "&ml=" . $_GET['ml'];

    // Send out confirmed e-mail
    $subject = 'Mailing List Subscription Confirmed';
    $body = "Hello " . $firstname . ",\n" .
       "Thank you for subscribing to the " .
       $listname . " mailing list. Welcome!\n\n" .
       "If you did not subscribe, please accept our apologies.\n".
       "You can remove this subscription immediately by ".
       "visiting the following URL:\n" . $url;

    mail($e-mail,$subject,$body,$headers);

    $redirect = "thanks.php?u=" . $_GET['u'] . "&ml=" .
       $_GET['ml'] . "&t=s";
   } else {
    $redirect = 'user.php';
   }
   break;

  default:
   $redirect = 'user.php';
 }
}

header('Location: ' . $redirect);

?>



Reply With Quote
  #2 (permalink)  
Old August 4th, 2004, 09:23 AM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I'm also receiving the same errors as Funky Monk from the user_transact script:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hsphere/local/home/jamcp/jamesmcpherson.co.uk/kg_testing/user_transact.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/jamcp/jamesmcpherson.co.uk/kg_testing/user_transact.php:37) in /hsphere/local/home/jamcp/jamesmcpherson.co.uk/kg_testing/user_transact.php on line 136


I've checked the Errata for the book and changed all occurances of $e-mail to $email as recommended.

As a newbie I'm unclear on how to implement the suggestions of Richard - can anyone suggest a solution to this problem?:(

TIA
Reply With Quote
  #3 (permalink)  
Old August 4th, 2004, 10:18 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The second error that you're seeing is a result of the first error. The header() function modifies the outgoing HTTP headers. The HTTP headers *must* go out before any content, e.g. the HTML document itself.

The first error that you're seeing is the real problem. mysql_num_rows is complaining that the resource handle passed to it isn't valid, this is the variable that's created when you call on mysql_query.

$result = mysql_query('Query stuff');

The $result variable is what's called a resource handle, it contains a pointer to the information contained in that query, but not the information itself. This pointer let's you call on functions like mysql_num_rows or mysql_fetch_array, which are functions that look at the information that pointer is referring to to tell you what you want to know about the query. When you see that error, then, that means that something went wrong with the creation of that resource pointer, most likely indicating an error in the SQL syntax used in the query. So adding a simple test of the pointer and functions that retrieve those potential errors tell you more about what went wrong and help you to correct the errors.

What's going on here is the $result variable is created after a call to mysql_query, directly after that call to mysql_query and the creation of the resource pointer, put in a check of that pointer to see if anything went wrong, if anything did tell the program to spit out some errors... this is done with the following snippet.

if (!$result)
{
    echo mysql_error()."<br />\n";

    // I'd even go so far as to dump out the $sql variable itself
    echo $sql."<br />\n";
}

This says if the resource pointer is FALSE, output some errors to find out what went wrong. If the resource was created successfully, it won't be FALSE and PHP will pass over this snippet of code. Does that make sense?

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
  #4 (permalink)  
Old August 4th, 2004, 04:52 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am new to Apache and php...
I followed the directions in the appendix and on pps. 15-16; after installation of Apache I got the success page. I customized the installation adding php with AddType statements (both of them), and then later on page 15 the instuctions state to enter LoadModule php4_module d:/php/sapi/php4apapche2.dll which I did, and then saved the changes. I made the changes stated on page 16, and I created a directory called test, entered the index.html text, but am unable to view the page. The error I get states that the module php4apache2.dll cannot be found, and yet when I checked in the files php/sapi/php4apache2.dll is just where it is supposed to be. Why can't the module be loaded and what must I do to get it to load? Thanks!

Reply With Quote
  #5 (permalink)  
Old August 4th, 2004, 05:05 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi saross, your question isn't on topic for this thread, please start a new thread or ask your question in a thread containing the same topic. Besides making more sense, this helps future forum visitors to find answers to their questions too.

That said, did you copy the php4ts.dll to the system32 directory?
http://www.php.net/manual/en/install.apache2.php

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
  #6 (permalink)  
Old August 4th, 2004, 10:04 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry, but I purchased the Beginning PHP, Apache, MySQL Web Development book by Wrox...I thought that this was the site to have those questions answered...

Reply With Quote
  #7 (permalink)  
Old August 4th, 2004, 10:13 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well you have the right forum but how does this thread with the topic of *mailing lists - Chapter 13* fit the topic *installation problems*?!?

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
  #8 (permalink)  
Old August 4th, 2004, 10:33 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

btw...Appendix I only mentions that the phpt4s.dll should be copied to d:/Program Files/Apache Group/Apache2/bin, which was what I did... After this post I promise I will find a thread on a similar subject or start a new one...

Reply With Quote
  #9 (permalink)  
Old August 5th, 2004, 07:41 AM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help Richard and taking time-out to explain the problem. After inserting the code you suggested I received this error:

unknown column 'e-mail' in 'where clause'

This pointed out that even though I had changed all instances of $e-mail to $email in my script I hadn't changed the name of the field in the database.

Once that was changed it works fine - thanks!

Reply With Quote
  #10 (permalink)  
Old August 6th, 2004, 12:03 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

No worries ;) Glad to be of help!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 bwoll BOOK: Beginning Access 2003 VBA 1 June 7th, 2007 03:57 PM
Chapter 13 ElMorenito BOOK: Beginning ASP 3.0 0 January 14th, 2005 02:56 PM



All times are GMT -4. The time now is 01:28 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.