0

I saw other questions that seemed like this one but they were actually quite different so I'm posting my own version. The code below, especially the form, was simplified so as to better fit.

This query is being generated dynamically but has duplicates due to the fact that the date and time are being submitted using six individual fields, one for each portion of the human-readable date. Then a custom function puts these into a Unix timestamp for insertion but because it is within a foreach loop, it duplicates the field. How can I remove the duplicates?

UPDATE tablename SET 
`Name`='First Last',
`EMail`='[email protected]',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
 WHERE ID='9'

This is a portion of the function and the conditional starting with if (Contains("Date", $key) is where the six selectors are being processed and where the problem is. To clarify it is all working but I want to get rid of the extra DateUpdate values!

if (isset($_POST['update'])) :
            unset($_POST['update']);
            // Remove unneeded fields specified in $RemoveFields variable
            if (isset($RemoveFields) && !is_array($RemoveFields)) $RemoveFields = array($RemoveFields);
            $filteredarray = array_diff_key($_POST, array_flip($RemoveFields));
            foreach ($filteredarray as $key=>$value ) :
                if ($key === 'ID') continue;
                if ($key === 'VerifyCode') continue;
                // Process any password field
                if (Contains("Pass", $key)) :
                    // Encode password field
                    if ($value !== "") $value=md5($value);
                    // If no changes, save original password
                    if ($value === "") continue;
                endif;
                // Process date and time selectors
                if (Contains("Date", $key) && is_numeric($value)):
                    $removals = array('year','month','day','hour','minute','second');
                    $FieldName = trim(str_replace($removals,"",$key));
                    $key = $FieldName;
                    $value=dateProcess($FieldName,"Unix");
                endif;
                // Prepare array for query
                $Values[] = "`$key`=".isNull($value, $DBName);
            endforeach;
            $sqlUpdate = "UPDATE $TableName SET ".implode(",",$Values)
                                    ." WHERE ID='".intval($PostID)."'";
endif;

And finally, here is a simplified version of the form

<form method="POST" name="SendMessage" action="formname.php">
<fieldset>
<legend>Form Name</legend>
<p><label for="Name">Full Name</label>
<input type="text" name="Name" value="Full Name" size="32" class="Input" id="Name">

<p><label for="EMail">EMail</label>
<input type="text" name="EMail" value="[email protected]" size="32" class="Input" id="EMail"> 

<p><label for="DateUpdated">Date Updated</label>
<select name="monthDateUpdated" id="monthDateUpdated">
<option value=""></option>
<option value="12" SELECTED>December</option>
</select>

<select name="dayDateUpdated" id="dayDateUpdated">
<option value=""></option>
<option value="06" SELECTED>06</option>
</select>

, <select name="yearDateUpdated" id="yearDateUpdated">
<option value=""></option>
<option value="2018" SELECTED>2018</option>
</select>

 at <select name="hourDateUpdated" id="hourDateUpdated">
<option value=""></option>
<option value="01" SELECTED>01</option>
</select>

:<select name="minuteDateUpdated" id="minuteDateUpdated">
<option value=""></option>
<option value="36" SELECTED>36</option>
</select>

:<select name="secondDateUpdated" id="secondDateUpdated">
<option value=""></option>
<option value="59" SELECTED>59</option>
</select>

<input type="hidden" name="ID" value="9"> 
<p><div class="ButtonCenter">
<input name="update" type="submit" value="Save Changes">
</div>

</fieldset>
</form>
4
  • Please, post print_r($filteredarray) before foreach Commented Dec 6, 2018 at 9:19
  • What does dateProcess($FieldName,"Unix") do? If inside that code you get the values of all the datelike fields, of course, it will be repeated six times. Also, $key = $FieldName; it's not advisable to reassign a loop variable inside the loop itself... Commented Dec 6, 2018 at 9:21
  • dateProcess takes the $_POST from six fields from the individual date/time selectors and outputs them as a single Unix timestamp. Commented Dec 6, 2018 at 16:48
  • filteredarray on this form is not doing anything as there is no $RemoveFields value. When there are fields to NOT be processed, it removes them from the array. Commented Dec 6, 2018 at 17:46

3 Answers 3

0

Remove this :

// Process date and time selectors
if (Contains("Date", $key) && is_numeric($value)):
    $removals = array('year','month','day','hour','minute','second');
    $FieldName = trim(str_replace($removals,"",$key));
    $key = $FieldName;
    $value=dateProcess($FieldName,"Unix");
endif;

Replace with this :

if (Contains("Date", $key) && is_numeric($value)):
    $removals = array('year','month','day','hour','minute','second');
    $FieldName = trim(str_replace($removals,"",$key));
    $key = $FieldName;
    $time_val = $filteredarray['yearDateUpdated'].'-'.$filteredarray['monthDateUpdated'].'-'.$filteredarray['dayDateUpdated'].' '.$filteredarray['hourDateUpdated'].':'.$filteredarray['minuteDateUpdated'].':'.$filteredarray['secondDateUpdated'];
    $value=strtotime($time_val);
endif;

and put array_unique($Values);
before the :

$sqlUpdate = "UPDATE $TableName SET ".implode(",",$Values)
                                ." WHERE ID='".intval($PostID)."'";
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, DateUpdated was the field name in the example I posted but it might be something else on other forms so can't be hard-coded into the function. In any event, if I filter out the date submissions prior to any processing, then dateProcess() will have no values with which to work. However, array_unique() seems like a good idea but when I add it, I still get the same results in the generated query.
+1 to you! I was sure that your idea of array_unique() was the answer so I revisited what I had added and realized I had used it but I hadn't assigned it to the array! Adding $Values = array_unique($Values) immediately after the foreach loop's closing did the trick (I had added only array_unique($Values) originally). I did not change the other code as dateProcess() is needed to handle other date field types although I've not yet implemented that ability.
0
$check_point='';
foreach ()
{

    if(!in_array($check_point, $array))
    {

     //condition
     // do your whole updating task here and after this assign it to a variable  for checking next time , so if it's found then unset it .

      $check_point=$array[$va];
     }
     else{

         unset($array[$check_point]);
         $check_point='';
     }
}

1 Comment

Yes, I know, which is why I’m using $Values = array_unique($Values) but I forgot the $Values = the first try. It is all working well now.
0
$my_Unique_Array = array_unique($my_Array);

Comments

Your Answer

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