查询的变量集未被识别

时间:2016-04-02 03:23:09

标签: php mysql

如果用户已经提交了提交(即每个用户只有1条记录),我需要预先选择一组三个单选按钮。我使用我知道正常工作的查询设置了三个变量,但是单选按钮中的代码没有识别它们。非常感谢任何帮助或指导。这是php代码:

$query  = "SELECT * ";
$query .= "FROM mypicks ";
$query .= "WHERE user_id = {$user_id} ";
$result = mysqli_query($connection, $query);

if ($result->num_rows === 1) {
    $anthem1 = mysqli_query($connection, "SELECT anthem1 FROM mypicks WHERE user_id = '$user_id'");
    $cointoss2 = mysqli_query($connection, "SELECT cointoss2 FROM mypicks WHERE user_id = '$user_id'");
    $firstscore3 = mysqli_query($connection, "SELECT firstscore3 FROM mypicks WHERE user_id = '$user_id'");

    if (isset($_POST['submit'])) {
        if (isset($_POST['anthem1'])) {
             $anthem1 = $_POST['anthem1'];
        }       

        if (isset($_POST['cointoss2'])) {
             $cointoss2 = $_POST['cointoss2'];
        }       

        if (isset($_POST['firstscore3'])) {
             $firstscore3 = $_POST['firstscore3'];
        }   

        $query  = "UPDATE mypicks SET ";
        $query .= "anthem1 = '{$anthem1}', ";
        $query .= "cointoss2 = '{$cointoss2}', ";
        $query .= "firstscore3 = '{$firstscore3}' ";
        $query .= "WHERE user_id = {$user_id} ";
        $result = mysqli_query($connection, $query);
    }
}

这是单选按钮代码:

<form action="Game1_MyPicks.php" method="post">
    Will the National Anthem be over 3 mins and 15 secs? <br />
    Over  <input type="radio" name="anthem1" value="Over" <?php if ($anthem1=="Over") print('checked="checked"') ?>/><br />
    Under <input type="radio" name="anthem1" value="Under" <?php if ($anthem1=="Under") print('checked="checked"') ?>/><br />

    <br />
    <br />
    <br />
    Which team will win the coin toss? <br />
    Ravens <input type="radio" name="cointoss2" value="Ravens" <?php if ($cointoss2=="Ravens") print('checked="checked"') ?>/><br />
    Niners <input type="radio" name="cointoss2" value="Niners" <?php if ($cointoss2=="Niners") print('checked="checked"') ?> /><br />

    <br />
    <br />
    <br />

    Will the first score of the game be a FG? <br />
    Yes <input type="radio" name="firstscore3" value="Yes" <?php if ($firstscore3=="Yes") print('checked="checked"') ?>/><br />
    No <input type="radio" name="firstscore3" value="No" <?php if ($firstscore3=="No") print('checked="checked"') ?> /><br />

    <input class="button-md" type="submit" name="submit" value="Save">                                                                                                                          
</form>

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式改进查询:

 $query  = "SELECT * ";
 $query .= "FROM mypicks ";
 $query .= "WHERE user_id = '$user_id'";
 $result = mysqli_query($connection, $query);

if (mysqli_num_rows($result)==='1') {    
    $picked = mysqli_query($connection, "SELECT anthem1, cointoss2, firstscore3 
                                         FROM mypicks WHERE user_id = '$user_id'");
    $res = mysqli_fetch_assoc($picked);
 }

以您的形式:

Over  <input type="radio" name="anthem1" value="Over" 
      <?php if($res['anthem1']=="Over"){ print('checked="checked"');} ?>/><br />
Under <input type="radio" name="anthem1" value="Under" 
      <?php if($res['anthem1']=="Under"){ print('checked="checked"');} ?>/><br />

您也可以同样格式化其他四个单选按钮。并检查是否有提交的数据:

if (isset($_POST['submit'])) {        
             $anthem1 = isset($_POST['anthem1'])? $_POST['anthem1']:'';
             $cointoss2 = isset($_POST['cointoss2'])? $_POST['cointoss2']:'';
             $firstscore3 = isset($_POST['firstscore3'])? $_POST['firstscore3']:'';
         if(empty($anthem1)){
             $error = 'Anthem1 is empty';
          }elseif(empty($cointoss2)){
             $error = 'cointoss2  is empty';
          }elseif(empty($firstscore3)){
              $error = 'firstscore3 is empty';
          }else{
              $query  = "UPDATE mypicks SET ";
              $query .= "anthem1 = '$anthem1', ";
              $query .= "cointoss2 = '$cointoss2', ";
              $query .= "firstscore3 = '$firstscore3' ";
              $query .= "WHERE user_id = '$user_id'";
              $result = mysqli_query($connection, $query);
              if($result){
                 echo 'Updated successfully';
               }else{
                 echo 'Problem updatating';
               }
          }

        }