public function insert($tablename, $datafields = array(), $tablefields = array()) { //insert data $dbconfig = new DBconfig(); $host = $dbconfig->host(); $user = $dbconfig->username(); $pwd = $dbconfig->pwd(); $db = $dbconfig->db(); $datafld = DBmain::arraytostr2($datafields); //convert array to a single string separate with "," $tablefld = DBmain::arraytostr($tablefields); try { $conn = new PDO("mysql:host={$host};dbname={$db}", $user, $pwd); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO " . $tablename . " (" . $tablefld . ") VALUES (" . $datafld . ")"; // use exec() because no results are returned $conn->exec($sql); return true; } catch (PDOException $e) { echo $sql . "<br>" . $e->getMessage(); return false; } $conn = null; }
public function insert($tablename, $datafields = array(), $tablefields = array()) { //insert data $datafld = DBmain::arraytostr2($datafields); //convert array to a single string separate with "," $tablefld = DBmain::arraytostr($tablefields); try { $sql = "INSERT INTO " . $tablename . " (" . $tablefld . ") VALUES (" . $datafld . ")"; // use exec() because no results are returned $this->conn->exec($sql); return true; } catch (PDOException $e) { echo $sql . "<br>" . $e->getMessage(); return false; } $conn = null; }