简体   繁体   中英

PDO insert into inserting two rows

My request PDO Insert into is inserting two rows in my table, How can i solve it please? This is my scripts

 try
{
    $bdd = new PDO('mysql:host=XXX;dbname=XXX;charset=utf8', 'XXX', 'XXX');
    $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


}
catch(Exception $e)
{
    die('Erreur : ' . $e->getMessage());
}
    $today = date("Y-m-d");
    $id = $_POST['id'];
    $min = $_POST['min'];
    $req = $bdd->prepare('INSERT INTO Commentaires(pseudo, commentaire, date_comment, id_video) VALUES(:pseudo, :commentaire, :date_comment, :id_video)');

    $req->execute(array(
        'pseudo'=>$_POST['pseudo'],
        'commentaire'=>$_POST['comment'],
        'date_comment'=> $today,
        'id_video'=>$id));

    $req->execute();
    $req->closeCursor();
    header('Location: read.php?min='.$min.'&id='.$id);

Just remove second execution.

try {
    $bdd = new PDO('mysql:host=XXX;dbname=XXX;charset=utf8', 'XXX', 'XXX');
    $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


} catch(Exception $e) {
    die('Erreur : ' . $e->getMessage());
}

$today = date("Y-m-d");
$id = $_POST['id'];
$min = $_POST['min'];
$req = $bdd->prepare('INSERT INTO Commentaires(pseudo, commentaire, date_comment, id_video) VALUES(:pseudo, :commentaire, :date_comment, :id_video)');

$req->execute([
    'pseudo'=> $_POST['pseudo'],
    'commentaire'=> $_POST['comment'],
    'date_comment'=> $today,
    'id_video'=> $id
]);

$req->closeCursor();

Edit: Removed redirect code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM