Home > OS >  how to POST multiple input names (Array) to SQL using $_POST
how to POST multiple input names (Array) to SQL using $_POST

Time:07-18

The following code only Posts the first record to the sql database (Myanmar - Vietnam , Home, 4.30 ) I tried to create an array using $_POST['match'][0]; and renaming the input name to name="match[]" but still just gets the first row

how do i get it to post the whole array with three rows to the database?

here is my code


<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bet Plus 24/7</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="button.css">
    <link rel="stylesheet" href="cart.css">
    <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
    <script type="text/javscript" src="script.js"></script>
    
    
</head>

<body style ="margin:10px;">
 
 
 <?php

include "dbConn.php"; // Using database connection file here


    $match = $_POST['match'][0];
    $selection = $_POST['result'][0];
    $odd = $_POST['value'][0];

    $insert = mysqli_query($db,"INSERT INTO `receipts`(`Match`, `Selection`, `Odd`) 
    
    VALUES ('$match','$selection','$odd')");

    if(!$insert)
    {
        echo mysqli_error($db);
    }
    else
    {
        echo "Records added successfully.";
    }


mysqli_close($db); // Close connection

?>


<form action="" method="post">


      <input type="hidden" name="match[]" value="Myanmar - Vietnam">
      <input type="hidden" name="result[]" value="Home" readonly="">
      <input type="hidden" name="value[]" value="4.30">



      <input type="hidden" name="match[]" value="Thailand - Philippines">
      <input type="hidden" name="result[]" value="Draw" readonly="">
      <input type="hidden" name="value[]" value="3.20">



      <input type="hidden" name="match[]" value="Botswana - Cameroon">
      <input type="hidden" name="result[]" value="Home" readonly="">
      <input type="hidden" name="value[]" value="9.75">


      <input type="submit" name="submit" value="Bet">

</form>

</body>


</html>



CodePudding user response:

Your HTML looks fine. The only problem is in the handling of variables in you PHP code. You are just accessing the 0th element from the array. Hence only one record is getting inserted.

So, rather than selecting the 0th element, you should run a loop to the length of the match[] input. So for every element of the match, you would be running a separate SQL query.

Also you should add isset($_POST['submit']) so that you php code only runs on form submit.

<?php

if(isset($_POST['submit'])){ // runs the following code only if you form gets submitted

    $error = false; //set the error status value
    $error_msg = "";
    $total = count($_POST['match']); // get the length of the match input

    for($i=0;$i<$total;$i  ){ // run the loop till the total length

        //access variables on every iteration

        $match = $_POST['match'][$i]; 
        $selection = $_POST['result'][$i];
        $odd = $_POST['value'][$i];
    
        //run sql query for every iteration

        $insert = mysqli_query($db,"INSERT INTO `receipts`(`Match`, `Selection`, `Odd`) VALUES ('$match','$selection','$odd')");
    
        if(!$insert)
        {
            $error = true;
            $error_msg = $error_msg.mysqli_error($db);            
        }

    }

    //check your error status variable and show your output msg accordingly.
    if($error){
        echo "Error :".$error_msg;
    }else{
        echo "Record inserted successfully!";
    }
}

?>

Hopefully, this would get your desired output and you would be able to store all your data in different rows.

  • Related