<?php
namespace FormHandler;
use PDO;
class form{
const DSN = '';
const USER = '';
const PASSWORD = '';
const RETURNURL = '';
const ERRORUPDATE = '';
const ERRORDELETE = '';
const ERRORINSERT = '';
const ERRORLIST = '';
const ERRORCONNECT = '';
const ERRORINSERT = '';
const INSERTCONFIRM = '';
const ERRORSELECT = '';
private function __connect(){
try {
$dbh = new \PDO(\FormHandler\form::DSN, \FormHandler\form::USER, \FormHandler\form::PASSWORD);
return 'Connection established';
} catch (PDOException $e) {
return \FormHandler\form::ERRORCONNECT . $e->getMessage();
}
return $dbh;
}
public static function __init(){
$referer = $_SERVER['HTTP_REFERER'];
if($referer !== ''){
return \FormHandler\Form::RETURNURL;
}
$dbh = SELF::__connect();
$table_name = filter_var($_REQUEST['table_name'], FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>"/^[a-zA-Z0-9]{4,122}/")));
$insert_array = $_REQUEST['inserst_array'];
$delete = $_REQUEST['delete'];
$edit = $_REQUEST['edit'];
$select = $_REQUEST['select'];
if(isset($insert_array)){
$i = 0;
$arr_binder = array();
$insert_column_sequence = array();
foreach(array_keys($insert_array) as $insert_column){
if(end($insert_array)){
$insert_column .= $insert_column;
}else{
$insert_column .= $insert_column . ', ';
}
$insert_column_sequence[$i] = $insert_column;
$arr_binder[$i] = ':' . $insert_column;
$i++;
}
foreach(array_values($insert_array) as $insert_value){
$insert_value .= ' ,' . $insert_value;
}
$stmt = $dbh->prepare("INSERT INTO $table_name ($insert_column) VALUES ($insert_value)");
$i_bind = 0;
foreach($arr_binder as $arr_binding){
$stmt->bindParam($arr_binding[$i_bind], $insert_column_sequence[$i_bind], PDO::PARAM_STR);
$i_bind++;
}
$stmt->execute();
$count = $stmt->rowCount();
if($count === $i_bind)
return \FormHandler\form::INSERTCONFIRM;
}else{
return \FormHandler\form::ERRORINSERT;
}
}elseif(isset($delete)){
$delete_key = $delete['key'];
$delete_operator = $delete['operator'];
$delete_value = $delete['value'];
foreach($delete as $deleted){
$stmt = $dbh->prepare("DELETE FROM $table_name WHERE " . ":$deleted_key " . $deleted_operator . " '$deleted_value' LIMIT 1");
$stmt->bindParam(':' . $deleted['key'],$deleted['key'],PDO::PARAM_INT);
$stmt->execute();
}
return \FormHandler\form::DELETECONFIRM;
}elseif(isset($edit)){
$edit_arr = array();
$i_edit = 0;
$edit_key = $edit['key'];
$edit_operator = $edit['where_operator'];
$edit_value = $edit['value'];
foreach($edit as $edit_keys_values){
$edit_arr[$i] = ':' . $edit_keys_values['keys'];
$edit_value_to_update = $edit_keys_values['values_to_update'];
$stmt = $dbh->prepare("UPDATE $table_name SET $edit_arr[$i] = $edit_value_to_update WHERE $edit_key " . $edit_operator . " '$edit_value'");
$stmt->bindParam($edit_arr[$i],$edit_keys_values['values_to_update'],PDO::PARAM_STR);
$stmt->execute();
}
$error = $stmt->errorInfo();
die();
}elseif(isset($select)){
foreach($select as $select_values){
if(end($select_values)){
$select_values_for_selection .= $select_values;
}else{
$select_values_for_selection .= $select_values . ', ';
}
}
$sql = 'SELECT ' . $selected_values_for_selection . ' FROM ' . $table_name . ' ORDER BY ' . $select['order_by_value'] . $select['order_by_order'];
foreach ($dbh->query($sql) as $row) {
}
return $row;
}else{
return \FormHandler\form::ERRORSELECT;
}
}
}
// Example..
//\FormHandler\form::__init();
|