Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert array values to a database table as key value pairs in php

Tags:

arrays

php

insert

I am using array where I insert data to a table call z_error_log. These inserted data gives me an output as follows in the table under the column 'data'.

(user_role,email,nic,password,payment,reg_date)=(2,[email protected],123456789045,$2y$10$R4MOTLHlhe1M9GCt0utxQO3d43oFrH.34ivCPPtHpral.cM/kW5sq,0,2020-01-13 14:44:27)

But What I actually need to do is, to insert those data to the table where it stored as follows.

user_role = 2,[email protected],nic=123456789045,password=$2y$10$R4MOTLHlhe1M9GCt0utxQO3d43oFrH.34ivCPPtHpral.cM/kW5sq,payment,reg_date=2020-01-13 14:44:27

I need to assign the value right after its name as in the above.

Here I add the Part of the class Vendor_cont and function in the Log_model.php file where I use to enter data to the z_error_log table.

================Vendor_cont.php====================

class Vendor_cont
{

//CLASS Content

  }
            }

            // ===================EDITED If Recaptcha Fail=====================================

             else {

                             $userData = array(
                             'user_role'=>'2',
                             'email' => $email,
                             'nic' => $nic,
                             'password' =>$password,
                             'payment' =>0,
                             'reg_date' =>date('Y-m-d H:i:s')
                          );

                $_SESSION['Emessages'] = 'Recaptcha Failed. Please try again later'; 

                $this->LModel->createErrorLog(3,'Vendor_cont/register_vendor/recaptcha_fail',$email,$userData);


                }

        //========================================================================================

}

==================Log_model.php==================

    public function createErrorLog($user,$function,$error_data,$data_obj){ 

            $ip=$_SERVER['REMOTE_ADDR'];
            $browser_os=  $_SERVER['HTTP_USER_AGENT'];

            $data= (array) $data_obj;

            // print_r($data);

            if (is_array($data)) {
                $val = '(' . implode(',', array_keys($data)) . ')';
                $val .= '=(' . implode(',', $data) . ')';
            } else {
                $val = $data;
            }


    // =============EDITED==============

            $oStmt= $this->oDb->prepare('INSERT INTO z_error_log (`function`, `error_data`,`data`,`user` ,`ip`,`browser_os`) VALUES (:function,:error_data,:data,:user,:ip,:browser_os)');
            $oStmt->bindParam(':function', $function, \PDO::PARAM_STR);
            $oStmt->bindParam(':error_data', $error_data, \PDO::PARAM_STR);
            $oStmt->bindParam(':data', $val, \PDO::PARAM_STR);
            $oStmt->bindParam(':user', $user, \PDO::PARAM_INT);
            $oStmt->bindParam(':ip', $ip, \PDO::PARAM_STR);
            $oStmt->bindParam(':browser_os', $browser_os, \PDO::PARAM_STR);
            $oStmt->execute();

            return  $this->oDb->lastInsertId();

        }

I really appreciate your help. Thanks in advance.

like image 562
Kash Avatar asked Apr 25 '26 17:04

Kash


1 Answers

Please modify your createErrorLog() function using below code. I hope you can get your desired output by using this.

$email = "[email protected]";
$nic ="nic";
$password ="Pass@123";

$userData = array(
     'user_role'=>'2',
     'email' => $email,
     'nic' => $nic,
     'password' =>$password,
     'payment' =>0,
     'reg_date' =>date('Y-m-d H:i:s')
);
foreach($userData as $key=>$val) {
    $finalValue .= $key.'='.$val.',';
}
echo rtrim($finalValue,",");

Replace your createErrorLog() function in your model file with below code and you are done.Remember to create data column in database as text type and pass the value as string.

public function createErrorLog($user,$function,$error_data,$data_obj){ 
        $ip=$_SERVER['REMOTE_ADDR'];
        $browser_os=  $_SERVER['HTTP_USER_AGENT'];
        $data= (array) $data_obj;

        foreach($data as $key=>$val) {
            $finalValue .= $key.'='.$val.',';
        }
        $val = rtrim($finalValue,",");

        // =============EDITED==============
            $oStmt= $this->oDb->prepare('INSERT INTO z_error_log (`function`, `error_data`,`data`,`user` ,`ip`,`browser_os`) VALUES (:function,:error_data,:data,:user,:ip,:browser_os)');
            $oStmt->bindParam(':function', $function, \PDO::PARAM_STR);
            $oStmt->bindParam(':error_data', $error_data, \PDO::PARAM_STR);
            $oStmt->bindParam(':data', $val, \PDO::PARAM_STR);
            $oStmt->bindParam(':user', $user, \PDO::PARAM_INT);
            $oStmt->bindParam(':ip', $ip, \PDO::PARAM_STR);
            $oStmt->bindParam(':browser_os', $browser_os, \PDO::PARAM_STR);
            $oStmt->execute();
            return  $this->oDb->lastInsertId();
    }
like image 74
Dinesh Chandra Avatar answered Apr 27 '26 07:04

Dinesh Chandra