GrumpyCrouton
GrumpyCrouton

Reputation: 8621

Updating SQL table with PHP code from generated table

Could someone help me figure out why this isn't working?

My update function isn't working on my php code. I have a breaking point that lists all the information at the end of the for loops, which is correct information. However it prints it about 50 times per ID. Also, it doesn't update the SQL table.

How could I fix this?

Snippet:

if ( isset( $_POST[ 'RemarketingRollCall' ] ) )
                            {

echo 'RollCall called ';
foreach($_POST['userid'] as $param1){
  foreach($_POST['name'] as $param2){
    foreach($_POST['job'] as $param3){
      foreach($_POST['monday'] as $param4){
        foreach($_POST['tuesday'] as $param5){
          foreach($_POST['wednesday'] as $param6){
            foreach($_POST['thursday'] as $param7){
              foreach($_POST['friday'] as $param8){

                $idnumber = mysqli_real_escape_string( $con, $param1 );
                $Name = mysqli_real_escape_string( $con, $param2 );
                $Job = mysqli_real_escape_string( $con, $param3 );
                $Monday = mysqli_real_escape_string( $con, $param4 );
                $Tuesday = mysqli_real_escape_string( $con, $param5 );
                $Wednesday = mysqli_real_escape_string( $con, $param6 );
                $Thursday = mysqli_real_escape_string( $con, $param7 );
                $Friday = mysqli_real_escape_string( $con, $param8 );

                echo "(". $idnumber . " " . $Name . " " . $Job . " " . $Monday . " " . $Tuesday . " " . $Wednesday . " " . $Thursday . " " . $Friday . ")";
                $sql = "UPDATE `Employee` SET `Job`='$Job', `Monday`='$Monday', `Tuesday`='$Tuesday', `Wednesday`='$Wednesday', `Thursday`='$Thursday', `Friday`='$Friday' WHERE `ID`=$idnumber";
                mysqli_query($con,$sql);
                if ( !mysqli_query( $con, $sql ) )
                  {
                    die( 'Error: ' . mysqli_error( $con ) );
                  }                 
              }
            }
          }
        }
      }
    }
  }
}
mysqli_close( $con );
?>
//putting a script here

This is the output: http://gyazo.com/3006df320d4857314ea486936f656ba3

Here is my table generation:

while($row = mysqli_fetch_array($remarketingRoll)) {
  echo "<form id='RemarketingRollCall' method='POST'>";
  echo "<tr>";
  echo "<td contenteditable='false' align='center' width='11%'><input style='verticle-align:center;' type='text' name='userId[]' value=". $row['userId'] ." readonly></td>";
  echo "<td contenteditable='false' align='center' width='11%'><input style='verticle-align:center;' type='text' name='name[]' value=". $row['Name'] ." readonly></td>";
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='job[]'>
  <option value='Remarketing'>Remarketing</option>
  <option value='IOS'>IOS</option>
  <option value='Computers'>Computers</option>
  </select></td>";
  if ( strcmp( $row['Monday'], 'Attended' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='monday[]'>
  <option value='Attended''>Attended</option>
  <option value='Absent'>Absent</option>
  </select></td>";
  } else if ( strcmp( $row['Monday'], 'Absent' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='monday[]'>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  } else {
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='monday[]'>
  <option> </option>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  }
  if ( strcmp( $row['Tuesday'], 'Attended' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='tuesday[]'>
  <option value='Attended''>Attended</option>
  <option value='Absent'>Absent</option>
  </select></td>";
  } else if ( strcmp( $row['Tuesday'], 'Absent' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='tuesday[]'>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  } else {
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='tuesday[]'>
  <option> </option>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  }
  if ( strcmp( $row['Wednesday'], 'Attended' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='wednesday[]'>
  <option value='Attended''>Attended</option>
  <option value='Absent'>Absent</option>
  </select></td>";
  } else if ( strcmp( $row['Wednesday'], 'Absent' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='wednesday[]'>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  } else {
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='wednesday[]'>
  <option> </option>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  }
  if ( strcmp( $row['Thursday'], 'Attended' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='thursday[]'>
  <option value='Attended''>Attended</option>
  <option value='Absent'>Absent</option>
  </select></td>";
  } else if ( strcmp( $row['Thursday'], 'Absent' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='thursday[]'>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  } else {
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='thursday[]'>
  <option> </option>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  }
  if ( strcmp( $row['Friday'], 'Attended' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='friday[]'>
  <option value='Attended''>Attended</option>
  <option value='Absent'>Absent</option>
  </select></td>";
  } else if ( strcmp( $row['Friday'], 'Absent' ) == 0 ){
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='friday[]'>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  } else {
  echo "<td contenteditable='false' align='center' width='11%'>
  <select name='friday[]'>
  <option> </option>
  <option value='Absent'>Absent</option>
  <option value='Attended''>Attended</option>
  </select></td>";
  }
  echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
  echo "</tr>";
  echo "</form>"; 
}

Upvotes: 0

Views: 49

Answers (1)

Misunderstood
Misunderstood

Reputation: 5665

 $index = 0;
 foreach ($_POST as $key => $value){
   $idnumber = $_POST['userId'][$index];
   $Name = $_POST['name'][$index];
   $Job = $_POST['job'][$index];
   $Monday = $_POST['monday'][$index];
   $Tuesday = $_POST['tuesday'][$index];
   $Wednesday = $_POST['wednesday'][$index];
   $Thursday = $_POST['thursday'][$index];
   $Friday = $_POST['friday'][$index];
   $index++;
   // put your sql UPDATE here
}

You do not show the closing brace for the while{

  </select></td>";
  }
  echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
  echo "</tr>";
  echo "</form>";

If it is here:

  </select></td>";
  }
  echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
  echo "</tr>";
  echo "</form>";
}  // is it here????????????

If the brace is where I think it is you need to move it to here

  </select></td>";
  }

  echo "<td name='week[]' contenteditable='false' align='center' width='11%'>" . $row['Week'] . "</td>";
  echo "</tr>";
}  // <==========================
  echo "</form>";

And move:

while($row = mysqli_fetch_array($remarketingRoll)) {
  echo "<form id='RemarketingRollCall' method='POST'>";   

To:

echo "<form id='RemarketingRollCall' method='POST'>";
while($row = mysqli_fetch_array($remarketingRoll)) {

Update 2

Replace:

$idnumber = mysqli_real_escape_string( $con, $param1 );
$Name = mysqli_real_escape_string( $con, $param2 );
$Job = mysqli_real_escape_string( $con, $param3 );
$Monday = mysqli_real_escape_string( $con, $param4 );
$Tuesday = mysqli_real_escape_string( $con, $param5 );
$Wednesday = mysqli_real_escape_string( $con, $param6 );
$Thursday = mysqli_real_escape_string( $con, $param7 );
$Friday = mysqli_real_escape_string( $con, $param8 );

With:

$idnumber = $_POST['userId'][0];
$Name = $_POST['name'][0];
$Job = $_POST['job'][0];
$Monday = $_POST['monday'][0];
$Tuesday = $_POST['tuesday'][0];
$Wednesday = $_POST['wednesday'][0];
$Thursday = $_POST['thursday'][0];
$Friday = $_POST['friday'][0];

end of update 2


Update

Add this line of code to dump what is being submitted:

var_export($_POST);

Don't use foreach.

Change all the foreach:

$param2 = $_POST['name'];
$param3 = $_POST['job'];
$param4 = $_POST['monday'];
$param5 = $_POST['tuesday'];
$param6 = $_POST['wednesday'];
$param7 = $_POST['thursday'] ;
$param8 = $_POST['friday'];

To:

$param2 = $_POST['name'];
$param3 = $_POST['job'];
$param4 = $_POST['monday'];
$param5 = $_POST['tuesday'];
$param6 = $_POST['wednesday'];
$param7 = $_POST['thursday'] ;
$param8 = $_POST['friday'];

Upvotes: 1

Related Questions