#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Problem with mysql and form


    i get this error
    Code:
    ( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/jharvard/public_html/pset7/register2.php on line 23
    Call Stack
    #	Time	Memory	Function	Location
    1	0.0004	326808	{main}( )	../register2.php:0
    2	0.0220	356268	mysql_num_rows ( )	../register2.php:23
    this is my code for register2.php
    PHP Code:
    <?

        
    // require common code
        
    require_once("includes/common.php"); 

        
    // escape username to avoid SQL injection attacks
        
    $username mysql_real_escape_string($_POST["username"]);
        
        
    // encrypted password
        
    $password crypt($_POST["password"]);

        
    // prepare SQL
        
    $sql "INSERT INTO users (username, hash, cash) 
                VALUES (
    $username$password, 10000)";
        
        
    // TESTING        
        // die($sql."<br/><br/>".mysql_error());     

        // execute query
        
    $result mysql_query($sql);

        
    // if we found user, check password
        
    if (mysql_num_rows($result) == 1)
        {
            
    // grab row
            
    $row mysql_fetch_array($result);

            
    // compare hash of user's input against hash that's in database
            
    if (crypt($_POST["password"], $row["hash"]) == $row["hash"])
            {
                
    // remember that user's now logged in by caching user's ID in session
                
    $_SESSION["id"] = $row["id"];

                
    // redirect to portfolio
                
    redirect("index.php");
            }
        }

        
    // else report error
        
    apologize("Invalid username and/or password!");

    ?>
    this is my code for register.php
    PHP Code:
    <?

        
    // require common code
        
    require_once("includes/common.php");

    ?>

    <!DOCTYPE html>

    <html>

      <head>
        <link href="css/styles.css" rel="stylesheet" type="text/css">
        <title>C$50 Finance: Log In</title>
      </head>

      <body>

        <div id="top">
          <a href="index.php"><img alt="C$50 Finance" src="images/logo.gif"></a>
        </div>

        <div id="middle">
          <form action="register2.php" method="post">
            <table>
              <tr>
                <td>Username:</td>
                <td><input name="username" type="text"></td>
              </tr>
              <tr>
                <td>Password:</td>
                <td><input name="password" type="password"></td>
              </tr>
                <tr>
                <td>Repeat Password:</td>
                <td><input name="password" type="password2"></td>
              </tr>
              <tr>
                <td colspan="2"><input type="submit" value="Register"></td>
              </tr>
            </table>
          </form>
        </div>

        <div id="bottom">
          or <a href="login.php">log in</a> 
        </div>

      </body>

    </html>
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,867
    Rep Power
    6351
    The error means your query is failing, and you're not properly handling the failure condition with any error checking. Right off the top of my head, you're missing quotes in your INSERT statement, but there could be more. you posted a lot of code.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Your testing line is in the wrong place, but congrats on actually having a testing line (that's more than most people have). You need to call mysql_error() after mysql_query() or you won't get any results. An error can't be generated until after you attempt to run the query.

    Also, what ManiacDan said above is probably the issue.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo