PHP MySQL一次插入/更新多行

时间:2015-12-15 11:58:24

标签: php mysql arrays

我已阅读此问题:https://stackoverflow.com/questions/20255690/php-insert-a-variable-number-of-records-to-mysql-from-a-html-form#=

但是我无法弄清楚如何将它应用于具有多个输入的表单。 我希望这些输入进入同一行(每个数组ID)。

我的PHP呈现以下HTML:

<div class="panel-body">
    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 employee-container">
            <label for="visible-107">
                <img title="Jane Doe" src="/images/prof-pics/default.jpg" class="img-responsive img-circle" alt="Jane Doe">
            </label>
            <h4><input type="checkbox" name="visible[0]" id="visible-107"> <label for="visible-107">Jane Doe</label></h4>
            <div class="input-group">
                <span class="input-group-addon">Function</span>
                <input type="text" class="form-control" name="function[0]" id="function-107">
            </div>
            <div class="input-group">
                <span class="input-group-addon">Order</span>
                <select class="form-control" name="order[0]" id="order-107">
                    <option value="">-- select one --</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>
            </div>
            Description<br>
            <textarea class="form-control" name="description[0]" id="description-107"></textarea>
        </div>
        <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 employee-container">
            <label for="visible-2"><img title="John Doe" src="/images/prof-pics/default.jpg" class="img-responsive img-circle" alt="John Doe"></label>
            <h4><input type="checkbox" name="visible[1]" id="visible-2"> <label for="visible-2">John Doe</label></h4>
            <div class="input-group">
                <span class="input-group-addon">Function</span>
                <input type="text" class="form-control" name="function[1]" id="function-2">
            </div>
            <div class="input-group">
                <span class="input-group-addon">Order</span>
                <select class="form-control" name="order[1]" id="order-2">
                    <option value="">-- select one --</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>
            </div>
            Description<br>
            <textarea class="form-control" name="description[1]" id="description-2"></textarea>
        </div>
    </div>
</div>

我如何迭代这些数组,以便我可以将它们作为每个数组ID的行插入/更新?

实施Nana Partykar的回答后

function set_team($web_mysqli, $mysqli, $uid, $visible, $function, $order, $description, $action, $update_id = null ) {
    $number_empl = sizeof($function);

    for($i=0; $i<$number_empl; $i++) {
        $uid = $uid[$i];
        $visible = $visible[$i];
        $function = $function[$i];
        $order = $order[$i];
        $description = $description[$i];

        $name = get_full_name($mysqli, $uid, false);

        $sql = "INSERT INTO team (name, function, description, displayorder, visible) VALUES ('$name', '$function', '$description', '$order', '$visible')";
        $web_mysqli->query($sql) or die(mysqli_error($web_mysqli));
    }

    $_SESSION['success'] = "Employee list website updated";
    header("Location: ".BASE_PATH."/includes/views/list-employees.php");
    exit();

if(isset($_POST['submit-btn'])) {
    set_team($web_mysqli, $mysqli, $_POST['uid'], $_POST['visible'], $_POST['function'], $_POST['order'], $_POST['description'], 'insert');
}

当我保存时,只有第一个名字被插入到表格中,然后是3个空白行。

1 个答案:

答案 0 :(得分:2)

不需要name[0]

<input type="checkbox" name="visible[0]" id="visible-107">
<input type="text" class="form-control" name="function[0]" id="function-107">
<select class="form-control" name="order[0]" id="order-107">
<textarea class="form-control" name="description[0]" id="description-107"></textarea>

<input type="checkbox" name="visible[]" id="visible-107">
<input type="text" class="form-control" name="function[]" id="function-107">
<select class="form-control" name="order[]" id="order-107">
<textarea class="form-control" name="description[]" id="description-107"></textarea>

SomePage.php(提交页面)

<?
extract($_POST);
$sizeOfFunc=sizeof($function);

for($i=0;$i<$sizeOfFunc;$i++)
{
    $Visible=$visible[$i];
    $Function=$function[$i];
    $Order=$order[$i];
    $Description=$description[$i];

    echo $Visible." ".$Function." ".$Order." ".$Description;

    //Use $Visible, $Function, $Order, $Description in your query
}

?>

更新代码

  

不要使用相同的变量名称。 Atleast更改变量名称。

这是错误的。

$uid = $uid[$i];
$visible = $visible[$i];
$function = $function[$i];
$order = $order[$i];
$description = $description[$i];

这是正确的

$Uid = $uid[$i];
$Visible = $visible[$i];
$Function = $function[$i];
$Order = $order[$i];
$Description = $description[$i];

我已经改变了你的代码。使用以下代码。它会起作用。

<?
function set_team($web_mysqli, $mysqli, $uid, $visible, $function, $order, $description, $action, $update_id = null ) {
    $number_empl = sizeof($function);

    for($i=0; $i<$number_empl; $i++) {
        $Uid = $uid[$i];
        $Visible = $visible[$i];
        $Function = $function[$i];
        $Order = $order[$i];
        $Description = $description[$i];

        $name = get_full_name($mysqli, $Uid, false);

        $sql = "INSERT INTO team (name, function, description, displayorder, visible) VALUES ('$name', '$Function', '$Description', '$Order', '$Visible')";
        $web_mysqli->query($sql) or die(mysqli_error($web_mysqli));
    }

    $_SESSION['success'] = "Employee list website updated";
    header("Location: ".BASE_PATH."/includes/views/list-employees.php");
    exit();

if(isset($_POST['submit-btn'])) {
    set_team($web_mysqli, $mysqli, $_POST['uid'], $_POST['visible'], $_POST['function'], $_POST['order'], $_POST['description'], 'insert');
}
?>

我在编辑后检查了系统中的代码。它工作正常。

<?

error_reporting(0);
extract($_POST);

echo $number_empl = sizeof($function);

for($i=0; $i<$number_empl; $i++) 
{
    $Visible = $visible[$i];
    $Function = $function[$i];
    $Order = $order[$i];
    $Description = $description[$i];
    $name="Just";

    echo $sql = "INSERT INTO team (name, function, description, displayorder, visible) VALUES ('$name', '$Function', '$Description', '$Order', '$Visible')"."<br>";

}
?>