我一直在努力解决这个问题一个小时左右,看起来很基本,但我似乎无法深究它。我在这里要做的是用我的mysql数据库中的内容创建一个块5次。
工作正常,但我真正的问题是当我尝试回显错误消息时。如果$ _POST ['champ5v5name [1-5]']处于默认值,我会在FOR检查后立即获得IF。如果是,它应该回显出一条错误消息,说“选择你的冠军!”,但它没有(它只是进入下一页,好像它是成功的)。我尝试了各种不同的方法,但没有一种方法有效,有人可以帮忙吗?
for($i=1;$i<=5;$i++) {
$result = mysql_query("SELECT name,health,damage,armour,aspeed FROM champions");
$htmltext .= '<label>Champion '.$i.'</label><br/>';
$htmltext .= '<select name="champ5v5name'.$i.'">';
$htmltext .= '<option value="select'.$i.'">Select champion:</option>';
}
while($rowschamp = mysql_fetch_array($result,MYSQL_NUM)) {
$htmltext .= '<option value="'.$rowschamp[0].'">'.$rowschamp[0].' (HP: '.$rowschamp[1].' DMG: '.$rowschamp[2].' ARMOUR: '.$rowschamp[3].' ASPEED: '.$rowschamp[4].')</option>';
}
$htmltext .= '</select><br/><br/>';
if($_POST['champ5v5name1'] != 'Select champion:' || $_POST['champ5v5name2'] != 'Select champion:' || $_POST['champ5v5name3'] != 'Select champion:' || $_POST['champ5v5name4'] != 'Select champion:' || $_POST['champ5v5name5'] != 'Select champion:') {
if($_POST['champ5v5name1'] == $_POST['champ5v5name2'] || $_POST['champ5v5name1'] == $_POST['champ5v5name3'] || $_POST['champ5v5name1'] == $_POST['champ5v5name4'] || $_POST['champ5v5name1'] == $_POST['champ5v5name5']) $error = 'A champion is repeated.';
if($_POST['champ5v5name2'] == $_POST['champ5v5name3'] || $_POST['champ5v5name2'] == $_POST['champ5v5name4'] || $_POST['champ5v5name2'] == $_POST['champ5v5name5'] || $_POST['champ5v5name2'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
if($_POST['champ5v5name3'] == $_POST['champ5v5name2'] || $_POST['champ5v5name3'] == $_POST['champ5v5name4'] || $_POST['champ5v5name3'] == $_POST['champ5v5name5'] || $_POST['champ5v5name3'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
if($_POST['champ5v5name4'] == $_POST['champ5v5name2'] || $_POST['champ5v5name4'] == $_POST['champ5v5name3'] || $_POST['champ5v5name4'] == $_POST['champ5v5name5'] || $_POST['champ5v5name4'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
if($_POST['champ5v5name5'] == $_POST['champ5v5name2'] || $_POST['champ5v5name5'] == $_POST['champ5v5name3'] || $_POST['champ5v5name5'] == $_POST['champ5v5name4'] || $_POST['champ5v5name5'] == $_POST['champ5v5name1']) $error = 'A champion is repeated.';
}
else {
$error = 'Choose your champions!';
}
答案 0 :(得分:0)
您正在做的就是为变量分配一个字符串,无论条件是true
还是false
,您都会设置错误。如果出现错误,我认为没有逻辑可以执行任何特定任务。
话虽如此,你真的需要简化一些事情:
try {
$champions = array();
$error = null;
for ($i = 1; $i < 6; $i++){
$champions[] = $_POST['champ5v5name' . $i];
}
$dups = array_count_values($champions);
rsort($dups);
// Check for missed assignments, assuming no champions have the word 'select' in their name
if(in_array('select',$champions)){
$error = 'Choose your champions!';
}
// Check for duplicate champs
if($dups[0] != 1){
$error = 'Champion Repeated!';
}
if($error){
throw new Exception($error);
}
}
catch (Exception $e) {
echo $e->getMessage();
exit(); // Probably don't exit, just show the form again.
}
// Do other stuff, everything is ok if the code gets here...
答案 1 :(得分:-3)
您是否正在检查所有冠军是否为默认值,或任何是否为默认值?
你的条件是:
$_POST['champ5v5name1'] != 'Select champion:' ||
$_POST['champ5v5name2'] != 'Select champion:' ||
$_POST['champ5v5name3'] != 'Select champion:' ||
$_POST['champ5v5name4'] != 'Select champion:' ||
$_POST['champ5v5name5'] != 'Select champion:'
如果您要参加任何活动,那应该是
$_POST['champ5v5name1'] != 'Select champion:' &&
$_POST['champ5v5name2'] != 'Select champion:' &&
$_POST['champ5v5name3'] != 'Select champion:' &&
$_POST['champ5v5name4'] != 'Select champion:' &&
$_POST['champ5v5name5'] != 'Select champion:'
问题是表单提交的值为select$i
,而不是Select champion:
您应该使用PHP的表单数组功能:
for($i = 1; $i <= 5; $i++) {
$result = mysql_query("SELECT name,health,damage,armour,aspeed FROM champions");
$htmltext .= '<label>Champion '.$i.'</label><br/>';
$htmltext .= '<select name="champ5v5name[]">';
#Field names ending in `[]` turn into arrays!
$htmltext .= '<option value="">Select champion:</option>';
while($rowschamp = mysql_fetch_array($result, MYSQL_NUM)) {
$htmltext .= '<option value="'.$rowschamp[0].'">'.$rowschamp[0].' (HP: '.$rowschamp[1].' DMG: '.$rowschamp[2].' ARMOUR: '.$rowschamp[3].' ASPEED: '.$rowschamp[4].')</option>';
}
$htmltext .= '</select><br /><br />';
}
$champions = $_POST['champ5v5name'];
$error = "No champions chosen!";
for($champions as $champion) {
if($champion) {
$error = "";
break;
}
}
if(!$error && count($array) != count(array_unique($champions))) {
$error = "A champion is repeated"
}