1

I have a set of three radio buttons that I need to preselect if the user has already made a submission (i.e. only 1 record per user). I set the three variables using queries that I know are working correctly, but the code in the radio button isn't recognizing them. Any help or guidance is greatly appreciated. Here is the php code:

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

And here is the radio button code:

<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 Answer 1

1

You can improve your query this way:

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

In your form:

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

You can format the other four radio buttons likewise. And check if there is submitted data:

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

        } 
Sign up to request clarification or add additional context in comments.

1 Comment

Mawia, when I use that it code, I get an an "undefined variable error" for $res when I get to the form even though the user has a record created. Any thoughts as to why $res is undefined?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.