1

let say that, i want to my change password in php code then it will validate by the use of javascript. before it return to my index page or it will popup on index page. how can i do it? any trick that you can suggest? :)

<?php
  include('config2.php');
  error_reporting(E_ERROR | E_PARSE);
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $val = $_GET['val1'];
    session_start();
    $ak = $_SESSION['autokey'];

    $sql = "UPDATE tbl_user SET password = '". md5($val) ."' WHERE autokey = '$ak'";
    mysql_query($sql);

    header("location:index");
?>

thanks in advance :)

7
  • 3
    You're hashing your passwords wrong. Instead of md5 use password_hash(). md5 is NOT suitable for hashing passwords. Commented Mar 1, 2014 at 2:28
  • actually that is not my question :| Commented Mar 1, 2014 at 2:29
  • 2
    Also, you're mixing mysql_* and mysqli_* up. Which one are you actually using? I would hope mysqli since ext/mysql is deprecated. Commented Mar 1, 2014 at 2:29
  • I highly recommend taking note on this. Anyways, please post your JavaScript validation as well. Commented Mar 1, 2014 at 2:30
  • I know it's not your question. That's why I'm putting it here in the comments section instead of the answers section. Commented Mar 1, 2014 at 2:32

1 Answer 1

3

You could change your code block like this..

$sql = "UPDATE tbl_user SET password = '". md5($val) ."' WHERE autokey = '$ak'";
mysql_query($sql);
if(mysql_affected_rows())
{
    echo "<script>alert('Password was successfully changed !');</script>";
    echo "<script>window.location='index.php'</script>";
} else
{
    echo "<script>alert('Password was not changed');</script>";
    echo "<script>window.location='index.php'</script>";
}

As the comment says.. You are mixing up mysql_* and mysqli_*. Change that first.

Sidenote: Switching to PreparedStatements is even more better to ward off SQL Injection attacks !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.