Partage
  • Partager sur Facebook
  • Partager sur Twitter

inserting multiple rows in to a database

insertion using an array and a prepare statement

    11 janvier 2020 à 1:34:54

    hello,

    i'm trying to insert multiple rows at once into the database using a single insert statement and a prepare statement.

    i've tried with the code below but it is returning me an error" Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\wamp64\www\Projet Apache\My website\html\addquestionrequest.php on line 48"

    this is the line 48 in question 

    $stmt->execute($row);

    the complete code is this:

    $questionid = 1;
    
            $records = array(
                array("Questionid"=>$questionid,"Options"=>$_POST['Option_1']),# row 1 to insert
                array("Questionid"=>$questionid,"Options"=>$_POST['Option_2']),# row 2 to insert
                array("Questionid"=>$questionid,"Options"=>$_POST['Option_3']),# row 3 to insert
                array("Questionid"=>$questionid,"Options"=>$_POST['Option_4']));# row 4 to insert
    
                $args=array_fill(0,count($records[0]), '?');
     $req="INSERTINTO answerproposal (Questionid,Options) values (".implode(',',$args).")";
     $stmt=$db->prepare($req);
     foreach ($records as $row ) {
        $stmt->execute($row);
     }
    

    please for help i've being looking foe the solution for almost 2 weeks

    thanks in advance


    -
    Edité par DerlichDonfack 13 janvier 2020 à 3:41:02

    • Partager sur Facebook
    • Partager sur Twitter
      11 janvier 2020 à 15:47:15

      Hello Derlich,

      You have english side to OC (in french).

      Can you please also use </> button and PHP into list to color your code into code tags ?

      more over, you have 4 options but only 1 db request, so you must loop on option to insert them

      thanks by advance

      • Partager sur Facebook
      • Partager sur Twitter
        11 janvier 2020 à 16:08:04

        Bonjour,

        Mauvais forum

        Le sujet est déplacé de la section PHP vers la section Let's talk!

        Please use syntax highlighting with the button

        OpenClassrooms' forums have a highlighting feature for code lisibility.

        You need to use the Codebutton in the buttons bar, choose a language (here PHP), paste your code and save.

        You can edit your first message or post a new one below mine.

        • Partager sur Facebook
        • Partager sur Twitter

        Pas d'aide concernant le code par MP, le forum est là pour ça :)

        inserting multiple rows in to a database

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown