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());
                }
        }

Why is your $whereString before the WHERE clause?

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

Member Avatar for diafol

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

And?

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);

                            }

                    }

            }
        }
Member Avatar for diafol

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);

}

Thanks diafol.

i will get back once it tested

Diafol its not working

Member Avatar for diafol

Diafol its not working

Well that was helpful. Can you give us a clue?

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

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);

                            }

                    }

            }
        }
Member Avatar for diafol

Great. :)

Thanks for your support diafol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.