TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Need some help with an error (http://www.talkphp.com/absolute-beginners/5020-need-some-help-error.html)

code_junkie 10-13-2009 02:59 PM

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?

adamdecaf 10-14-2009 12:08 AM

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 { 


Alan @ CIT 10-24-2009 07:38 PM

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


All times are GMT. The time now is 06:43 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0