Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Wondering If I'm Having A Code Problem Or If Its My Database
lonebyrd
post May 22 2006, 03:29 AM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 302
Joined: 23-February 06
From: Northeastern Connecticut USA
Member No.: 11,487



I'm still trying to figure out my login/registration page. It's probably a problem with my database itself. I have come up with this error on my webpage:

QUOTE
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/lonebyrd/public_html/registration.php on line 93


This is the code for that line:
CODE
if(mysql_num_rows(mysql_query($nameq)) > 0)


Before that is probably what matters so I will give that too. Here it is:

CODE
elseif(isset($_POST['register'])) {
            $name = mysql_real_escape_string($_POST['ad_name']);
            $email = mysql_real_escape_string($_POST['ad_email']);
            $nickname = mysql_real_escape_string ($_POST['ad_nickname']);
            $password = mysql_real_escape_string($_POST['ad_password']);
            $location = mysql_real_escape_string($_POST['ad_location']);
            $gender = mysql_real_escape_string ($_POST['ad_gender']);
            $date_of_birth = mysql_real_escape_string ($_POST['ad_date_of_birth']);
            
            $activation_code = generateCode(25);
            $nameq = "SELECT ad_name FROM registration WHERE ad_name = $name LIMIT 1";
            $emailq = "SELECT ad_email FROM registration WHERE ad_email = $email LIMIT 1";
            $nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = $nickname LIMIT 1";
            }
            //put errors into an array I need to change these if I change the db
            if(empty($name)) {
                $errors[] = "The name field was blank! <br />";
            }
            if(mysql_num_rows(mysql_query($nameq)) > 0)
            {    $errors[] = "The name given is already in use! Please try another one! <br />";


Like I said, it's probably a problem in the way my database is set-up but, I am very new to all this.
Go to the top of the page
 
+Quote Post
yordan
post May 22 2006, 11:33 AM
Post #2


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 1,964
Joined: 16-August 05
Member No.: 7,896



Somebody more skilled shoud have a look at your code, but it seems that the supplied name is not in the atabase, so the number of answers does not exist.

This post has been edited by yordan: May 22 2006, 08:45 PM
Go to the top of the page
 
+Quote Post
vizskywalker
post May 22 2006, 04:39 PM
Post #3


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



It's a code problem. mysql_num_rows requires, like any mysql command, tat you be connected to a mysql server and have picked a database. So first you must use mysql_connect() and then mysql_select_db();

~Viz
Go to the top of the page
 
+Quote Post
Logan Deathbring...
post May 24 2006, 02:23 AM
Post #4


Geek in-training
Group Icon

Group: Members
Posts: 301
Joined: 2-July 05
From: Washington State, USA, 3rd Rock from the Star Sol
Member No.: 6,772



and Viz saves the day again...lol. I was about to tell him the same thing but you beat me to it...

But ya Viz is correct if you make/update those entries then you should be able to fix your problem. If you keep gettting errors let us know.
Go to the top of the page
 
+Quote Post
lonebyrd
post May 24 2006, 04:35 AM
Post #5


Premium Member
Group Icon

Group: Members
Posts: 302
Joined: 23-February 06
From: Northeastern Connecticut USA
Member No.: 11,487



I have my database info in different files. But at the top of the script, I have it try to locate it. Maybe I am doing it wrong. Here is what it says:

CODE
<?php
require("config.php");
include("login.php");
require("functions.php");
?>
Go to the top of the page
 
+Quote Post
vizskywalker
post May 24 2006, 04:39 AM
Post #6


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



Without seeing the various scripts and the main page in its entirety this will be incredibly difficult to solve. Please, post all relevant code. Unless you are absolutely sure (would stake your programming career) that a piece of code is not the cause, include it. This includes any external files that are accessed by the script.

~Viz
Go to the top of the page
 
+Quote Post
lonebyrd
post May 24 2006, 04:59 AM
Post #7


Premium Member
Group Icon

Group: Members
Posts: 302
Joined: 23-February 06
From: Northeastern Connecticut USA
Member No.: 11,487



Ok.. I'm a newbie at this so I'll give you all the coding. Here are the scripts for the files given at the beginning.

config.php
CODE
<?php
$nickname = "lonebyrd";
$password = "mypassword";
$database = "lonebyrd_FTV";
$server = "localhost";
?>


login.php
CODE
<?php
mysql_connect($server, $nickname, $password) or die (mysql_error());
mysql_select_db($database) or die (mysql_error());
?>


functions.php
CODE
<?php
function generateCode($length = 10)
{
   $password="";
   $char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
   srand((double)microtime()*1000000);
   for ($i=0; $i<$length; $i++)
   {
      $password = $password . substr ($chars, rand() % strlen($chars), 1);
   }
   return $password;
}
?>


registration.php
CODE
<?php
require("config.php");
include("login.php");
require("functions.php");
?>
<HTML>
<HEAD>
<TITLE>Registration---Fantasized TeleVision</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<style type="text/css">

BODY {
    background:#808080;
    text-align:center;
    font-family:Verdana, Arial;
    font-weight:bold;
    font-size:12px;
    color:#FF0000;
    }
      
register_box
    {
    border: 1px solid #0000A0;
    background-color: #0000FF;
    font-family: Verdana, Arial;
    font-weight: bold;
    font-size: 9px;
    color: #FF0000;
    top margin: 200px
    left margin: 200px
    }

</style>
</head>
<body>
<?php
// The following line verifies which action is requested by the user
// by default is setting to new if nothing is requested
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "new";
switch($action) {
    //--------------------------------------
    //       [New Registration]
    //--------------------------------------
      
      case "new":
        if(!isset($_POST['register'])) {
            echo "
            <form action='registration.php' method='POST'>
                Name: <br />
                <input type='text' name='name' class='register_box'>
                <br />
                Email: <br />
                <input type='text' name='email' class='register_box'>
                <br />
                Nickname: <br />
                <input type='text' name='nickname' class='register_box'>
                <br />
                Password: <br />
                <input type='password' name='password' class='register_box'>
                <br />
                Location: <br />
                <input type='text' name='location' class='register_box'>
                <br />
                Gender: <br />
                <input type='text' name='gender' class='register_box'>
                <br />
                Date_of_Birth: <br />
                <input type='date' name='date_of_birth' class='register_box'>
                <br />
                <input type='submit' name='register' value='New Registration!' class='register_box'>
                <input type='hidden' name='action' value='new'>
            </form>
            ";
        }
        elseif(isset($_POST['register'])) {
            $name = mysql_real_escape_string($_POST['ad_name']);
            $email = mysql_real_escape_string($_POST['ad_email']);
            $nickname = mysql_real_escape_string ($_POST['ad_nickname']);
            $password = mysql_real_escape_string($_POST['ad_password']);
            $location = mysql_real_escape_string($_POST['ad_location']);
            $gender = mysql_real_escape_string ($_POST['ad_gender']);
            $date_of_birth = mysql_real_escape_string ($_POST['ad_date_of_birth']);
            
            $activation_code = generateCode(25);
            $nameq = "SELECT ad_name FROM registration WHERE ad_name = $name LIMIT 1";
            $emailq = "SELECT ad_email FROM registration WHERE ad_email = $email LIMIT 1";
            $nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = $nickname LIMIT 1";
            }
            //put errors into an array I need to change these if I change the db
            if(empty($name)) {
                $errors[] = "The name field was blank! <br />";
            }
            if(mysql_num_rows(mysql_query($nameq)) > 0)
            {    $errors[] = "The name given is already in use! Please try another one! <br />";
            }

            if(empty($email)) {
                    $errors[] = "The email field was blank! <br />";
            }
            if(mysql_num_rows(mysql_query($emailq)) > 0) {
                $errors[] = "The email given is already in use! Please try another one! <br />";
            }
            
            if(empty($nickname)) {
                $errors[] = "The nickname field was blank! <br />";
            }
            if(mysql_num_rows(mysql_query($nicknameq))>0) {
                $error[] = "That nickname is already taken.  Please try another one.<br />";
            }
            
            if(empty($password)) {
                $errors[] = "The password field was blank! <br />";
            }

            if(empty($location)) {
                $errors[] = "The location field was blank! <br />";
            }

            if(empty($gender)) {
                $errors[] = "The gender field was blank! <br />";
            }

            if(empty($date_of_birth)) {
                $errors [] = "No date of birth was entered! <br />";
            }
            if(count($errors) > 0) {
              foreach($errors as $err) {
                echo $err;
            }
            }
            else {
              $sqlq = "INSERT INTO registration (ad_name, ad_email, ad_nickname, ad_password, ad_location, ad_gender, ad_date_of_birth, is_activated, activation_code)";
              $sqlq .= " VALUES ('$name', '$email', '$nickname', '".md5($password)."', '$location', '$gender', '$date_of_birth', '0', '$activation_code')";
              mysql_query($sqlq) or die(mysql_error());
              echo "Thanks for registering! You will recieve an email shortly containing your validation code, and a link to activate your account!";
              mail($email, "New Registration, www.ftv.astahost.com", "Thanks for registering on FTV.\n\nHere are your login details:\n\nNickname: ".$nickname."\n\nPassword: ".$password."\n\nIn order to login and gain full access, you must validate your account.\n\nClick here to validate:\n\nhttp://www.ftv.astahost.com/registration.php?action=activate&nickname=".$nickname."&code=".$activation_code."\n\nThanks!\n\n[lonebyrd]");              
// \n is equal to a new line
// in your original version you use the variable user to send the nickname data
// to your activation file and in that file you check for a variable nickname, //so it never work.
        }
        }
    
?>
</body>
</html>


I also have an activation.php file for after the registration process. If you have any suggestions, or see anything wrong with this, please let me know.
Go to the top of the page
 
+Quote Post
minnieadkins
post May 24 2006, 05:36 PM
Post #8


Premium Member
Group Icon

Group: Members
Posts: 292
Joined: 15-December 04
Member No.: 1,768



Perhaps I'm overlooking something, but mysql_num_rows is apparently not receiving the correct object. I'm guessing it's a problem with your query. I'm fairly 'noobish' at this as well but if you're using a text field in your database or charvar, then your statements field should be enclosed in quotes. Correct me if I'm wrong here...and I may be, but:
QUOTE
CODE

$nameq = "SELECT ad_name FROM registration WHERE ad_name = $name LIMIT 1";
            $emailq = "SELECT ad_email FROM registration WHERE ad_email = $email LIMIT 1";
            $nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = $nickname LIMIT 1";

Would that work? or would this work?
CODE

$nameq = "SELECT ad_name FROM registration WHERE ad_name = '$name' LIMIT 1";
$emailq = "SELECT ad_email FROM registration WHERE ad_email = '$email' LIMIT 1";
$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = '$nickname' LIMIT 1";


I usually do this when working with variables, because I encounter errors when variables are in quotes. Some variables work, but larger more complex variables, and superglobals often times have trouble.

CODE

$nameq = "SELECT ad_name FROM registration WHERE ad_name = '" .  $name . "' LIMIT 1";
$emailq = "SELECT ad_email FROM registration WHERE ad_email = '" . $email . "' LIMIT 1";
$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = '" . $nickname . "' LIMIT 1";


Put an 'OR DIE' at the end of your query, or just echo out your query and copy and paste it into your SQL command prompt in phpMyAdmin and see if it executes properly.
Go to the top of the page
 
+Quote Post
yordan
post May 24 2006, 08:19 PM
Post #9


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 1,964
Joined: 16-August 05
Member No.: 7,896



QUOTE
copy and paste it into your SQL command prompt in phpMyAdmin and see if it executes properly.

The old good tricks which stil work well : that's usually named "debugging" ; write down the queries and see if they give a correct result, and then put them in a program. Oh, remembering the old good ancient times! rolleyes.gif
Go to the top of the page
 
+Quote Post
lonebyrd
post May 25 2006, 03:30 AM
Post #10


Premium Member
Group Icon

Group: Members
Posts: 302
Joined: 23-February 06
From: Northeastern Connecticut USA
Member No.: 11,487



Ok, I've tried the two different ways of placing the quotes, but it was still the same results. The other things, adding 'OR DIE' and echoing out the query and putting it into a sql command prompt, i just dont understand. But one thing I don't understand is, I went through and put in all the data into my registration page on my website, and it said thank you for registering, there was no errors there. I'm lost.
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Error 406 - Problem In My Phpbb Forum(8)
  2. Creating A Game In Rpg Maker 2000/2003(18)
  3. Do You Program/code Your Own Games(11)
  4. Mirror My MySQL Database To Another Mysql Server(4)
  5. How To: Connect, Read, Write, Close A Database(3)
  6. Problem: I Need Urgent Help. Can't Boot.(9)
  7. Need Help With A PHP - MySQL Registration Script(13)
  8. Uploading Image File Through JSP Code To Server(9)
  9. Dynamically Change The Background Image On Mouse Effects!(3)
  10. Photoshop Cropping Problem(6)
  11. Need To Hack An Admin Account On Xp... No Problem!(61)
  12. Spam Problem On My Forums(25)
  13. Frustrating Problem With XP On Laptop(19)
  14. Problem With Drag And Drop (or So It Seems).(11)
  15. Internet Explorer 7 Problem(8)
  1. Fedora Core 6 Install Problem(6)
  2. Trojan / Virus Problem ,please Help(17)
  3. A Gaiaonline Problem(8)
  4. Administrator Account Problem In Microsoft Xp [solved](20)
  5. Accessing Ms Access Database From A Centralized Location?(5)
  6. Mysql Database Management(1)
  7. Mysql Database Entry By Excel Sheets(2)
  8. Php Random Selector(2)
  9. Space Needed For Database(10)
  10. Database Access On Remote Server W/jsp(0)
  11. Activation Code(7)
  12. Some Useful Database Links.(5)
  13. Instant Replay Code?(0)