gpt4 book ai didi

php - 使用循环而不是一条一条地向 MySql 插入数据

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:18 26 4
gpt4 key购买 nike

我的 Html 代码如下:

<form name="creditAssessPage2" id="basicform" method="post" action="post.php" enctype="multipart/form-data">
<tr>
<td><input type="text" name="finanIns1" id="finanIns1" value=""></td>

<td id="balance"><input type="number" name="balance1" id="balance1" value="" min="0"></td>
<td id="MonthyComm"><input type="number" name="monthlyComm1" id="monthlyComm1" value="" min="0"></td>
</tr>
<tr>
<td><input type="text" name="finanIns2" id="finanIns2" value=""></td>

<td id="balance"><input type="number" name="balance2" id="balance2" value="" min="0"></td>
<td id="MonthyComm"><input type="number" name="monthlyComm2" id="monthlyComm2" value="" min="0"></td>
</tr>
<tr>
<td><input type="text" name="finanIns3" id="finanIns3" value=""></td>

<td id="balance"><input type="number" name="balance3" id="balance3" value="" min="0"></td>
<td id="MonthyComm"><input type="number" name="monthlyComm3" id="monthlyComm3" value="" min="0"></td>
</tr>
<tr>

</form>

我的 post.php 代码如下:

    require_once 'db/dbfunction.php';
require_once 'db/dbCreditAssessment.php';

session_start();
$con = open_connection();
$finanIns1 = $_POST['finanIns1'];
$balance1 = $_POST['balance1'];
$monthlyComm1 = $_POST['monthlyComm1'];

$finanIns2 = $_POST['finanIns2'];
$balance2 = $_POST['balance2'];
$monthlyComm2 = $_POST['monthlyComm2'];

$finanIns3 = $_POST['finanIns3'];
$balance3 = $_POST['balance3'];
$monthlyComm3 = $_POST['monthlyComm3'];

addemployementdetails($con,$finanIns1,$balance1,$monthlyComm1);
addemployementdetails($con,$finanIns2,$balance2,$monthlyComm2);
addemployementdetails($con,$finanIns3,$balance3,$monthlyComm3);

close_connection($con);

我的 dbCreditAssessment.php 代码如下:

function addemployementdetails($con,$finanIns1,$balance1,$monthlyComm1){

$query = "insert into employementdetails(finance,balance,monthlycomm)
values('$finanIns1','$balance1','$monthlyComm1')";
//echo "{$sqlString}";



$insertResult = mysqli_query($con, $query);


if($insertResult){
echo " Applicant Detail Added !<br />";
echo "<a href='index.php'>Back to Home</a>";
}
else {
echo " Error !";
echo "{$query}";
//header('Location: post.php');
}
}

这个函数会重复3次。实际上,还有很多相同的字段需要输入。例如finanIns1、finanIns2、finanIns3、finanIns4、finanIns5 等。

有没有办法用循环来代替这种插入数据的方式呢?另外,如果字段为空,则数据不会插入到Mysql

最佳答案

首先,计算 $_POST 数组中 finanIns 的出现次数。然后使用 'for' 函数循环它。

$count = count(preg_grep("/^finanIns[\d]*/", array_keys($_POST)));

for($i = 1; $i <= $count; $i++){
$finanIns = $_POST['finanIns'.$i];
$balance = $_POST['balance'.$i];
$monthlyComm = $_POST['monthlyComm'.$i];

addemployementdetails($con,$finanIns,$balance,$monthlyComm);
}

请记住使用 mysqli_real_escape_string 和 trim 清理这些输入。将您的代码更改为:

$count = count(preg_grep("/^finanIns[\d]*/", array_keys($_POST)));

for($i = 1; $i <= $count; $i++){
$finanIns = trim(mysqli_real_escape_string($_POST['finanIns'.$i]));
$balance = trim(mysqli_real_escape_string($_POST['balance'.$i]));
$monthlyComm = trim(mysqli_real_escape_string($_POST['monthlyComm'.$i]));

addemployementdetails($con,$finanIns,$balance,$monthlyComm);
}

关于php - 使用循环而不是一条一条地向 MySql 插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44930959/

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