php – 如何在数据库中插入多个动态行

前端之家收集整理的这篇文章主要介绍了php – 如何在数据库中插入多个动态行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用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,Weightage,target_date,ID) VALUES ('$formno','".$_POST['Objectives'][$i]."','".$_POST['Measures'][$i]."','".$_POST['Achievement'][$i]."','".$_POST['Weightage_Target'][$i]."','".$_POST['Date_Target'][$i]."','$statement')"; 
    $stmt = sqlsrv_query($conn,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
)

我也尝试使列自动增量,但是不按照与前端输入相同的顺序保存数据.

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

回答你问什么

如果要通过$_POST [‘SNo’]命令插入,请更改此行

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

到了

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

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

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

这是你必须做的

在您的代码中,您在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,...
$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,...) VALUES $query";

在这个例子中,我只是告诉你如何做到这一点.记住,您必须检查$v并通过列类型进行准备.

对您的代码非常重要

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

原文链接:https://www.f2er.com/php/131570.html

猜你在找的PHP相关文章