简体   繁体   中英

multiple inserts to database using mysqli

So i am updating my question as the coding has massively changed, thanks

So here is my premier_league.php page that is called upon when the league is selected ...

<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
}
?>

So from that, you can see that the one row is now looped, and depending on how many teams are in the league, that will determine how many rows of fixtures are needed, in the Prem, this is 10.. so the page for the fixtures seems to be sorted, but if there seems to be something the matter, please bring it up and let me know..

Next was well, the original issue, about using MySQLi and adding the mulitple rows to a database. So again with help, the result ended up looking like this ...

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;
}

So now the main issue i guess, is how to fit this within a function, my functions sit on general.func.php and i guess as there are 10 parameters, and there could be up to 12 rows that's up to 120 parameters pass through, so is there a shorter/cleaner way to pass that many parameters through a function? With the variables being $x* [$i] * due to the loop, would i pass them through like that? so the function could read

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

Or would i have to actually pass all the params through the function??

First of all, there is never, ever a good reason to store variables as $*1 ... $*n . Use an array.

Second, here is how to prepare a statement:

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;
    }
}

Try the following:

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

Notice there are semicolons after each INSERT query. You can except the last one.

Alternative:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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