TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-13-2009, 02:59 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default Need some help with an error

Ok so I've been trying to teach myself php and am doing pretty good so far. I am making a little newsletter sign up for a site and it works fine, but I get an error. From what I understand this error should not allow the script to continue if it happens, but it doesn't and the script runs. The error is:
Code:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
And like I said the script runs fine, might not be the best way to do it, its a start. Here is what I have done.
Code:
if(isset($_POST['submit'])) {
		//Set variables.
		$firstname = strip_tags($_POST['firstname']);
		$lastname = strip_tags($_POST['lastname']);
		$email = $_POST['emailaddress'];
		$accept = $_POST['accept'];
		
		// Check for empty fields.
		if (empty($firstname) || empty($lastname) || empty($emailaddress)) {
			$message = "Please fill in all fields!";
		} else {
			$message = "All fields have been filled in!";
			// If successful, continue sign up proccess.
			// Check to see if info already exist.
			$sql = "SELECT * FROM mailing_list WHERE firstname = '$firstname' AND lastname = '$lastname' AND email = '$emailaddress'";
			$result = mysql_query($sql) or die(mysql_error());
			if(mysql_num_rows($result) != 0) {
				$message = "This information already exist!";
			} else {
				// If information doesn't exist, add it.
				$sql = "INSERT INTO mailing_list (id, firstname, lastname, email, accept) VALUES (null, '$firstname', '$lastname', '$emailaddress', '$accept')";
				$result = mysql_query($sql) or die(mysql_error());
				if(mysql_num_rows($result) != 0) {
					$message = "Users information was not added!";
				} else {
					$message = "Users information was added successfully!";
				}
			}
		}
	} else {
	}
How can I fix what is triggering this error or stop it from showing?
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-14-2009, 12:08 AM   #2 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

Correct me if I'm wrong but not all browses seem to send a undefined value for blank form fields, some tend to send '' (aka, nothing [blank string]).

PHP Code:
function is_empty($value){
    if (
$value === '' || empty($value)){
         return 
false;
    }
return 
true;
}

if (
is_empty($firstname) || is_empty($lastname) || is_empty($emailaddress)) {
            
$message "Please fill in all fields!";
        } else { 
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 10-24-2009, 07:38 PM   #3 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

You're using mysql_num_rows() in 2 places and I'm guessing that it is the second one (after the INSERT) that is causing the error?

mysql_num_rows() only works on SELECT statements, for your INSERT statement, try using mysql_affected_rows() instead - that should return 1 if the new info was inserted into the database, or -1 if there was a problem.

More info: http://uk.php.net/manual/en/function...ected-rows.php
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Parse error: parse error,syntex error unexpected ',' expectingT_STRING in my code tech Absolute Beginners 13 01-29-2013 10:00 AM
cant work this error out talonos Absolute Beginners 11 07-13-2009 11:03 AM
Can't solve browser error Peuplarchie Absolute Beginners 1 06-09-2008 05:48 AM
Keep getting mySQL error No. 1064, but i can't seem to find the problem Durux MySQL & Databases 8 04-13-2008 07:51 PM
an error makes me crazy nuts !!!!!!! webtuto Absolute Beginners 1 02-27-2008 10:31 AM


All times are GMT. The time now is 02:26 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design