PHP / SQL有关使用sql数据库在php中更新/删除的问题

I'm working on a basic database app which uses a sql database to store and retrieve information from as part of the crud operations the creation and reading of data works perfectly fine. However I'm facing issues with updating and deleting the data stored and it never happened before.Is there something I'm doing wrong? I'm assuming the something that I've done wrong in update may be similar to my issue in delete. Here's the code for the update part : [Note this is just for a basic demo and so security features aren't important]

<?php
require "config.php";
require "common.php";
if (isset($_POST['submit'])) {
    try {
        $connection = new PDO($dsn, $username, $password, $options);
        $user =[
                "char_id"    => $_POST['char_id'],
                "char_name"  => $_POST['char_name'],
                "currency"   => $_POST['currency'],
                "server_id"  => $_POST['server_id'],
                "account_id" => $_POST['account_id']
                ];

        $sql = "UPDATE characters 
                SET 
                  char_name = :char_name, 
                  currency = :currency, 
                  server_id = :server_id, 
                  account_id = :account_id
                WHERE char_id = :char_id";

        $statement = $connection->prepare($sql);
        $statement->execute($user);
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}

if (isset($_GET['char_id'])) {
    try {
        $connection = new PDO($dsn, $username, $password, $options);
        $char_id = $_GET['char_id'];
        $sql = "SELECT * FROM characters WHERE char_id = :char_id";
        $statement = $connection->prepare($sql);
        $statement->bindValue(':char_id', $char_id);
        $statement->execute();
        $user = $statement->fetch(PDO::FETCH_ASSOC);
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
} else {
    echo "Something went wrong!"; //this happens
    exit;
}
?>

<?php require "templates/header.php"; ?>

<?php if (isset($_POST['submit']) && $statement) : ?>
    <blockquote><?php echo escape($_POST['char_name']); ?> successfully 
updated.</blockquote>
<?php endif; ?>

<h2>Edit a character</h2>
<form method="post">
    <?php foreach ($user as $key => $value) : ?>
      <label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?> 
   </label>
            <input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo escape($value); ?>"     <?php echo ($key === 'id' ? 'readonly' : null); ?>>
        <?php endforeach; ?> 
        <input type="submit" name="submit" value="Submit">
    </form>
        <a href="index.php">Back to home</a>
    <?php require "templates/footer.php"; ?>

The problem seems to be that your loop of inputs expects an array variable called $user. The $user comes from a DB query, using inputs from your form, but the actual input values comes from the $user variable which isn't set until the DB query is run!

First I would try keeping the $_GET in your code. Your SELECT query expects $_GET['char_id'] to be set in order to execute. Do that by adding ?char_id=SOME NUMBER HERE to your url and go. The number should be a char_id present in your Database.

Now the DB query gets the $_GET['char_id'] that it needs (since the $_GET method fetches parameters from the URL), and you should get some working inputs from your loop, and be able to update entries in your Database.