Problem with my form validations?

Discussion in 'PHP' started by mokimofiki, Aug 24, 2008.

  1. #1
    The code below is where my form goes to validate the information on it. I need to be sure that the form is completely filled along with unique email address and player name. Any help would be appriciated thank you :)

    <?php
    
    $dbserver = "xxxxxx";
    $dblogin = "xxxxxx";
    $dbpassword = "xxxxxxxx";
    $dbname = "xxxxxxxx";
    
    $pname=$_POST['pname'];
    $pword=$_POST['pword'];
    $email=$_POST['email'];
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $country=$_POST['country'];
    $referrals=$_POST['referrals'];        
    
    //if no message entered and no playername entered print an error 
    if (empty($pname)){
    print "No Player Name was entered. <br>Please include your Requested in game Name.<br>";
    }
    //if no password entered send print an error 
    elseif (empty($pword)){
    print "No password was entered.<br>Please include a password.<br>";
    }
    //if no password entered send print an error 
    elseif (empty($fname)){
    print "No First Name was entered.<br>Please include your First Name.<br>";
    }
    //if no password entered send print an error 
    elseif (empty($lname)){
    print "No Last Name was entered.<br>Please include your Last Name.<br>";
    }
    //if no password entered send print an error 
    elseif (empty($country)){
    print "No Country was entered.<br>Please include Your Country.<br>";
    }
    //if no email entered send print an error 
    elseif (empty($email)){
    print "No email address was entered.<br>Please include your email. <br>";
    }
    //if the form has been completely filled out continue 
    else {
    
        $con = mysql_connect("$dbserver","$dblogin","$dbpassword");
    if (!$con)
      {
      die('Could not connect to the mySQL server please contact gangwars.mofiki.com technical support with the following information: ' . mysql_error());
      } 
      
    mysql_select_db("$dbname", $con);
    
    
    $result = mysql_query("SELECT pname FROM users WHERE pname = '$pname'");
    $num_rows = mysql_num_rows($result);
    
    if ($num_rows > 0){echo "Username $pname exists please press back and try again";}
    
    $result = mysql_query("SELECT email FROM users WHERE email = '$email'");
    $num_rows = mysql_num_rows($result);
    
    if ($num_rows > 0){echo "Email Address $pname has already been used please press back and try again";}
    
    
    $sql = mysql_query("INSERT INTO users (playerid, playername, password, email, firstname, lastname, country, referrals) VALUES ('','$_POST[pname]','$_POST[pword]','$_POST[email]','$_POST[fname]','$_POST[lname]','$_POST[country]','$_POST[referrals]')");
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "Thank you for registering you will recieve an email at each phase of Gang Wars release";
      
    mysql_close($con); 
    } 
    ?>
    Code (markup):
     
    mokimofiki, Aug 24, 2008 IP
  2. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Okay I have changed:
    $result = mysql_query("SELECT pname FROM users WHERE pname = '$pname'");
    $num_rows = mysql_num_rows($result);
    
    if ($num_rows > 0){echo "Username $pname exists please press back and try again";}
    
    $result = mysql_query("SELECT email FROM users WHERE email = '$email'");
    $num_rows = mysql_num_rows($result);
    
    if ($num_rows > 0){echo "Email Address $pname has already been used please press back and try again";}
    
    Code (markup):
    to:
    if(mysql_num_rows(mysql_query("SELECT pname FROM users WHERE pname = '$pname'")))
    {
    echo "Player Name $pname has already been used please press back and try again";
    }
    
    if(mysql_num_rows(mysql_query("SELECT pname FROM users WHERE pname = '$email'")))
    {
    echo "The Email Address $email has already been used please press back and try again";
    }
    Code (markup):
    Still not working please help :)
     
    mokimofiki, Aug 24, 2008 IP
  3. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What's not working about it?
    foreach ($_POST as $name => value) {
        if (strlen($value)==0)
            $errors .= "{name} was left blank!<br />";
    }
    //check for existing email address and username and add to $errors
    if ($errors=="") {
        //retrieve the data, add to db, output success message
    } else {
        echo "<b><font color=\"red\">There were errors in your request!</font></b><br/>{$errors}";
    }
    Code (markup):
     
    rcadble, Aug 24, 2008 IP
  4. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Error is:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 54

    line 54 is:

    $num_rows = mysql_num_rows($result);

    If you have time to explain line by line the code you have posted above I would be interested in learning it (i'm a noob as php still I just know the basics)

    Thank you
     
    mokimofiki, Aug 24, 2008 IP
  5. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You get that error because there's something wrong with the queries or the database. Make sure the database name, table name, user, pass, and server are valid. Additionally, add ";" inside each of your queries (i.e. mysql_query("SELECT * FROM `table`;");). Additionally, at the end of each query you can do something like
    $result = mysql_query("query here") or die(mysql_error());
     
    rcadble, Aug 24, 2008 IP
    mokimofiki likes this.
  6. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #6
    Thank you so much for your time I will review all of my query's and see where i'm having the problem at least know I have an idea of where to look.

    Thank you again :)
     
    mokimofiki, Aug 24, 2008 IP
  7. Feral

    Feral Active Member

    Messages:
    70
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #7
    right now your just saying where pname = $pname not the value stored in $pname;


    try
    
    mysql_query("SELECT pname FROM users WHERE pname = '{$pname}'");
    
    Code (markup):
    when using double quotes " " around your query the php parser will recognize the curly brackets {} and replace it with the contents of the variable.
     
    Feral, Aug 24, 2008 IP
  8. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #8
    so would this be correct? or is there a better way to write it?

    if(mysql_num_rows(mysql_query("SELECT pname FROM users WHERE pname = '{$pname}'")))
    {
    echo "Player Name $pname has already been used please press back and try again";
    }
    Code (markup):
     
    mokimofiki, Aug 24, 2008 IP