PHP Mysql error! Help needed!

Discussion in 'PHP' started by 812402, Jul 4, 2011.

  1. #1
    When you try to create an account, a mysql_num_rows error comes up. I checked my PHP book and my mysql command was fine and I couldn't detect another problem source. Here is the link to the script: http://812402.com/812/register.php Try to make an account and see the results. I will also give the code.

    <!DOCTYPE HTML 5>
    <html>
    <head>
    <title>Register</title>
    <link rel="stylesheet" type="text/css" href="812style.css">
    </head>
    <body>
    <a href="index.php"><img src="logo.png"></a>
    </body>
    </html>
    
    <?php
    
    if (isset($_SESSION['user_id'])) {
    
    $url = 'http://812402.com/812/index.php';
    
    header ( 'Location: $url' );
     
    }
    
    if(isset($_POST['submitted'])) {
    
    require ('config.php');
    
    
     if (isset($_POST['username'])) {
    
    $un = stripslashes($_POST[username]);
     } else {
    
     echo '<p align="center"><font color="red"><b>Please enter a username!</b></font color></p>';
    
     }
    
     if($_POST['age'] >'7') {
     $age = ($_POST[age]);
     $ageok ="true";
     } else {
     echo '<p align="center"><font color="red">You are to young to be using this site.</font color></p>';
     }
    
     if(isset($_POST['password'])) {
    $pw = stripslashes($_POST[password]);
    
    
     } else {
    
     echo '<p align="center"><font color="red"><b>Please enter a password!</b></font color></p>';
    
     }
    
     if(isset($_POST['password2'])) {
    
      if($_POST['password'] == $_POST['password2']) {
    $pw = stripslashes($_POST[password]);
    
      } else {
    
      echo '<font color="red"><b>The two passwords must match!</b></font color>';
    
      }
    
     } else {
    
     echo '<font color="red"><b>Please enter a confirming password!</b></font color>';
    
     }
    
    
    
    
    
    if (isset($_POST['email'])) {
    
    $e = stripslashes($_POST[email]);
    
    } else {
    echo '<p align="center><font color="red">Please enter your email/parents email!</font color></p>';
    }
    
    if ($u && $pw && $age && $e) {
     
     $query = "SELECT user_id FROM XXX WHERE email='$e'";
      $result = 'mysql_query ($query) or trigger_error("Error:" mysql_error())';
    
      
      $query2 = "SELECT user_id FROM XXX WHERE username='$u'";
      $result2 = 'mysql_query ($query2) or trigger_error("Error:" mysql_error())';
    
      $query3 = "SELECT user_id FROM XXX WHERE password='$pw'";
      $result3 = 'mysql_query ($query3) or trigger_error("Error:" mysql_error())';
    
    } else {
    echo '<p align="center"><font color="red">Sorry, but you could not be registered. Please try again later.</font color></p>';
    }
    
    if (mysql_num_rows($result) == 0)  {
     
    $eav = "true";
    
    } else {
    
    $eav = "false";
    
    echo '<p align="center">The email is already taken</p>';
    
    }
    if (mysql_num_rows($result2) == 0) {
    
    $uav = "true";
    
    } else {
    
    $uav = "false";
    
    echo '<p align="center">The username is already taken.</p>';
    
    }
    if (mysql_num_rows($result3) == 0) {
    
    $pav = "true";
    
    } else {
    
    $pav = "false";
    
    echo '<p aliagn="center">The password is already taken</p>';
    
    }
     
    if ($eav && $uav && $pav && $ageok == "true") {
    
    echo '<h4 align="center">Thanks for registering! An email has been sent to you that will activate your account.<h4>';
    
    $a = md5(uniqid(rand(), true));
    
    $query = "INSERT INTO XXX (username, password, email, age, role, active, date_registered) VALUES ('$u', '$pw', '$e, '$age', '1', '$a', NOW() )"; 
    $result = 'mysql_query ($query) or trigger_error(Error mysql_error())';
    
    
    
    $body = 'Thank you for registering! Your activation link is http://812402.com/812/activate.php?id=". mysql_insert_id() . "code=$a"';
    mail($_POST['email'],
    'Confirm Email',
    $body);
    
     
    } else {
    
    echo 'Please try again.';
    
    }};
    
    
    
    
    ?>
    
    
    
    <form method="post" action="register.php">
    
    <h4 align="center">username</h4><p align="center"><input type="text" maxlength="45" name="username"></p>
    <h4 align="center">password</h4><p align="center"><input type="password" name="password" maxlength="50"></p>
    <h4 align="center">confirm password</h4><p align="center"><input type="password" name="password2" maxlength="50"></p>
    <h4 align="center">email/parents email</h4><p align="center"><input type="text" name="email" maxlength="50"></p>
    <h4 align="center">age
    <p align="center">
    <select name="age">
    <option value="3">3
    <option value="4">4
    <option value="5">5
    <option value="6">6
    <option value="7">7
    <option value="8" selected>8
    <option value="9">9
    <option value="10">10
    <option value="11">11
    <option value="12">12
    <option value="13">13+
    </select></p>
    <input type="hidden" name="submitted" value="TRUE">
    <p align="center"><input type="submit" value="Join!"></p>
    
    </form>
    PHP:
    Please comment if you have a way to fix it. (and if you think I should add something to my script.)
     
    812402, Jul 4, 2011 IP
  2. akshat.gl

    akshat.gl Member

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    $un = stripslashes($_POST[username]);
    and so on...

    it should be
    $un = stripslashes($_POST['username']);

    You missed the quotes when initializing variables.

    Thanks,
    Akshat Goel
     
    akshat.gl, Jul 4, 2011 IP
  3. akshat.gl

    akshat.gl Member

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    On long run, you would face another problem of "headers were already sent". Shift these lines to page top even before HTML
    and yeah, start a session too..

    session_start();
    if (isset($_SESSION['user_id'])) {

    $url = 'http://812402.com/812/index.php';

    header ( 'Location: $url' );

    }
     
    akshat.gl, Jul 4, 2011 IP
  4. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #4
    Ouch, there is a lot of errors in your code. Please look around on php.net regarding single (') and double (") quotes, when and where to use them.

    Here is a corrected code. I don't know whether it works or not, I didn't test it. However there is a lot to do here. I mean security flaws and SQL injection, you can google this topic. (mysql_real_escape_string, prepared statements, etc.)

    You should also read more about HTML, w3schools.com is a good place to start. I would get rid of "align="center"" and "font color="red">" there, you should use CSS for these.

    I hope it helps.

    
    <?php 
    
    if ( isset($_SESSION['user_id']) ) {
    	
    	$url = 'http://812402.com/812/index.php';
    	header ( "Location: $url" );
    }
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Register</title>
    <link rel="stylesheet" type="text/css" href="812style.css">
    </head>
    <body>
    <a href="index.php"><img src="logo.png"></a>
    
    <?php
    
    if ( isset($_POST['submitted']) ) {
    	
    	require ('config.php');
    	
    	if ( isset($_POST['username']) ) {
    		
    		$un = stripslashes($_POST['username']);
    	}
    	else {
    		
    		echo '<p align="center"><font color="red"><b>Please enter a username!</b></font color></p>';
    	}
    	
    	if ( $_POST['age'] > 7 ) {
    		
    		$age   = $_POST['age'];
    		$ageok = true;
    	}
    	else {
    
    		echo '<p align="center"><font color="red">You are to young to be using this site.</font></p>';
    	}
    	
    	if ( isset($_POST['password']) ) {
    		
    		$pw = stripslashes($_POST['password']);
    	}
    	else {
    		
    		echo '<p align="center"><font color="red"><b>Please enter a password!</b></font></p>';
    	}
    	
    	if ( isset($_POST['password2']) ) {
    		
    		if ( $_POST['password'] == $_POST['password2'] ) {
    			
    			$pw = stripslashes($_POST['password']);
    		}
    		else {
    			
    			echo '<font color="red"><b>The two passwords must match!</b></font>';
    		}
    	}
    	else {
    		
    		echo '<font color="red"><b>Please enter a confirming password!</b></font>';
    	}
    	
    	if ( isset($_POST['email']) ) {
    		
    		$e = stripslashes($_POST['email']);
    	}
    	else {
    		
    		echo '<p align="center><font color="red">Please enter your email/parents email!</font></p>';
    	}
    	
    	if ( $u && $pw && $age && $e ) {
    		
    		$query   = "SELECT user_id FROM XXX WHERE email='$e'";
    		$result  = mysql_query($query) or trigger_error('Error:' .mysql_error());
    		
    		$query2  = "SELECT user_id FROM XXX WHERE username='$u'";
    		$result2 = mysql_query($query2) or trigger_error('Error:' .mysql_error());
    		
    		$query3  = "SELECT user_id FROM XXX WHERE password='$pw'";
    		$result3 = mysql_query($query3) or trigger_error('Error:' .mysql_error());
    	}
    	else {
    		
    		echo '<p align="center"><font color="red">Sorry, but you could not be registered. Please try again later.</font color></p>';
    	}
    	
    	if ( mysql_num_rows($result) == 0 ) {
    		
    		$eav = true;
    	}
    	else {
    		
    		$eav = false;
    		echo '<p align="center">The email is already taken</p>';
    	}
    	
    	if ( mysql_num_rows($result2) == 0 ) {
    		
    		$uav = true;
    	}
    	else {
    		
    		$uav = false;
    		echo '<p align="center">The username is already taken.</p>';
    	}
    	
    	if ( mysql_num_rows($result3) == 0 ) {
    		
    		$pav = true;
    	}
    	else {
    		
    		$pav = false;
    		echo '<p aliagn="center">The password is already taken</p>';
    	}
    	
    	if ( true == $eav && true == $uav && true == $pav && true == $ageok ) {
    		
    		echo '<h4 align="center">Thanks for registering! An email has been sent to you that will activate your account.<h4>';
    		
    		$a = md5(uniqid(rand(), true));
    		
    		$query  = "INSERT INTO XXX (username, password, email, age, role, active, date_registered) ";
    		$query .= "VALUES ('$u', '$pw', '$e, '$age', '1', '$a', NOW())";
    		$result = mysql_query ($query) or trigger_error('Error:' .mysql_error());
    		
    		$body = 'Thank you for registering! Your activation link is http://812402.com/812/activate.php?id='. mysql_insert_id() ."&amp;code=$a";
    		
    		mail($_POST['email'], 'Confirm Email', $body);
    	}
    	else {
    		
    		echo 'Please try again.';
    	}
    }
    
    ?>
    
    <form method="post" action="register.php">
    
    <h4 align="center">username</h4>
    <p align="center"><input type="text" maxlength="45" name="username" /></p>
    <h4 align="center">password</h4>
    <p align="center"><input type="password" name="password" maxlength="50" /></p>
    <h4 align="center">confirm password</h4>
    <p align="center"><input type="password" name="password2" maxlength="50" /></p>
    <h4 align="center">email/parents email</h4>
    <p align="center"><input type="text" name="email" maxlength="50" /></p>
    <h4 align="center">age
    <p align="center">
    <select name="age">
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8" selected="selected">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13+</option>
    </select></p>
    <input type="hidden" name="submitted" value="TRUE" />
    <p align="center"><input type="submit" value="Join!" /></p>
    </form>
    </body>
    </html>
    
    PHP:
     
    Last edited: Jul 4, 2011
    bogi, Jul 4, 2011 IP
  5. 812402

    812402 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for trying, but the error is still there.
     
    812402, Jul 5, 2011 IP