PHP PDO插入方法

编程入门 行业动态 更新时间:2024-10-27 14:26:40
本文介绍了PHP PDO插入方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究一种PHP类方法,以便使用PDO将表单值插入mysql数据库.下面概述了这个想法,但是我无法弄清楚如何传入方法的第四个参数.有人可以解释如何做到这一点吗?

I'm working on a PHP class method to insert form values into mysql database with PDO. The idea is outlined below but I cannot figure out how to pass in the fourth parameter of the method. Could someone explain how to do this?

谢谢!

<?php class Contact { private $DbHost = DB_HOST; private $DbName = DB_NAME; private $DbUser = DB_USER; private $DbPass = DB_PASS; public function MySqlDbInsert($DbTableName, $DbColNames, $DbValues, $DbBindParams){ try{ $dbh = new PDO("mysql:host=$this->DbHost;dbname=$this->DbName",$this->DbUser,$this->DbPass, array(PDO::ATTR_PERSISTENT => true)); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $dbh->exec("SET CHARACTER SET utf8"); $sth = $dbh->prepare("INSERT INTO $DbTableName($DbColNames) VALUES ($DbValues)"); // i know this is all wrong ---------------- foreach($DbBindParams as $paramValue){ $sth->bindParam($paramValue); } // ---------------------------------------- $sth->execute(); } catch(PDOException $e){ $this->ResponseMessage(true, 'Database access FAILED!'); } } $object = new Contact(); $object->MySqlDbInsert( 'DbTableName', 'DbColName1, DbColName3, DbColName3', ':DbColValue1, :DbColValue2, :DbColValue3', // this part is all wrong ------------------- array( ':DbColValue1', $col1, PDO::PARAM_STR, ':DbColValue2', $col2, PDO::PARAM_STR, ':DbColValue2', $col3, PDO::PARAM_STR ) // ------------------------------------------ );

推荐答案

用于PDO中的动态插入,我使用以下功能.

for dynamic insert in PDO i use below function.

使用此传递的值以数组格式起作用:

for use this passed values in array format to function :

<?php class Contact { private $UploadedFiles = ''; private $DbHost = DB_HOST; private $DbName = DB_NAME; private $DbUser = DB_USER; private $DbPass = DB_PASS; private $table; function __construct() { $this->table = strtolower(get_class()); } public function insert($values = array()) { $dbh = new PDO("mysql:host=$this->DbHost;dbname=$this->DbName", $this->DbUser, $this->DbPass, array(PDO::ATTR_PERSISTENT => true)); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->exec("SET CHARACTER SET utf8"); foreach ($values as $field => $v) $ins[] = ':' . $field; $ins = implode(',', $ins); $fields = implode(',', array_keys($values)); $sql = "INSERT INTO $this->table ($fields) VALUES ($ins)"; $sth = $dbh->prepare($sql); foreach ($values as $f => $v) { $sth->bindValue(':' . $f, $v); } $sth->execute(); //return $this->lastId = $dbh->lastInsertId(); } }

并使用它:

$contact = new Contact(); $values = array('col1'=>'value1','col2'=>'value2'); $contact->insert($values);

更多推荐

PHP PDO插入方法

本文发布于:2023-10-25 03:13:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   PHP   PDO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!