Ingus Graholskis Ingus Graholskis - 1 month ago 8
MySQL Question

php update , updates some values double

i have large table where is night and day work hour.
for some reason some of the values are updated like valuex2 why?

my code:

if ($source=='editEmployeeHours') {

$object=htmlentities($_POST['object'], ENT_QUOTES, "UTF-8");
$period=htmlentities($_POST['period'], ENT_QUOTES, "UTF-8");
$m=htmlentities($_POST['m'], ENT_QUOTES, "UTF-8");
$y=htmlentities($_POST['y'], ENT_QUOTES, "UTF-8");
$date=htmlentities($_POST['date'], ENT_QUOTES, "UTF-8");
$records=htmlentities($_POST['records'], ENT_QUOTES, "UTF-8");
include('inc/s.php');
$g = null;
while ($g <= $records){
$g++;
$edithoursday = $personday = $day = null;
$edithoursday=htmlentities($_POST['edithoursday'][$g], ENT_QUOTES, "UTF-8");
$personday=htmlentities($_POST['personday'][$g], ENT_QUOTES, "UTF-8");
$day=htmlentities($_POST['day'][$g], ENT_QUOTES, "UTF-8");

if($edithoursday!=''){
$resultd = mysqli_query($conn,"SELECT * FROM timesheets WHERE day='".$day."' AND person_code = '".$personday."' AND object_id = '".$object."' AND period_code='".$period."' AND type='0'");
if (!$resultd){die("Attention! Query to show fields failed.");}
if (mysqli_num_rows($resultd)!=0){
$queryied = "UPDATE timesheets SET value='".$edithoursday."' WHERE day='".$day."' AND person_code = '".$personday."' AND object_id = '".$object."' AND period_code='".$period."' AND type='0'";
mysqli_query($conn, $queryied);
}
}
}

header("Location: "home");
die(0);

}


For updating night hours i have same code but
with nights variables
Is there anything wrong that i do not see?

My apologize for long code!

Ima Ima
Answer

Run this code, probably you will find error yourself

if ($source == 'editEmployeeHours')
{

    $object  = htmlentities($_POST['object'], ENT_QUOTES, "UTF-8");
    $period  = htmlentities($_POST['period'], ENT_QUOTES, "UTF-8");
    $m       = htmlentities($_POST['m'], ENT_QUOTES, "UTF-8");
    $y       = htmlentities($_POST['y'], ENT_QUOTES, "UTF-8");
    $date    = htmlentities($_POST['date'], ENT_QUOTES, "UTF-8");
    $records = htmlentities($_POST['records'], ENT_QUOTES, "UTF-8");
    include('inc/s.php');
    $g       = null;
    echo "<pre>";
    while ($g <= $records)
    {
        echo "<br><br>---------------------------------------<br><br>";
        $g++;
        $edithoursday = $personday    = $day          = null;
        $edithoursday = htmlentities($_POST['edithoursday'][$g], ENT_QUOTES, "UTF-8");
        $personday    = htmlentities($_POST['personday'][$g], ENT_QUOTES, "UTF-8");
        $day          = htmlentities($_POST['day'][$g], ENT_QUOTES, "UTF-8");

        echo "<pre>";
        print_r(array('edithoursday' => $edithoursday, 'personday' => $personday, 'day' => $day));
        echo "</pre>";
        if ($edithoursday != '')
        {
            $q = "SELECT * FROM timesheets WHERE
            day='" . $day . "' AND
            person_code = '" . $personday . "' AND
            object_id = '" . $object . "' AND
            period_code='" . $period . "' AND
            type='0'";

            echo "<pre>";
            echo $q;
            echo "</pre>";

            $resultd = mysqli_query($conn, $q);
            if (!$resultd)
            {
                die("Attention! Query to show fields failed.");
            }
            if (mysqli_num_rows($resultd) != 0)
            {
                $queryied = "UPDATE timesheets SET "
                    . "value='" . $edithoursday . "' "
                    . "WHERE "
                    . "day='" . $day . "' AND "
                    . "person_code = '" . $personday . "' AND "
                    . "object_id = '" . $object . "' AND "
                    . "period_code='" . $period . "' AND "
                    . "type='0'";
                echo "<pre>";
                echo $queryied;
                echo "</pre>";
                mysqli_query($conn, $queryied);
            }
        }
    }
    echo "</pre>";
    header("Location: home");
    die(0);
}