2012-11-28 80 views
0

我有一個包含4個複選框的表單;首次亮相,擊球,不擊球。當添加,如果被選中,將數據添加與此代碼(在這種情況下,首次亮相):'else if'對於多個複選框組合

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。我在做什麼錯?

回答

0

我注意到這是在一個elseif語句中......也許第一個條件得到滿足,代碼的這部分從未執行?

如果此代碼遵循上面的if語句,那肯定會是這種情況。

[編輯]

您應該檢查代碼的重複,以及,如果唯一的區別在於,設置「擊」和「亮相」爲0或1,有做沒有短得多的方式「如果'聲明。

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

然後就像你對其他人一樣綁定值。請參閱http://davidwalsh.name/php-ternary-examples三元陳述的例子

+0

好的,這是有道理的。如果我交換訂單(所以組合出現在個人之前)是否會起作用? – cameronjonesweb

+0

如果這是你檢查的唯一組合,是的 – soulfreshner

+0

保留訂單,它的工作完美,非常感謝你 – cameronjonesweb