problem with mysql and form

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

durrrr
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Oct 29, 2012 2:29 pm

problem with mysql and form

Postby durrrr » Mon Oct 29, 2012 2:33 pm

i get this error

Code: Select all

( ! ) 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

Code: Select all

<?

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

Code: Select all

<?

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

MeroD
New php-forum User
New php-forum User
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: problem with mysql and form

Postby MeroD » Mon Oct 29, 2012 10:04 pm

Try: mysql_affected_rows

http://www.php.net/manual/en/function.m ... d-rows.php

Also: Check if query was successful before checking for results.

if (!$result) {
die('Insert failed...' . mysql_error());
}


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron