Search code examples
phpsql-serverloopsforeachvar-dump

MSSQL and Post Variables


It may have been a bit since I've worked with this and I think I have been looking at this one to long. Just can't see what I am obviously missing.

I am attempting to query records assigned to terminated team members and within that query perform a second query with displays employed team members in a select menu. Allowing records from terminated team members to be assigned to employed team members.

I am working with a form that passing the following values to POST:

            <?php
            //Loop Through all records in Select Query
            foreach ($TMReservesResult as $Record=>$Value){
        ?>  
            <?php //var_dump($Value); ?>
            <td><?php echo $Value['Company Name']; ?></td>
            <td><?php echo $Value['PhoneNumber']; ?></td>
            <td><?php echo $Value['State']; ?></td>

        <?php
            //STRIP TIME FROM DATE STRING
            $str = $Value['CallDate'];
            $res = $Value['Call Back Date'];
        ?>  

            <td><?php echo substr($str, 0, 11); ?></td>
            <td><?php echo substr($res, 0, 11); ?></td>
            <td>
            <center>
                    <div class="form-group">
                            <div class="col-lg-12 col-md-12 col-sm-12">
                                 <select class="form-control" name="UserName">
                                    <?php foreach($FindActiveTMNamesResults AS $TMNames=>$TMs) { ?>
                                        <option value="<?php echo $TMs['UserName']; ?>"><?php echo $TMs['UserName']; ?></option>
                                    <?php } ?> 
                                 </select>
                           </div>
                    </div>
           </center>                         
        </td>
        </tr>

        <input type="hidden" name="ContactID[]" value="<?php echo $Value["ContactID"]; ?>" />

        <?php } ?>

Processing the POST values:

    <?php
        if (isset($_POST["submit"])){
            //update Employee Info
            $ContactID =  Escape_Function($_POST["ContactID"]);
            $UserName = Escape_Function(($_POST["UserName"]));

            foreach($ContactID as $id){

            //Update Query
            $UpdateEmployeeQuery = 'UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = \'' . $UserName . '\' WHERE [ContactID] = \'' . $id . '\'';
                $UpdateEmployeeResult = $DB->query($UpdateEmployeeQuery);

    } 

 }

?>

And the results of

<?php 

var_dump($UpdateEmployeeQuery);

?>

string(113) "UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = 'sbarnes' WHERE [ContactID] = '1362718628'" string(113) "UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = 'sbarnes' WHERE [ContactID] = '1362703257'" string(113) "UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = 'sbarnes' WHERE [ContactID] = '1364930874'" string(113) "UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = 'sbarnes' WHERE [ContactID] = '1364930900'" string(113) "UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = 'sbarnes' WHERE [ContactID] = '1364930976'"

It appears that my ID loop is working correctly, however, the loop (or lack of) is only grabbing the last record terminate team member name.

What am I missing? Thank you in advanced.


Solution

  • Change

    <select class="form-control" name="UserName">

    To

    <select class="form-control" name="UserName[]">

    Page (Where you are processing the POST values)

    <?php
    if (isset($_POST["submit"])) {
      for($i = 0; $i < sizeof($_POST["ContactID"]);$i++){
    
        $id = Escape_Function($_POST["ContactID"][$i]);
        $UserName = Escape_Function($_POST["UserName"][$i]);
    
        $UpdateEmployeeQuery = 'UPDATE [ACR Contact Management].[dbo].[Contacts] SET [LeadGenerator] = \'' . $UserName . '\' WHERE [ContactID] = \'' . $id . '\'';
        $UpdateEmployeeResult = $DB->query($UpdateEmployeeQuery);
      }
    }?>
    

    You are passing ContactId as array but, not passing username as array type. So, it's taking the option selected of last dropdown. Change it as array type and loop to iterate each and every record.