1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<?php
final class crud {
public function __construct($connexionName) {
$this->connexionName = $connexionName;
}
public final function insert($tableName, $fields=array()){
$this->tableName = $tableName;
$this->fields = $fields;
foreach ($this->fields as $vf) {
$inKeys[] = $vf;
$inKeysDotted[] = ':' . $vf;
$insImKeys = implode(', ', $inKeys);
$insImKeysDotted = implode(', ', $inKeysDotted);
$this->insImKeys = $insImKeys;
$this->insImKeysDotted = $insImKeysDotted;
}
$this->insertedKeys = $inKeys;
$this->insertedKeysDotted = $inKeysDotted;
//print_r($this->insertedKeys);
//echo '<br />';
$sql = "INSERT INTO `$this->tableName` ($this->insImKeys) VALUES ($this->insImKeysDotted);";
//echo $sql.'<br />';
$insertItems = $this->connexionName->prepare($sql);
$this->insertItems = $insertItems;
//print_r($insertItems).'<br />';
} // end prepareStm()
public final function bindParams($setValues=array()){
$combine = array_combine($this->insertedKeys, $setValues);
foreach ($combine as $getKey => $getVal) {
switch ($getVal) {
case is_int($getVal):
//echo $getVal .' is INT<br />';
$setType = PDO::PARAM_INT;
//return PDO::PARAM_INT;
break;
case is_bool($getVal):
//echo $getVal .' is BOOL<br />';
$setType = PDO::PARAM_BOOL;
//return PDO::PARAM_BOOL;
break;
case is_null($getVal):
//echo $getVal .' is NULL<br />';
$setType = PDO::PARAM_NULL;
//return PDO::PARAM_NULL;
break;
default:
//echo $getVal .' is STR<br />';
$setType = PDO::PARAM_STR;
//return PDO::PARAM_STR;
break;
return $setType;
}
echo "this->insertItems->bindParam($getKey, $getVal, $setType)<br />";
$this->insertItems->bindParam($getKey, $getVal, $setType);
//echo '<pre>';
//print_r($this->insertItems);
//echo '</pre>';
}
} // end bindParams()
public final function executeQuery(){
return $this->insertItems->execute();
}
}
require_once '../Included_Files/Connect.php';
$con = new crud($connexion);
echo '<br />';
$con->insert('test', array('field1', 'field2', 'field3'));
$con->bindParams(array('pour field1', 'pour field2', 'pour field3'));
$con->executeQuery();
?> |
Partager