简体   繁体   中英

Recursive PHP PDO POST INSERT

I have part of my PHP file below. I'm using PDO for communication with a database and I'm receiving the data from a HTML Form. Both try/catches are laid out in the same format, however only the first try/catch will INSERT the information correctly. The second try/catch is not inserting any information, and it's not catching any errors. Echos display just fine, but again no data in NAMES table.

If anyone can point out my mistake I'd greatly appreciate it.

$thenumdrivers = $_POST['reg_drivers_num'];

//Query for INSERT drivers into DRIVERS table
try{
    echo "Number of drivers being registered: $thenumdrivers!<br>";

    $STH=$DBH->prepare("INSERT INTO `Drivers` (`Account_id`, `Driver_license`, `Driver_name`, `Driver_birthdate`)
    VALUES (:reg_accountid,:drivers_license,:drivers_firstname,:drivers_dob)");
    for($i=1;$i<=$thenumdrivers;$i++){
        $STH->execute(array(':reg_accountid'=>$theaccountid,':drivers_license'=>$_POST['drivers_license'.$i],':drivers_firstname'=>$_POST['drivers_firstname'.$i],':drivers_dob'=>$_POST['drivers_dob'.$i]));

        echo "1 Driver added!<br>";
    }
}
catch(PDOException $e){
    $e->getMessage();
}

//Query to INSERT driver names into NAMES table
try{
    echo "Number of drivers being added to Names: $thenumdrivers!<br>";

    $STH=$DBH->prepare("INSERT INTO `Names` (`Account_id`, `Name_first`, `Name_middle`, `Name_last`) VALUES (:reg_accountid,:drivers_firstname,:drivers_middleinit,:drivers_lastname)");
    for($k=1;$k<=$thenumdrivers;$k++){
        $STH->execute(array(':reg_accountid'=>$theaccountid,':drivers_firstname'=>$_POST['drivers_firstname'.$k],':drivers_middleinit'=>$_POST['drivers_middleinit'.$k],':drivers_lastname'=>$_POST['drivers_lastname'.$k]));

        echo "1 Name added!<br>";
    }
}
catch(PDOException $e){
    $e->getMessage();
}

print_r($_POST) output for just this section of data. I only tried to register 1 driver. So the blanks for the others are correct.

Array ( [reg_vehicles_num] => 1 [reg_make1] => make [reg_model1] => model [reg_year1] => 2014 [reg_vin1] => 3 [reg_make2] => [reg_model2] => [reg_year2] => [reg_vin2] => [reg_make3] => [reg_model3] => [reg_year3] => [reg_vin3] => [reg_make4] => [reg_model4] => [reg_year4] => [reg_vin4] => [reg_drivers_num] => 1 [drivers_lastname1] => Tester [drivers_middleinit1] => J [drivers_firstname1] => Fester [drivers_dob1] => 2014-04-01 [drivers_license1] => 0987654321 [drivers_lastname2] => [drivers_middleinit2] => [drivers_firstname2] => [drivers_dob2] => [drivers_license2] => [drivers_lastname3] => [drivers_middleinit3] => [drivers_firstname3] => [drivers_dob3] => [drivers_license3] => [drivers_lastname4] => [drivers_middleinit4] => [drivers_firstname4] => [drivers_dob4] => [drivers_license4] => )

Ok the only thing I can see is that in your sending $_POST['reg_drivers_num'] but tying to access $_POST['reg_number_drivers'] , enable error_reporting(E_ALL) .

You might also want to look into structuring your POST input values better and send an array of values, this way you can use a foreach to loop the values and not need the reg_number_drivers value.

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