Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How should I update my php radio input to my sql table? radio options are not up

ID: 3827834 • Letter: H

Question

How should I update my php radio input to my sql table? radio options are not updating when changed with the code I have written bellow. Everything else updates correctly but "Gender" and "Demographic".

Table:

PHP PAGE 1

<?php   
       $link = mysqli_connect("localhost", "root", "root", "test") or die ('cannot connect');

       $query = "SELECT * FROM employee";
       $result = mysqli_query($link, $query) or die ("Query failed" .mysqli_connect_error());


       echo"<form action=./employee_anex.php method=POST>
               <table>";

   $count = 0;
   while($info = mysqli_fetch_assoc($result))
   {
       $count++;

  
       //from Employee table
       echo"<tr>
           <td>Employee ID:</td> <td><input type=hidden name=EmployeeID$count value='$info[EmployeeID]' />$info[EmployeeID]</td></tr>";
       echo"<tr>
           <td>First Name:</td> <td><input type=text name=FirstName$count value='$info[FirstName]' /></td></tr>";
       echo"<tr>
           <td>Last Name:</td> <td><input type=text name=LastName$count value='$info[LastName]' /></td></tr>";
       echo"<tr>
       <td>Birthday:</td><td> <input type=date name=DateOfBirth$count value='$info[DateOfBirth]' /></td></tr>";
       echo"<tr>
       <td>Gender: </td>
       <td colspam='2'>
           Male <input type=radio name=Gender$count value='$info[male]' />
           Female <input type=radio name=Gender$count value='$info[female]' /></td></tr>";
       echo"<tr>
           <td>Demographic: </td>
           <td colspam='5'>
           Caucasian <input type=radio name=Demographic$count value='$info[caucasian]' />
           African American <input type=radio name=Demographic$count value='$info[AfricanAmerican]' />
           Asian <input type=radio name=Demographic$count value='$info[asian]' />
           Indian <input type=radio name=Demographic$count value='$info[indian]' />
           Native American <input type=radio name=Demographic$count value='$info[NativeAmerican]' />
           </td></tr>";
       echo"<tr>
           <td>Date Joined Company: <input type=date name=DateJoined$count value='$info[DateJoined]' /></td></tr>";
       echo"<tr>
           <td>Anual Annual Salary: <input type=text name=AnnualSalary$count value='$info[AnnualSalary]' /></td></tr>";


/*       echo"<tr>
           <td>Department ID: <input type=hidden name=DepartmentID$count value='$info[DepartmentID]' /></td></tr>"; */

   }
   echo"<tr>
       <td colspam='2'><input type=submit name=submit value=Submit>&nbsp;&nbsp;
       <input type=reset name=reset value=Reset></td></tr>";
       echo "</table></form>";

       mysqli_close($link);

?>

PHP PAGE 2

<?php
  
  
   $one = array();
   $two = array();
   $three = array();
   $four = array();
   $five = array();
   $six = array();
   $seven = array();
   $idCheck = array();


   foreach($_POST as $key => $value)
   {
       if(preg_match('/EmployeeID/', $key))
       {
           $idCheck[] = $value;
       }
  
   if(preg_match('/FirstName/', $key))
       {
           $one[] = $value;
       }
       if(preg_match('/LastName/', $key))
       {
           $two[] = $value;
       }

       if(preg_match('/DateOfBirth/', $key))
       {
           $three[] = $value;
       }

       if(preg_match('/Gender/', $key))
       {
           $four[] = $value;
       }
       if(preg_match('/Demographic/', $key))
       {
           $five[] = $value;
       }
       if(preg_match('/DateJoined/', $key))
       {
           $six[] = $value;
       }
       if(preg_match('/AnnualSalary/', $key))
       {
           $seven[] = $value;
       }
  
   }

      

   $num = count($idCheck);

   $link = mysqli_connect("localhost", "root", "root", "test") or die('cannot connect');
//mysql_select_db("test") or die('cannot select DB');

   for($i = 0; $i < $num; $i++)
   {
       $query = "UPDATE Employee SET FirstName='$one[$i]', LastName='$two[$i]', DateOfBirth='$three[$i]', Gender='$four[$i]', Demographic='$five[$i]', DateJoined='$six[$i]', AnnualSalary='$seven[$i]' WHERE EmployeeID='$idCheck[$i]'";
      
   $result = mysqli_query($link, $query) or die("Query failed" .mysqli_connect_error());
   }


  
   mysqli_close($link);
   header("Location: ./update_department.php");
  

?>

Explanation / Answer

For radio button need to add 'checked' attribute instead of adding 'value' attribute,
For Gender,
$m_check = $g_check = "";
if(strtolower($info[Gender]) == 'male'){
    $m_check = "checked";
}else
    $g_check = "checked";
  
echo"<tr>
            <td>Gender: </td>
            <td colspam='2'>
            Male <input type=radio name=Gender$count value='$info[male]' $m_check/>
            Female <input type=radio name=Gender$count value='$info[female]' $g_check/></td></tr>";

For Demographic,
$ca_check = $af_check = $as_check = $in_check = $na_check = "";
if(strtolower($info[Demographic]) == 'caucasian') $ca_check = "checked";
if(strtolower($info[Demographic]) == 'africanAmerican') $af_check = "checked";
if(strtolower($info[Demographic]) == 'asian') $as_check = "checked";
if(strtolower($info[Demographic]) == 'indian') $in_check = "checked";
if(strtolower($info[Demographic]) == 'nativeAmerican') $na_check = "checked";

echo"<tr>
            <td>Demographic: </td>
            <td colspam='5'>
            Caucasian <input type=radio name=Demographic$count value='$info[caucasian]' $ca_check/>
            African American <input type=radio name=Demographic$count value='$info[AfricanAmerican]' $af_check/>
            Asian <input type=radio name=Demographic$count value='$info[asian]' $as_check/>
            Indian <input type=radio name=Demographic$count value='$info[indian]' $in_check/>
            Native American <input type=radio name=Demographic$count value='$info[NativeAmerican]' $na_check/>
            </td></tr>";

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote