0

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

1 Answer 1

1
 $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'];
Sign up to request clarification or add additional context in comments.

11 Comments

Okay. This fixed the issue with it going through dozens of times. But I still can't get the freakin' thing to update my database.
Okay. I got it to actually use the UPDATE thing, but it cleared everything.
But what about when I add more to the database? This is meant to iterate through the entire database. - - basically, what my goal is, I have a table generating from a database that has dropdown boxes so users can edit it. The update function is suppose to update every field.
I have no idea what you are talking about. All I see here is a POST being made and the posted data being used to update a single db record. Is the data being updated now?
Just updated my main post with my table generation so you can see where the data comes from. The page generates a table full of information from a table, and inserts everything into an array. The table it generates has dropdown boxes with possible choices for the user to select, and thats what the update code is for. The user should be able to go through the entire table for each ID that is posted, and select options under the days. The update function should update all of these at once, and not just one of them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.