SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    forgot password - script - please help my first month in php

    I have recently decided to take the plunge and try my hand at php, Ia m enjoying it and I have so far created a login/logout and register page.

    But I am having trouble with the forgot password script. Please can you help and be kind as it is my first time posting.
    This is the error that i am getting Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
    PHP Code:
    <?php

    if (isset($_POST['submit']))
    {    
        
        
    mysql_connect("host"username", "password") or die("could not connect to the database");
        mysql_select_db("
    db_name") or die("could not select database");

        
        
    $username = $_POST['username'];
        
    $email = $_POST['email'];

        
        
    $sql = mysql_query("SELECT FROM `usersWHERE `username` = '$username' AND `email` = '$email'");
        
    $result = mysql_num_rows($sql);

        
        if (!
    $result)
        {
            die (mysql_error());
        }

        
        if (
    $result == 1)
        {
            
    $rows = mysql_fetch_array($result);
            
    $password = $rows['password'];
            
    $email = $rows['email'];
            
    $subject ="Password request";
            
    $header = "fromomarmuzaffar@live.co.uk";
            
    $body = "Your password is " . $password

            mail (
    $email$subject$header$body); 
            print("
    an email containning the password has been sent to you");
        }else
        {
            echo ("
    No such user exists in the systemPlease try again");
        }

    }

    ?>

    <html>
    <head>
        <title>
            forgot password form
        </title>
    </head>
        <body>
            <form action = "
    <?php $_SERVER['PHP_SELF'];?>" method = "POST">
                <p>
                    <label for = "username">Username:</label>
                    <input type = "text" name = "username" />
                </p>
                <p>
                    <label for = "email">Email:</label>
                    <input type = "text" name = "email" />
                </p>
                <p>
                    <input type = "submit" name="submit" />
                    <input type = "reset" name = "reset" />
                </p>
            </form
        </body>
    </html>

  2. #2
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by omarfeb View Post
    I have recently decided to take the plunge and try my hand at php, Ia m enjoying it and I have so far created a login/logout and register page.

    But I am having trouble with the forgot password script. Please can you help and be kind as it is my first time posting.
    This is the error that i am getting Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
    PHP Code:
    <?php

    if (isset($_POST['submit']))
    {    
        
        
    mysql_connect("host"username", "password") or die("could not connect to the database");
        mysql_select_db("
    db_name") or die("could not select database");

        
        
    $username = $_POST['username'];
        
    $email = $_POST['email'];

        
        
    $sql = mysql_query("SELECT FROM `usersWHERE `username` = '$username' AND `email` = '$email'");
        
    $result = mysql_num_rows($sql);

        
        if (!
    $result)
        {
            die (mysql_error());
        }

        
        if (
    $result == 1)
        {
            
    $rows = mysql_fetch_array($result);
            
    $password = $rows['password'];
            
    $email = $rows['email'];
            
    $subject ="Password request";
            
    $header = "fromomarmuzaffar@live.co.uk";
            
    $body = "Your password is " . $password

            mail (
    $email$subject$header$body); 
            print("
    an email containning the password has been sent to you");
        }else
        {
            echo ("
    No such user exists in the systemPlease try again");
        }

    }

    ?>

    <html>
    <head>
        <title>
            forgot password form
        </title>
    </head>
        <body>
            <form action = "
    <?php $_SERVER['PHP_SELF'];?>" method = "POST">
                <p>
                    <label for = "username">Username:</label>
                    <input type = "text" name = "username" />
                </p>
                <p>
                    <label for = "email">Email:</label>
                    <input type = "text" name = "email" />
                </p>
                <p>
                    <input type = "submit" name="submit" />
                    <input type = "reset" name = "reset" />
                </p>
            </form
        </body>
    </html>
    You have provided inappropriate resources to mysql_fetch_array() , it should be query result you have given the number of mysql query-result.

    correction : $rows = mysql_fetch_array($sql);

  3. #3
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    219
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    // for your development, set to true.
    // When in production, set to false
    define('DEBUGMODE''true');

    ...
    // avoid SQL injection
    $username e($username);
    $email e($email);
    $sql mysql_query("SELECT * FROM `users` WHERE `username` = '$username' AND `email` = '$email'") or die( DEBUGMODE mysql_error() : 'Server error' );
    ...

    function 
    e$s ) {
        
    $s stripslashes($s);
        return 
    mysql_real_escape_string($s);
    }

    ?>

  4. #4
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajeev13 View Post
    You have provided inappropriate resources to mysql_fetch_array() , it should be query result you have given the number of mysql query-result.

    correction : $rows = mysql_fetch_array($sql);

    Thank you so much it works but the only thing is that as i mentioned before the mail does not get sent and the message with print does not get sent

  5. #5
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by omarfeb View Post
    Thank you so much it works but the only thing is that as i mentioned before the mail does not get sent and the message with print does not get sent
    are you testing it on local server ?
    because as i know it doesn't work on local server.

  6. #6
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajeev13 View Post
    are you testing it on local server ?
    because as i know it doesn't work on local server.
    WHooo hoo!!! Thanks for the help rich. It works now

  7. #7
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by omarfeb View Post
    WHooo hoo!!! Thanks for the help rich. It works now
    thanks for such a reply.this will encourage me to help others too if i could do so.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •