'else if'用于多个复选框组合

时间:2012-11-28 02:38:33

标签: php checkbox

我有一个包含4个复选框的表单;首演,击球,没有和保龄球。添加时,如果选中一个,它将使用此代码添加数据(在本例中,首次亮相):

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

它完美无缺。但是,如果我尝试组合(例如首次亮相和击球)它使用此代码无效:

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

在这种情况下,只有首次亮相才会被插入1.我做错了什么?

1 个答案:

答案 0 :(得分:0)

我注意到这是在一个elseif语句中...也许第一个条件得到满足,这部分代码永远不会被执行?

如果此代码遵循上述if语句,那肯定是这种情况。

[编辑]

你也应该检查代码重复,如果唯一的区别是将'batted'和'debut'设置为0或1,那么在没有'if'语句的情况下执行它的方法要短得多。

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

然后只需像处理其他值一样绑定值。有关三元语句的示例,请参阅http://davidwalsh.name/php-ternary-examples