Return to Snippet

Revision: 60130
at October 22, 2012 21:28 by BergBenji


Initial Code
// Build the field:value array
$in = array();
$in['fieldname'] = 'Value';
$in['fieldname2'] = 'Value 2';

// Insert into db
$db->insert('tablename', $in);

// Method:
public function insert($table, $data)
    {
	ksort($data);
	$fieldnames = '`'.implode('`, `', array_keys($data)).'`';
	$fieldvalues = ':'.implode(', :', array_keys($data));
	
	$SQL = "INSERT INTO `{$table}` ({$fieldnames}) VALUES ({$fieldvalues})";
	$sth = $this->prepare($SQL);
	return $sth->execute($data);
    }

Initial URL

                                

Initial Description
Simple method to insert data to sql

Initial Title
Simple PDO Insert method

Initial Tags
mysql, php

Initial Language
PHP