gpt4 book ai didi

php - 如何将多个动态行插入数据库

转载 作者:可可西里 更新时间:2023-10-31 22:49:53 25 4
gpt4 key购买 nike

我有一个使用 php 和 jQuery 创建的多行动态表。这是 the link to view the table .

一切正常,除了当我将数据插入数据库时​​,序列号没有按顺序保存。我的插入查询如下:

for($i = 0; $i < count($_POST['C_Objectives']); $i++)
{
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,Measures,Targets,subtotal,Corporate_Objective,Row_Number,ID) Values ('$formno','||<==','==','==','".$_POST['SubTotals'][$i]."','".$_POST['C_Objectives'][$i]."','".$_POST['SNo'][$i]."','$statement')";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
die(print_r(sqlsrv_errors(), true));
else
echo " ";
}

for($i = 0; $i < count($_POST['Measures']); $i++)
{
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,Measures,Targets,Weightage,Row_Number,target_date,ID) VALUES ('$formno','".$_POST['Objectives'][$i]."','".$_POST['Measures'][$i]."','".$_POST['Achievement'][$i]."','".$_POST['Weightage_Target'][$i]."','".$_POST['SNo'][$i]."','".$_POST['Date_Target'][$i]."','$statement')";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
die(print_r(sqlsrv_errors(), true));
else
echo " ";
}

序列号保存在 Row_Number 列中,使用 $_POST['SNo'][$i]。是否可以使用 1 个插入查询来保存两个动态行,以便顺序保存序列号?

这是 $_POST 数组结果:

    [Row_Number] => Array
(
[0] => 1
[1] => 2
)

[C_Objectives] => Array
(
[0] => A
[1] => B
)

[Objectives] => Array
(
[0] => a1
[1] => a4
[2] => a7
[3] => b1
)

[Measures] => Array
(
[0] => a2
[1] => a5
[2] => a8
[3] => b2
)

[Achievement] => Array
(
[0] => a3
[1] => a6
[2] => a9
[3] => b3
)

[Date_Target] => Array
(
[0] => 2016-09-09
[1] => 2016-09-09
[2] => 2016-09-09
[3] => 2016-09-09
)

[Weightage_Target] => Array
(
[0] => 25
[1] => 25
[2] => 25
[3] => 25
)

[SNo] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
)

[SubTotals] => Array
(
[0] => 75
[1] => 25
)

[GrandTotal] => 100
)

我也试过使列自动递增,但保存数据的顺序与在前端输入的顺序不同。

enter image description here

enter image description here

最佳答案

您的插入有性能问题。请更改插入数据库的方式。您可以在一个查询中完成所有这些操作。即使第一个“for”有 20 个循环,第二个“for”有 20 个循环。

回答您的问题

如果你想按$_POST['SNo']顺序插入,改变这一行

for($i = 0; $i < count($_POST['C_Objectives']); $i++)

foreach($_POST['SNo'] as $i)

如果您需要一次插入多个,只需执行以下操作:

INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,...)
VALUES (Value1,Value2,...), (Value1,Value2,...)

这是你必须做的

在您的代码中,您在 6 个查询中执行了相同的查询。它甚至可以超过 6 个 $_POST['Measures'] 或 $_POST['C_Objectives'] 数组长度。您需要将它们放在一个查询中,当您不需要设置值时,只需将其设置为列默认值即可。例如 NULL

像这样:

//first we create $values array. it contains all values that you need to insert to the db
$values = array();
$j=0;

for($i = 0; $i < count($_POST['C_Objectives']); $i++){
$values[$j]['Serial_Number'] = $formno;
$values[$j]['Objectives'] = '||<==';
//and fill others here
//fill all cols that you wrote inside your query with the correct order
$j++;
}

for($i = 0; $i < count($_POST['Measures']); $i++){
$values[$j]['Serial_Number'] = $formno;
$values[$j]['Objectives'] = $_POST['Objectives'][$i];
//and fill others here
//fill all cols that you wrote inside your query with the correct order
$j++;
}

//now create (value1,value2,...),(value1,value2,...),...
$query = NULL;
foreach($values as $value){
$tmp = NULL;
foreach($value as $v){
$tmp .= ($v=='')? 'NULL,' : "'$v',";
}
$tmp = rtrim($tmp,',');
$query .= "($tmp),";
}
$query = rtrim($query,',');

//Now Insert
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,...) VALUES $query";

在这个例子中,我只是向你展示了如何去做。请记住,您必须检查 $v 并根据您的列类型准备它。检查是否设置了 $_POST[KEY] 并且它是数组。如果 $query 为空,则不要插入数据库。

对您的代码非常重要

如果这不是您的原始代码,则没有问题,但如果是,请更改您在查询中使用 $_POST 的方式。它的安全性非常低。至少您需要在使用前验证它们。

关于php - 如何将多个动态行插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346539/

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