在通过函数传递数组后将参数绑定到数组

时间:2014-02-27 01:26:14

标签: php mysql sql mysqli

这是我更新的代码,用于显示已完成的操作并帮助人们更轻松地查看我要求的内容 在我目前的项目中,我正在尝试制作一个页面,为英格兰足球/足球的前4个联赛增加装备。在别人的帮助下,我已经到了......

  1. 创建了一个页面,其中包含一行输入和位于表单内的select-option项目,此行只是可能的10或12中的一个,取决于选择哪个联盟。使用一行,这意味着我循环访问数据并生成与特定联盟需要的行数相同的行。

  2. 再次,在其他人的帮助下,准备,绑定和执行 MySQLi 查询的过程似乎已经完成。

  3. 我的问题是如何将它们放在一起。当我验证POSTS灯具的表格时,我是否将一个函数调用到* add_fixtures()*到数据库。如果是这种情况,那么传递参数就成了问题..

    由于每行会有10个参数,然后最多有12行,那120个数据要通过该函数传递,还是有另一种方式?... 这是我的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
    }
    

    然后代码mysqli查询准备,绑定然后执行查询是...

    $sql = "INSERT INTO `fixtures` 
    (`home`, `home-score`, `away-score`, `away`, `kickoff`, `on-tv`, `channel`, `league`, `result`, `date`)
     VALUES
     (?,?,?,?,?,?,?,?,?,?)";
    $stmt = $db->prepare($sql);
    foreach($_POST['result'] as $fixture) {
        $stmt->bind_param("s", $fixture['home']);
        $stmt->bind_param("i", $fixture['hs']);
        $stmt->bind_param("i", $fixture['as']);
        $stmt->bind_param("s", $fixture['away']);
        $stmt->bind_param("s", $fixture['kickoff']);
        $stmt->bind_param("s", $fixture['on-tv']);
        $stmt->bind_param("s", $fixture['channel']);
        $stmt->bind_param("s", $fixture['league']);
        $stmt->bind_param("s", $fixture['result']);
        $stmt->bind_param("s", $fixture['date']);
        $stmt->execute();
    

    所以我认为我的问题现在已经改变了,我猜它更像是......

    如何绑定数组的参数?我应该如何通过函数

    传递数组

    我认为这更符合我目前的需求。

1 个答案:

答案 0 :(得分:1)

总结评论:

<form action="" method="POST">
<input type="submit" name="add" value="Add" class="btn btn-medium btn-success"><br><br>
<?php
/*
1. Prepare elements that require actions, but stay the same througout, before the loop:
*/
$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) {    
/*
2. Always use htmlspecialchars() if you're outputting something that shouldn't be interpreted as HTNL
2.a If the value is exactly the same as the text in an <option>, you may omit the value=""
*/
        $leaguelist = .'<option>'.htmlspecialchars($rows['team']);'</option>';
    }
}
/*
3. Loop though content that stays the same
*/
$needed_rows = 10;
for($i=1; $i <= $needed_rows; $i++){
?>
<!-- ....................................... FIXTURE 1 .............................................. -->
<!--
4. use names with the [] format, so your POST is a nicely formatted array, and you know wich options belong to each other
-->
<select name="result[<?=$i?>][home]" id="" style="width:175px">
<?=$leaguelist?>
</select>

<input type="text" name="result[<?=$i?>][hs]" class="edit_league_input" value="">
vs
<input type="text" name="result[<?=$i?>][ha]" class="edit_league_input" name="" value="">
<select name="result[<?=$i?>][away]" id="" style="175px">
<?=$leaguelist?>
</select>
....
<?php
}

哪一方在接收方:

//prepare content that stays the same _outside_ of the loop:
$sql = "INSERT INTO `fixtures` 
 (`home`, `home-score`, `away-score`, `away`, `kickoff`, `on-tv`, `channel`, `league`, `result`, `date`)
 VALUES
 (?,?,?,?,?,?,?,?,?,?)";
$stmt = $db->prepare($sql);
foreach($_POST['result'] as $fixture) {
        $stmt->bind_param("s", $fixture['home']);
        $stmt->bind_param("i", $fixture['hs']);
        $stmt->bind_param("i", $fixture['as']);
        $stmt->bind_param("s", $fixture['away']);
        //...and so on
        $stmt->execute();
}