0

I am having trouble updating my database data. I put the data that I want to update but when I click on the "Update" button it does nothing. I have called the file on another php file by using <a href="updateinfo.php?edit=<?php echo $row['id']; ?>"> Update </a>

It also shows this error

Notice: Undefined variable: fname in C:\xampp\htdocs\project\change1.php on line 71

Can someone help me figure this issue, please?

 <?php include("config.php"); ?>
    <?php


        if (isset($_GET['edit'])) {

            $update = true;
            $record = mysqli_query($con, "SELECT * FROM employee WHERE id='".$_GET['edit']."'");
            $row = mysqli_fetch_array($record,MYSQLI_BOTH);
        }

        if (isset($_POST['update'])) {

        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $password = $_POST['password'];
        $addr = $_POST['addr'];
        $phone = $_POST['phone'];

        $id=$_GET['edit'];

        $query = "UPDATE employee SET fname='".$fname."',lname='".$lname."',password='".$password."',addr='".$addr."',phone='".$phone."' WHERE id='".$id."'";
        $result = mysqli_query($con,$query) or die ("problem inserting new record into database");
        if($result){
        header('location: show_db.php');
        }
        else {echo "Update not successful"; }
        }



    ?>
    <!DOCTYPE html>
    <html>
    <head>    
        <title>Update Data</title>
    </head>

    <body>


        <a href="show_db.php">Home</a>
        <br/><br/>

        <input type="hidden" name="id" value="<?php echo $id; ?>">

        Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
        Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
        Password:<input type="text" name="password" value="<?php echo $password; ?>">
        Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
        Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
        <input type="submit" name="update" value="Update">
    </body>
    </html>
3
  • 2
    You need to enclose your inputs with form tags. You'll wanna set the method to POST. Read the intro into forms from w3 Commented May 3, 2018 at 12:33
  • 1
    Misses the <form> tag that encloses your input collection Commented May 3, 2018 at 12:34
  • Thank you for the suggestion Commented May 3, 2018 at 13:10

1 Answer 1

1

Put the html inputs inside a form

  <form name ="form1" method ="get" action="">
    <input type="hidden" name="id" value="<?php echo $id; ?>">

            Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
            Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
            Password:<input type="text" name="password" value="<?php echo $password; ?>">
            Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
            Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
            <input type="submit" name="update" value="Update">
    </form>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot for the help

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.