0

我有一个带有 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。我做错了什么?

4

1 回答 1

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

于 2012-11-28T02:44:29.733 回答