0

Hi

I am trying to create a update function. But it not updating in the database. Please help me to fix.

Test-2.php

$whereString = '';
    $bind = NULL;

    if(isset($_POST['Submit']))
        {
            if($_POST)
            {
                $SCYQ64UKID = $_GET['SCYQ64UKID'];
                $SCYQ64AVGP = $_POST['SCYQ64AVGP']*100;

                $searchFields = array('SCYQ64EMAL'=>$_POST['SCYQ64EMAL'],'SCYQ64EV01'=>$_POST['SCYQ64EV01'],'SCYQ64EV02'=>$_POST['SCYQ64EV02'],'SCYQ64EV03'=>$_POST['SCYQ64EV03'],'SCYQ64EV04'=>$_POST['SCYQ64EV04'],'SCYQ64EV05'=>$_POST['SCYQ64EV05'],'SCYQ64EV06'=>$_POST['SCYQ64EV06'],'SCUSER'=>$_POST['SCUSER'],'SCYQ64AVGP'=>$SCYQ64AVGP,'SCYQ64TZ'=>$_POST['SCYQ64TZ'],'SCJOBN'=>gethostname(),'SCPID'=>'SSWEB','SCUPMJ'=>$_POST['SCUPMJ'],'SCUPMT'=>$_POST['SCUPMT']);
                if($post = array_filter($_POST))
                    {
                        if($fields = array_intersect_key($searchFields, $post))
                            {
                                $where = array();
                                $bind = array();
                                foreach($fields as $key=>$field)
                                    {
                                        $where[] = "$key LIKE '%$field%'";
                                         $bind[":$key"] = $_POST[$key]; 

                                         $gwhere[] = "$key=$field";
                                         $gbind[":$key"] = $_POST[$key]; 
                                    }
                                 //$whereString = ' WHERE ' . implode(' AND ', $where);
                                 $gwhereString = implode(',', $gwhere);
                            }
                            $Users = new Users($db);
                            $query = $Users->smipleUpdate('FQ64000',$gwhereString,'SCYQ64UKID='.$SCYQ64UKID);
                    }

            }
        }

users.php

/*Update Query with array and id*/
        public function smipleUpdate($tablen,$gwhereString,$whereCondition)
        {
            $query = $this->conn->prepare("Update $this->db_name.$tablen SET $gwhereString where $whereCondition");
            try {
                    $query->execute();

                    return $query;

            }catch(PDOException $e) {
                    die($e->getMessage());
                }
        }
3
Contributors
12
Replies
41
Views
2 Years
Discussion Span
Last Post by rpv_sen
0

I just copied it from search. i have removed $where and $whereString from the code.

0

here is my update code. Please help me how to fix.

if(isset($_POST['Submit']))
        {
            if($_POST)
            {
                $SCYQ64UKID = $_GET['SCYQ64UKID'];
                $SCYQ64AVGP = $_POST['SCYQ64AVGP']*100;

                $searchFields = array('SCYQ64EMAL'=>$_POST['SCYQ64EMAL'],'SCYQ64EV01'=>$_POST['SCYQ64EV01'],'SCYQ64EV02'=>$_POST['SCYQ64EV02'],'SCYQ64EV03'=>$_POST['SCYQ64EV03'],'SCYQ64EV04'=>$_POST['SCYQ64EV04'],'SCYQ64EV05'=>$_POST['SCYQ64EV05'],'SCYQ64EV06'=>$_POST['SCYQ64EV06'],'SCUSER'=>$_POST['SCUSER'],'SCYQ64AVGP'=>$SCYQ64AVGP,'SCYQ64TZ'=>$_POST['SCYQ64TZ'],'SCJOBN'=>gethostname(),'SCPID'=>'SSWEB','SCUPMJ'=>$_POST['SCUPMJ'],'SCUPMT'=>$_POST['SCUPMT']);
                if($post = array_filter($_POST))
                    {
                        if($fields = array_intersect_key($searchFields, $post))
                            {

                                foreach($fields as $key=>$field)
                                    {
                                        $setfields[] = "$key=$field";
                                        $gbind[":$key"] = $_POST[$key]; 
                                    }

                                 echo $gsetString = implode(',', $setfields);
                                $Users = new Users($db);
                                $query = $Users->smipleUpdate('FQ64000',$gsetString,'SCYQ64UKID='.$SCYQ64UKID);

                            }

                    }

            }
        }
0

