简体   繁体   English

如果多个复选框组合为“否则”

[英]'else if' for multiple checkbox combinations

I have a form with 4 checkboxes; 我有一个带有4个复选框的表格; debut, batted, no and bowled. 首次亮相,击球,不打球。 When adding, if one is checked, it will add in the data with this code (in this instance, debut): 添加时,如果选中,则将使用以下代码添加数据(在本例中为首次亮相):

if(!empty($_POST["debut"])) {
try
{
$sql = 'INSERT INTO performance SET
    matchid = :matchid,
    playerid = :playerid,
    team = :team,
    debut = 1,
    batted = 0,
    batpos = :batpos,
    runs = :runs,
    ballsfaced = :ballsfaced,
    fours = :fours,
    sixes = :sixes,
    no = 0,
    howout = :howout,
    fielder = :fielder,
    bowler = :bowler,
    bowled = 0,
    ballsbowled = :ballsbowled,
    maidens = :maidens,
    wickets = :wickets,
    runsconceded = :runsconceded,
    catches = :catches,
    stumpings = :stumpings,
    runouts = :runouts';
$s = $pdo->prepare($sql);
$s->bindValue(':matchid', $_POST['matchid']);
$s->bindValue(':playerid', $_POST['playerid']);
$s->bindValue(':team', $_POST['team']);
$s->bindValue(':batpos', $_POST['batpos']);
$s->bindValue(':runs', $_POST['runs']);
$s->bindValue(':ballsfaced', $_POST['ballsfaced']);
$s->bindValue(':fours', $_POST['fours']);
$s->bindValue(':sixes', $_POST['sixes']);
$s->bindValue(':howout', $_POST['howout']);
$s->bindValue(':fielder', $_POST['fielder']);
$s->bindValue(':bowler', $_POST['bowler']);
$s->bindValue(':ballsbowled', $_POST['ballsbowled']);
$s->bindValue(':maidens', $_POST['maidens']);
$s->bindValue(':wickets', $_POST['wickets']);
$s->bindValue(':runsconceded', $_POST['runsconceded']);
$s->bindValue(':catches', $_POST['catches']);
$s->bindValue(':stumpings', $_POST['stumpings']);
$s->bindValue(':runouts', $_POST['runouts']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted performance.';
include 'error.html.php';
exit();
}
}

And it works perfectly fine. 而且效果很好。 But if I try a combination (eg debut and batted) it doesnt work by using this code: 但是,如果我尝试组合(例如首次亮相和击打),则无法通过使用以下代码来工作:

else if(!empty($_POST["debut"]) && (!empty($_POST["batted"]))) {
  try
{
$sql = 'INSERT INTO performance SET
    matchid = :matchid,
    playerid = :playerid,
    team = :team,
    debut = 1,
    batted = 1,
    batpos = :batpos,
    runs = :runs,
    ballsfaced = :ballsfaced,
    fours = :fours,
    sixes = :sixes,
    no = 0,
    howout = :howout,
    fielder = :fielder,
    bowler = :bowler,
    bowled = 0,
    ballsbowled = :ballsbowled,
    maidens = :maidens,
    wickets = :wickets,
    runsconceded = :runsconceded,
    catches = :catches,
    stumpings = :stumpings,
    runouts = :runouts';
$s = $pdo->prepare($sql);
$s->bindValue(':matchid', $_POST['matchid']);
$s->bindValue(':playerid', $_POST['playerid']);
$s->bindValue(':team', $_POST['team']);
$s->bindValue(':batpos', $_POST['batpos']);
$s->bindValue(':runs', $_POST['runs']);
$s->bindValue(':ballsfaced', $_POST['ballsfaced']);
$s->bindValue(':fours', $_POST['fours']);
$s->bindValue(':sixes', $_POST['sixes']);
$s->bindValue(':howout', $_POST['howout']);
$s->bindValue(':fielder', $_POST['fielder']);
$s->bindValue(':bowler', $_POST['bowler']);
$s->bindValue(':ballsbowled', $_POST['ballsbowled']);
$s->bindValue(':maidens', $_POST['maidens']);
$s->bindValue(':wickets', $_POST['wickets']);
$s->bindValue(':runsconceded', $_POST['runsconceded']);
$s->bindValue(':catches', $_POST['catches']);
$s->bindValue(':stumpings', $_POST['stumpings']);
$s->bindValue(':runouts', $_POST['runouts']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted performance.';
include 'error.html.php';
exit();
}
}

And in this instance only debut will be inserted as 1. What am I doing wrong? 在这种情况下,只会将首次亮相插入为1。我在做什么错?

I notice this is inside an elseif statement... maybe the first condition is met and this part of the code is never executed? 我注意到这在elseif语句中……也许满足了第一个条件,而这部分代码从未执行过?

If this code follows the above if statement, that would definitely be the case. 如果此代码遵循上述if语句,则肯定是这种情况。

[edit] [编辑]

You should check for code duplication as well, if the only difference is setting 'batted' and 'debut' to 0 or 1, there is a much shorter way of doing it without the 'if' statement. 您还应该检查代码是否重复,如果唯一的区别是将“ batt”和“ debut”设置为0或1,则没有'if'语句的方法要短得多。

$debut = (isset($_POST['debut']) ? 1 : 0);
$batted = (isset($_POST['batted']) ? 1 : 0);

Then just bind the values as you did with the others. 然后,只需像绑定其他值一样绑定值即可。 see http://davidwalsh.name/php-ternary-examples for examples of ternary statements 有关三元语句的示例,请参见http://davidwalsh.name/php-ternary-examples

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

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