Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
17.4k views
in Technique[技术] by (71.8m points)

mysql - Undefined variable in PHP - After updating the data

I'm following a tutorial on youtube and I encountered a problem. Can't fix the problem since it been displaying on the textbox "Undefined Variable" after submitting.

<?php
    require('./database.php');



    if (isset($_POST['update'])){
        $updateId = $_POST['updateId'];
        $updateUsername = $_POST['updateUsername'];
        $updatePassword = $_POST['updatePassword'];

        $queryUpdate = "UPDATE accounts SET username = '$updateUsername', password ='$updatePassword' WHERE id = '$updateId'";
        $sqlQuery = mysqli_query($connection, $queryUpdate);

        
        echo '<script>alert("Successfully Updated!")</script>';
        echo '<script>window.location.href ="/crud-tutorial/index.php"</script>';
    }    
    if (isset($_POST['edit'])){
        $editId = $_POST['editId'];
        $editUsername = $_POST['editUsername'];
        $editPassword = $_POST['editPassword'];
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UPDATE USER</title>
    <style>
    html, body{
        margin: 0;
        padding: 0;
    }

    .main{
        height: 100vh;

        display: grid;
        grid-template-rows: auto 1fr;
        justify-items: center;
        row-gap: 20px;

    }

    .main .update-main{
        grid-row: 1/2;
        display: grid;
        grid-auto-rows: auto;
        row-gap: 5px;
    }

    .main .update-main h3{
        text-align: center;
    }


    </style>
</head>
<body>
    <div class="main">
        <form action="/crud-tutorial/update.php" method="post" class="update-main">
            <h3>UPDATE USER</h3>
            <input type="text" name="updateUsername"  placeholder="Enter your username" value="<?php echo $editUsername?>" required>
            <input type="password" name="updatePassword" required placeholder="Enter your password" value="<?php echo $editPassword?>" required>
            <input type="submit" value="update" name="UPDATE">
            <input type="hidden" name="updateId" value="<?php echo $editId ?>">
        </form>


    </div>

    
</body>
</html>

My index enter image description here

Update file enter image description here After updating data enter image description here

The error


Notice: Undefined variable: editUsername in C:xampphtdocscrud-tutorialupdate.php on line 65


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am not sure what tutorial you're following but I found too many issues and it's easier to re-write it. I have added some comments to help you understand.

<?php
require('./database.php');
//Pre-define your variables first so they're available before submitting the form
$updateId = $_GET["id"]; //supposed to get an edit id from somewhere. I'm guessing the edit link is something like update.php?id=1
$updateUsername = "";
$updatePassword = "";
$script = "";

if (isset($_POST['update'])){
    extract($_POST); //converts $_POST to variables
    $queryUpdate = "UPDATE accounts SET username = '$updateUsername', password ='$updatePassword' WHERE id = '$updateId'";
    $sqlQuery = mysqli_query($connection, $queryUpdate);

    //You can't echo scripts before html so save it in a variable and echo it out inside html head tag.
    $script .= '<script>alert("Successfully Updated!")</script>';
    $script .= '<script>window.location.href ="/crud-tutorial/index.php"</script>';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UPDATE USER</title>
    <style>
        html, body{
            margin: 0;
            padding: 0;
        }

        .main{
            height: 100vh;

            display: grid;
            grid-template-rows: auto 1fr;
            justify-items: center;
            row-gap: 20px;

        }

        .main .update-main{
            grid-row: 1/2;
            display: grid;
            grid-auto-rows: auto;
            row-gap: 5px;
        }

        .main .update-main h3{
            text-align: center;
        }


    </style>
    <?= $script ?>
</head>
<body>
<div class="main">
    <form action="" method="post" class="update-main"><!-- You don't need to add action value when submitting to the same page -->
        <h3>UPDATE USER</h3>
        <input type="text" name="updateUsername"  placeholder="Enter your username" value="<?= $updateUsername ?>" required>
        <input type="password" name="updatePassword" required placeholder="Enter your password" value="<?= $updatePassword ?>" required>
        <input type="submit" value="UPDATE" name="update">
        <input type="hidden" name="updateId" value="<?= $updateId ?>">
    </form>


</div>


</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...