繁体   English   中英

使用mysqli多次插入数据库

[英]multiple inserts to database using mysqli

所以我正在更新我的问题,因为编码已经发生了很大的变化,谢谢

所以这是我的Premier_league.php页面,当选择联赛时会调用该页面...

<form action="" method="POST">
<input type="submit" name="add" value="Add" class="btn btn-medium btn-success"><br><br>
<?php

$leaguelist = '<option disabled>Please select team</option>';
if ($league_var == NULL) {
    $leaguelist .= '<option "disabled"><strong>Please Select a League Table</strong></h1>';
} else {
    $league_table = get_table($league_var);
foreach ($league_table as $rows) {    

        $leaguelist .= '<option>'.htmlspecialchars($rows['team']).'</option>';
    }
}
$needed_rows = ceil(count(get_table($league_var)) / 2);
for($i=1; $i <= $needed_rows; $i++){
?>
<select name="result[<?=$i?>][home]" id="" style="width:175px">
<?=$leaguelist?>
</select>

<input type="text" name="result[<?=$i?>][home-score]" class="edit_league_input" value="">
vs
<input type="text" name="result[<?=$i?>][away-score]" class="edit_league_input" name="" value="">
<select name="result[<?=$i?>][away]" id="" style="175px">
<?=$leaguelist?>
</select>
<input type="date" name="result[<?=$i?>][date]" style="width:150px;">
<input type="time" name="result[<?=$i?>][kickoff]" style="width:90px;">
<input type="checkbox" name="result[<?=$i?>][on-tv]" value="Yes" style="margin:-10px 5px 0px 5px;">on T.v
<input type="text" name="result[<?=$i?>][channel]" value="" placeholder="Channel..." style="width:100px;">
<select name="result[<?=$i?>][result]" id="" style="width:125px;">
<option value="">Match Choice...</option>
<option value="HT">Half Time</option>
<option value="FT">Full Time</option>
<option value="P">Postponed</option>
</select>
<br>
<?php
}
?>

所以从这一点,你可以看到排正在循环,并取决于有多少支球队是联盟,将决定有多少灯具行需要,可在炳,这是10 ..所以页面固定装置似乎已排序,但如果似乎有问题,请提出来,让我知道。

接下来是关于使用MySQLi并将多行添加到数据库的原始问题。 因此,再次在帮助下,结果最终看起来像这样……

for ($i = 0; $i < count($h); $i++) {
    $sql = "INSERT INTO `fixtures` (`home`, `home-score`, `away-score`, `away`, `kickoff`, `on-tv`, `channel`, `league`, `result`, `date`)
VALUES(?,?,?,?,?,?,?,?,?,?)";

$stmt = $db->prepare($sql);
$stmt->bind_param("s", $h[$i]);
$stmt->bind_param("i", $hs[$i]);
$stmt->bind_param("i", $as[$i]);
$stmt->bind_param("s", $a[$i]);
$stmt->bind_param("s", $time[$i]);
$stmt->bind_param("i", $tv[$i]);
$stmt->bind_param("i", $channel[$i]);
$stmt->bind_param("s", $league[$i]);
$stmt->bind_param("s", $result[$i]);
$stmt->bind_param("s", $date[$i]);
$success = $stmt->execute();

if ($success === false) {
    echo $stmt->errno . ": " . $stmt->error;
}

所以现在我想的主要问题是如何将其适合一个函数,我的函数位于general.func.php上 ,我想有10个参数,最多可以有12行,最多可以传递120个参数,那么有没有更短/更干净的方法来通过一个函数传递那么多参数呢? 由于循环导致变量为$ x * [$ i] *,我会这样传递它们吗? 因此该功能可以读取

add_function($h[$i], $hs[$i], $as[$i], $a[$i], $time[$i], $tv[$i], $channel[$i], $league[$i], $league[$i], $result[$i], $date[$i]) {...}

还是我必须通过函数实际传递所有参数?

首先,永远没有一个很好的理由将变量存储为$*1 ... $*n 使用数组。

其次,这是如何准备语句:

for ($i = 0; $i < count($h); $i++) {
    $sql = "INSERT INTO `fixtures` (`home`, `home-score`, `away-score`, `away`, `kickoff`, `on-tv`, `channel`, `league`, `result`, `date`)
VALUES(?,?,?,?,?,?,?,?,?,?)"

    $stmt = $db->prepare($sql);
    $stmt->bind_param("s", $h[$i]);
    $stmt->bind_param("i", $hs[$i]);
    $stmt->bind_param("i", $as[$i]);
    $stmt->bind_param("s", $a[$i]);
    $stmt->bind_param("s", $time[$i]);
    $stmt->bind_param("i", $tv[$i]);
    $stmt->bind_param("i", $channel[$i]);
    $stmt->bind_param("s", $league[$i]);
    $stmt->bind_param("s", $result[$i]);
    $stmt->bind_param("s", $date[$i]);
    $success = $stmt->execute();

    if ($success === false) {
        echo $stmt->errno . ": " . $stmt->error;
    }
}

请尝试以下操作:

$sql = "INSERT INTO tablename (`home`, ...) VALUES (`$h1`, ...); ";
$sql .= "INSERT INTO tablename (`home`, ...) VALUES (`$h2`, ...); ";

请注意,每个INSERT查询之后都有分号。 您可以除外最后一个。

选择:

$sql = "INSERT INTO Table (`home`, ...) VALUES( `$h1`, ... ), ( `$h2`, ... )";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM