0

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] => )

Nathan
  • 11
  • 4
  • Whats `print_r($_POST)` look like – Lawrence Cherone Apr 19 '14 at 01:38
  • *also fyi* when echoing `$thenumdrivers` your opening yourself upto XSS nasties, always remember to use [htmlentities()](http://php.net/manual/en/function.htmlentities.php) when echoing user supplied strings.. even more so in your admin areas... – Lawrence Cherone Apr 19 '14 at 01:45
  • @LozCherone Updated OP, Thanks for the heads up. They were more so just for error checking right now. I had planned on deleting them once completed. – Nathan Apr 19 '14 at 01:46
  • Duplicate of http://stackoverflow.com/a/15990858/285587 – Your Common Sense Apr 19 '14 at 03:44
  • According to your code, if an exception happens then you do not do anything with it. Except hide it. i suggest that you log it at least. Better yet, don't bother catching them while you are developing the code. – Ryan Vincent Apr 19 '14 at 15:14

1 Answers1

0

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.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • I'm sorry that's my error. I looked back in my code and it is 'reg_drivers_num'. I put the wrong one in the code of this post. – Nathan Apr 19 '14 at 12:37