0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

phpでページを再読み込みでデータ登録されない方法

Posted at

リロードした際の再処理を防ぐ方法

php
//登録ボタンが押されたら入る処理
if(isset($_POST["submitButton"])){
    //insertクエリでDBに内容を登録する処理
    $stmt = $db->prepare('INSERT INTO `table` (`username`, `comment`, `postDate`) 
            VALUES (:username, :comment, :postDate)');
    
    header('Location: ' . $_SERVER['PHP_SELF']);//これを足す
}

このような処理があったとしてリロードするとなぜかsubmitButtonの値が見た目上何も入っていない状態でも内部的に値が残っているようで再びinsert文が走ってしまいます
解決策として
header('Location: ' . $_SERVER['PHP_SELF']);
を上部の位置に足すと再処理されないようになるようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?