0

I'm currently working on a profile page where people can change their password and email address. But everytime i try to save it says Undefined index.

Notice: Undefined index: password in C:\xampp\htdocs\profile.php on line 54

Notice: Undefined index: email in C:\xampp\htdocs\profile.php on line 54

Here is the code: profile.php

<?php  
include 'include/db.php';
include 'include/functions.php'; 
?>  
<!DOCTYPE html>  
<html>  
  <head>  
       <title>Profile</title>  
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
       <link rel="stylesheet" href="include/style.css" /> 
       <script src="include/js.js"></script>
  </head>  
  <body>  
    <br>
    <div class="container">
        <img src="images/banner.jpg" alt="" style="width:100%;">
        <div class="centered"><h3 align="center"></h3> </div>            
        <?php include 'navbar.php';?>
        <div class="container-center">
        <div class="left">
            <div class="header"><h4>Personal info</h4></div>
            <br>
            <div class="register" style="width:500px;">
            <?php
            $username = $_SESSION['username'];
            $sql = "select * from accounts where username = :username";

            $stmt = $pdo->prepare($sql);
            $stmt->setFetchMode(PDO::FETCH_ASSOC); 
            $stmt->execute(array('username'=>$username));

            while ($row = $stmt->fetch()) {
            $username = $row["username"];
            $password = $row["password"];
            $email = $row["email"];

            print "<form method='POST'>";
            print "<tr>";
            print "<td>Username: " . "<input type='readonly' name='' class='form-control' style='background-color: grey;' value='$username' readonly>" . "</td><br>";
            print "<td>Password: " . "<input type='password' name='' class='form-control' value='$password'</input>" . "</td><br>";
            print "<td>Email:    " . "<input type='email' name='' class='form-control' value='$email'</input>" . "</td><br>";
            print "</tr>";
            print "<td></td>";
            print "<td>" . "<input type='submit' class='btn btn-success' value='Save' name='update'></input>" . "</td>";
            print "</tr>";
            print "</form>";
            }
            if (isset($_POST['update'])) {
            //$password = md5($password);
            $stmt = $pdo->prepare("UPDATE accounts set username = ?, 'password' = ?, 'email' = ? WHERE username = ?"); 
            $stmt->execute([$_SESSION['username'], $_POST['password'], $_POST['email'], $_SESSION['username']]);
            }
            ?>
            </div>
            <br>
            <br>
        </div>
        <div class="right">
            <?php include 'status.php';?>
            <?php include 'ranking-list.php';?>
        </div>
    </div>
        <?php include 'footer.php';?>
</div>
<br>
</div>
</body>  
</html>

And

db.php

//SQL PDO
$db = "mysql:host=127.0.0.1;dbname=database;port=3306";
$user = "root";
$pass = "password";
return $pdo = new PDO($db, $user, $pass);

First time posting here, so the format could be a bit messy. I hope to hear from someone soon!

  • 1
    There are no values in `$_POST['password']` or `$_POST['email']` because the `name` attributes on those inputs are empty; they should say `name='password'` and `name='email`' as appropriate. – Nick Mar 21 '20 at 01:58

1 Answers1

0

you are missing name attributes

print "<td>Password: " . "<input type='password' name='password' class='form-control' value='$password'</input>" . "</td><br>";
print "<td>Email:    " . "<input type='email' name='email' class='form-control' value='$email'</input>" . "</td><br>";
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19