gpt4 book ai didi

php - 递归 PHP PDO POST 插入

转载 作者:行者123 更新时间:2023-11-30 00:21:54 25 4
gpt4 key购买 nike

下面是我的 PHP 文件的一部分。我使用 PDO 与数据库通信,并从 HTML 表单接收数据。两个 try/catch 都以相同的格式布局,但是只有第一个 try/catch 才能正确插入信息。第二个 try/catch 没有插入任何信息,也没有捕获任何错误。回显显示得很好,但 NAMES 表中再次没有数据。

如果有人能指出我的错误,我将不胜感激。

$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) 仅输出这部分数据。我只尝试注册 1 个驱动程序。所以其他的空格都是正确的。

Array ( [reg_vehicles_num] => 1 [reg_make1] => make [reg_model1] => 型号 [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] => 测试员 [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] => )

最佳答案

好吧,我唯一能看到的是,在您发送 $_POST['reg_drivers_num'] 但绑定(bind)访问 $_POST['reg_number_drivers'] 时,启用 错误报告(E_ALL)

您可能还想研究更好地构建 POST 输入值并发送值数组,这样您就可以使用 foreach 循环这些值,而不需要 reg_number_drivers 值。

关于php - 递归 PHP PDO POST 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com