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

    Join Date
    Oct 2012
    Rep Power

    Problem with mysql and form

    i get this error
    ( ! ) 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

    // 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

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

    this is my code for register.php
    PHP Code:

    // require common code


    <!DOCTYPE html>


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


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

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

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


  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Pennsylvania, USA
    Rep Power
    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
    Rep Power
    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.

    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