Php 通过函数传递数组后,将参数绑定到数组
这是我的更新代码,用于显示已完成的工作,并帮助人们更容易地看到我的要求 在我当前的项目中,我正在尝试制作一个页面,为英格兰足球/足球的前四名联赛添加赛程。在别人的帮助下,我已达到Php 通过函数传递数组后,将参数绑定到数组,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,这是我的更新代码,用于显示已完成的工作,并帮助人们更容易地看到我的要求 在我当前的项目中,我正在尝试制作一个页面,为英格兰足球/足球的前四名联赛添加赛程。在别人的帮助下,我已达到 创建了一个包含一行输入的页面,并选择表单中的选项项,根据选择的联赛,该行只是10或12个选项中的一个。对于一行,这意味着我循环了数据,它生成了特定联赛所需的任意多行 同样,在其他人的帮助下,准备、绑定和执行MySQLi查询的过程似乎已经完成 我的问题是如何把所有的东西放在一起。当我验证发布装置的表单时,我是否调用一个函
<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
}
所以我想我的问题现在变了,我想它更像是
如何绑定数组的参数?我应该如何通过函数传递数组
我想这更符合我目前的需要。从评论中总结:
<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
}
你为什么不在一个循环中创建你的“装置”?是的。如果它们是一个表单,请动态地命名:
for($i=1;$i也可以,我看到您多次使用
重新创建
元素。虽然您可能希望对元素执行不同的操作,但请选择元素(名称等),选项保持不变:在循环之前将其存储为字符串并没有什么错,只需在正在创建的select中预先生成HTML即可。echo
。是的,正是这样(如果只有一行,请考虑您将给这些表单元素的名称,并将result[]
粘贴在其前面)。这也有助于您在处理过程中,在循环之前准备stament,并执行以下操作:$stmt=$db->prepare($sql);foreach($_POST['result']as$fixture){$stmt->bind_param(“s”),$fixture['home']];$stmt->bind_param(“s”,$fixture['away']);..$stmt execute()}
。关于将准备移出循环:您希望在循环中尽可能少地执行操作,通常是:在循环开始之前,您可以执行始终保持不变的操作。请参见下面的答案。然后您可以确定正确的行数…我将其作为文本变量添加到那里,但可能,$needed\u rows=ceil(count(get\u table($leaguelist_var))/2);
那么我在同一页上执行函数/query prep bind有意义吗?还是在general.func.php上有一个函数?出于某种原因$leaguelist='''.htmlspecialchars($rows['team'])“;”只是单独提到西汉姆联队,你选择了禁区,这是唯一的一个禁区,我觉得我错过了一些非常明显的东西,哈哈,但我看到的是,所有这些看起来都应该起作用!。2.a-我一直被告知/指示该值=“”当它可以的时候应该有一些东西,即使它与实际文本相同,所以很高兴知道!哈哈……我想我最后一次对它的评论只带来了一个团队,我想这可能与我的函数get\u tables($league\u var)有关…我将ASC改为DESC,阿森纳出现了..但这并不意味着这与该功能有关,它只是意味着在某个地方,其他球队没有出现,只有排名最靠前/最靠后的球队出现了,但那会是什么?我已经解决了球队没有出现的问题,重点是..$leaguelist=。'htmlspecialchars($rows['team'])。”;句号在=符号的错误一侧,因此移动到…$leaguelist.=''.htmlspecialchars($rows['team']))’;而且一切都很好再次让你厌倦了所有这些问题,你甚至不知道从哪里开始,所以就像我3点32分生病一样,probs走开,试着做我的项目,直到明天晚上我想如果我时不时地问你,试着解决问题,如果我有问题,你也许可以在问题出现时回答n从列表开始被禁用的,不会显示为禁用的…如果我去掉“disabled”,那么它是可选的,但是如果该行被提交,它会说“Please pick a team”…我的问题是,如果该行为空,我如何避免将其输入数据库?对于空选项:您可以给它一个空值=“”
确实如此。但是:始终检查服务器端的行是否是您要插入的行(验证您的输入)。关于进一步的问题:尽量使它们紧凑且可复制,最多20行左右的代码,并在此处作为不同的问题提问(当然,在搜索现有答案之后).你在这里提出的问题有点……罗嗦,宽泛,对于通常的SO格式来说不具体;)
<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();
}