PHP Array inserting too many records in the database

后端 未结 2 1465
情深已故
情深已故 2021-01-29 10:18

If i enter only 1 record. It saves only 1 record in the database which is fine. But if i put two records of the same fields. It saves multiple records in the database which shou

2条回答
  •  春和景丽
    2021-01-29 10:46

    You can loop over only one field and use index for others to get appropriate data:

    foreach ($_POST["Description"] as $index => $val )
    {
        $Description = $_POST['Description'][$index];
        $Unit        = $_POST['Unit'][$index];
        $Quantity    = $_POST['Quantity'][$index];
        $Cost        = $_POST['Cost'][$index];
    
        $array = array($Description, $Unit, $Quantity, $Cost);
    
        $query = "
            INSERT INTO MRF_Request (Qty, Unit, Description, Cost) 
            VALUES ('$Quantity', '$Unit', '$Description', '$Cost')
        ";
    
        odbc_exec($conn, $query);
    }
    

    You should also think about sanitizing your $_POST data, to make the system secure and reliable.

提交回复
热议问题