You're using some sort of wrapper here that we have no idea what's going on with it. Here's a quick way - I've dispensed with the array filters and stuff for clarity:

if(isset($_POST['Submit']) && isset($_GET['SCYQ64UKID'])) {

    $postFields = [
        'SCYQ64EMAL',
        'SCYQ64EV01',
        'SCYQ64EV02',
        'SCYQ64EV03',
        'SCYQ64EV04',
        'SCYQ64EV05',
        'SCYQ64EV06',
        'SCUSER',
        'SCYQ64TZ',
        'SCUPMJ',
        'SCUPMT'
    ];

    //assuming $_POST['SCYQ64AVGP'] is a FLOAT. If INT change to FILTER_VALIDATE_INT
    $additionalFields = [
        'SCYQ64AVGP' => (isset($_POST['SCYQ64AVGP']) && filter_var(FILTER_VALIDATE_FLOAT, $_POST['SCYQ64AVGP'])) ? $_POST['SCYQ64AVGP'] * 100 : 0,
        'SCJOBN' => gethostname(),
        'SCPID' => 'SSWEB'
    ];

    $setBindArray = [];
    $fieldArray = [];

    foreach ($postFields as $f) {
        if (isset($_POST[$f])) {
            $setBindArray[':' . $f] = $_POST[$f];
            $fieldArray[] = "`$f` = :$f";
        }
    }
    foreach ($additionalFields as $f => $v) {
        $setBindArray[':' . $f] = $v;
        $fieldArray[] = "`$f` = :$f";
    }

    $setBindArray[':SCYQ64UKID'] = $_GET['SCYQ64UKID'];

    $fieldString = implode(',', $fieldArray);

    //Using PDO
    $stmt = $db->prepare("UPDATE `FQ64000` SET $fieldString WHERE `SCYQ64UKID` = :SCYQ64UKID");
    $stmt->execute($setBindArray);

}

Edited by diafol

0

i just print $fieldstring it shows as below.

SCYQ64EMAL = :SCYQ64EMAL,SCYQ64EV01 = :SCYQ64EV01,SCYQ64EV02 = :SCYQ64EV02,SCYQ64EV03 = :SCYQ64EV03,SCYQ64EV04 = :SCYQ64EV04,SCYQ64EV05 = :SCYQ64EV05,SCYQ64EV06 = :SCYQ64EV06,SCUSER = :SCUSER,SCYQ64TZ = :SCYQ64TZ,SCUPMJ = :SCUPMJ,SCUPMT = :SCUPMT,SCYQ64AVGP = :SCYQ64AVGP,SCJOBN = :SCJOBN,SCPID = :SCPID

0

Here is my updated Code. which works fine.

if(isset($_POST['Submit']))
        {
            if($_POST)
            {
                $SCYQ64UKID = $_GET['SCYQ64UKID'];
                $SCYQ64AVGP = $_POST['SCYQ64AVGP']*100;

                $searchFields = array('SCYQ64EMAL'=>$_POST['SCYQ64EMAL'],'SCYQ64EV01'=>$_POST['SCYQ64EV01'],'SCYQ64EV02'=>$_POST['SCYQ64EV02'],'SCYQ64EV03'=>$_POST['SCYQ64EV03'],'SCYQ64EV04'=>$_POST['SCYQ64EV04'],'SCYQ64EV05'=>$_POST['SCYQ64EV05'],'SCYQ64EV06'=>$_POST['SCYQ64EV06'],'SCUSER'=>$_POST['SCUSER'],'SCYQ64AVGP'=>$SCYQ64AVGP,'SCYQ64TZ'=>$_POST['SCYQ64TZ'],'SCJOBN'=>$_POST['SCJOBN'],'SCPID'=>$_POST['SCPID'],'SCUPMJ'=>$_POST['SCUPMJ'],'SCUPMT'=>$_POST['SCUPMT']);
                if($post = array_filter($_POST))
                    {
                        if($fields = array_intersect_key($searchFields, $post))
                            {

                                foreach($fields as $key=>$field)
                                    {
                                        $setfields[] = "$key='$field'";
                                        $gbind[":$key"] = $_POST[$key]; 
                                    }

                                 echo $gsetString = implode(',', $setfields);
                                $Users = new Users($db);
                                $query = $Users->smipleUpdate('FQ64000',$gsetString,'SCYQ64UKID='.$SCYQ64UKID);

                            }

                    }

            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.