2016-04-02 79 views
1

我有一組三個單選按鈕,我需要預先選擇用戶是否已經提交(即每個用戶只有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

您可以提高您的查詢是這樣的:

$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 /> 

您可以格式化另外四個ra dio按鈕也是如此。並檢查是否有提交的數據:

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'; 
       } 
      } 

     } 
+0

Mawia,當我使用它的代碼,我得到了一個「未定義變量錯誤」爲$水庫當我到達即使用戶創造了一個記錄形式。任何想法爲什麼$ res是未定義的? – user3367183