dongtan4046 2018-10-29 17:25
浏览 61

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"; ?>
  • 写回答

1条回答 默认 最新

  • duande8497 2018-10-30 01:53
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)