BASIC CRUD

<div style=”width:20%;border:1px solid;margin:0 auto;”><h3 style=”text-align:center;margin:0 2px;”>Web- Alliance Shortcut</h3><span style=”display:block;text-align:center;color:red;”>By: Shoeb Ahmad</span></div>
<?
if(!empty($_REQUEST[‘fields’])){
$fields = $_REQUEST[‘fields’];
$table_name = $_REQUEST[‘table_name’];
}
?>
<html>
<body>
<form method=”post” action=”” >
Fields
<textarea cols=”30″ rows=”4″ name=”fields” ><?php echo $fields;?></textarea>
DB table<input type=”text” name=”table_name” value=”<?php echo $table_name;?>”>
<input type=”submit” name=”add” value=”submit”>
</form>
</body>
</html>
<?
if($_REQUEST[‘add’] == “submit”){
$fields = $_REQUEST[‘fields’];
$table_name = $_REQUEST[‘table_name’];
echo “<form name=’form1′ id=’form1′>”;
foreach(explode(“,”,$fields) as $field){
echo $field.”&nbsp;&nbsp;”;
echo “<select name=’$field’>”;
echo “<option value=’id_field’>id</option>”;
echo “<option value=’text’>text</option>”;
echo “<option value=’select’>select</option>”;
echo “<option value=’multiselect’>multiselect</option>”;
echo “<option value=’radio’>radio</option>”;
echo “<option value=’checkbox’>checkbox</option>”;
echo “<option value=’textarea’>textarea</option>”;
echo “<option value=’date’>date</option>”;
echo “<option value=’keycode’>keycode</option>”;
echo “<option value=’time’>time</option>”;
echo “<option value=’amount’>amount</option>”;
echo “</select>”;
echo “<br>”;
}
echo “<br>”;
echo “<input type=’hidden’ name=’fields’ value='”.$fields.”‘>”;
echo “<input type=’hidden’ name=’table_name’ value='”.$table_name.”‘>”;
echo “<input type=’submit’ name=’create’ value=’submit’>”;
echo “</form>”;
}
?>
<?php
if($_REQUEST[‘create’] == “submit”){
$fields = $_REQUEST[‘fields’];

echo “<font color=’red’>################################################## START FORM ##################################################</font><br>”;
foreach($_REQUEST as $val=>$field){
//echo $val.”–>”.$field;
if($field == “id”) continue;
if($field == “id_field”) continue;
if($val == “create”) continue;
if($val == “fields”) continue;
if($val == “table_name”) continue;
$placeholder = ucwords(str_replace(“_”,” “, $val));
echo htmlentities(“<div class=\”form-group\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<label class=\”col-sm-2 control-label\” for=\”$placeholder\”>$placeholder</label>”).”<br>”;
if($field == “text”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<input type=\”$field\” id=\”$val\” name=\”$val\” class=\”form-control\” value=\”<?php echo $”.$val.”;?>\” placeholder=\”$placeholder\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “select”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<?php echo $”.”grant->getCountyList(\”$val\”,\$”.$val.”,\”100%\”,false,\”form-control select2\”);?>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “multiselect”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<?php echo $”.”grant->getCountyList(\”$val\”,\$”.$val.”,\”100%\”,true,\”form-control select2\”);?>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “radio”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“No <input type=\”radio\” name=\”$val\” value=\”0\” class=\”flat-red $val\” <? echo (\$”.$val.” == 0) ? \”checked\” : \”\”; ?>>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“Yes <input type=\”radio\” name=\”$val\” value=\”1\” class=\”flat-red $val\” <? echo (\$”.$val.” == 1) ? \”checked\” : \”\”; ?>>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “checkbox”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<input type=\”checkbox\” class=\”flat-red\” id=\”$val\” name=\”$val\” value=\”1\” <? echo (\$”.$val.” == 1) ? \”checked\” : \”\”; ?>>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “textarea”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<textarea class=\”form-control\” rows=\”3\” id=\”$val\” name=\”$val\” placeholder=\”$placeholder\”><?php echo stripslashes(\$”.$val.”);?></textarea>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “date”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<input type=\”text\” class=\”form-control datepicker\” id=\”$val\” name=\”$val\” value=\”<?php (isset(\$”.$val.”) && \$”.$val.” !=\”\”) ? utility::getFormatedDate(\$”.$val.”) : \”\”;?>\” placeholder=\”$placeholder\” autocomplete=\”off\” >”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “keycode”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<? echo Keycode::getKeycodeListValue(\”$val\”, \$”.$val.”, \$width = \”100%\”, \”$placeholder\”, \”id\”, \”code\”, \”$val\”,\”\”,\”form-control select2\”); ?>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “time”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”bootstrap-timepicker\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<input type=\”$field\” id=\”$val\” name=\”$val\” class=\”form-control timepicker\” value=\”<?php echo $”.$val.”;?>\” placeholder=\”$placeholder\” autocomplete=\”off\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}else if($field == “amount”){
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<div class=\”col-sm-10\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“<input type=\”$field\” id=\”$val\” name=\”$val\” class=\”form-control\” value=\”<?php echo ($”.$val.” != 0) ? $”.$val.” : \”\”;?>\” placeholder=\”$placeholder\”>”).”<br>”;
echo “&nbsp;&nbsp;&nbsp;&nbsp;”.htmlentities(“</div>”).”<br>”;
}
echo htmlentities(“</div>”).”<br>”;
}
echo “<font color=’red’>################################################## END FORM ##################################################</font><br><br>”;

$insert_values = “”;
$update_values = “”;
$tab = “&nbsp;&nbsp;&nbsp;&nbsp;”;
echo “<font color=’red’>################################################## START INSERT ##################################################</font>”;
echo “<br>”;
echo “if(isset($”.”_REQUEST[‘action’]) && $”.”_REQUEST[‘action’] == ‘insert’){“;
echo “<br>”;
echo $tab.”utility::addslashesOnAllInput(array(\”query_string\”));”.”<br>”;
echo “<br>”;
//foreach(explode(“,”,$_REQUEST[‘fields’]) as $field){
foreach($_REQUEST as $field=>$val){
if($field == “id”) continue;
if($field == “create”) continue;
if($field == “fields”) continue;
if($field == “table_name”) continue;

$field = trim($field);
if(empty($field)) continue;
if($val == “date”){
echo “$tab$”.$field.” = utility::setFormatedDate($”.”_REQUEST[‘$field’]);<br>”;
}elseif($val == “multiselect”){
echo “$tab$”.$field.” = implode(\”,\”,$”.”_REQUEST[‘$field’]);<br>”;
}else{
echo “$tab$”.$field.” = $”.”_REQUEST[‘$field’];<br>”;
}
$insert_values.=”‘$”.$field.”‘,”;
}
echo “$tab\$created_on = date(\”Y-m-d\”);<br>”;
echo “$tab\$updated_by = \$_SESSION[\”user_id\”];<br>”;
$insert_values = substr($insert_values,0,-1);

echo “<br>”;
echo “$tab$”.”sql= \”INSERT INTO $”.”table_name ($fields,created_on,updated_by) VALUES($insert_values,’\$created_on’,’\$updated_by’)\”;”;
echo “<br>”;
echo $tab.”mysql_query($”.”sql);”;
echo “<br>”;
echo “<br>”;
echo $tab.”\$_SESSION[‘info_message’] = \””.ucfirst(str_replace(“_”,” “,$table_name)).” added sucessfully\”;”;
echo “<br>”;
echo $tab.”header(\”Location: $”.”file_name\”);”;
echo “<br>”;
echo $tab.”exit();”;
echo “<br>”;
echo “}”;
echo “<br>”;

echo “<font color=’red’>################################################# END INSERT #################################################</font><br>”;

echo “<font color=’red’><br>################################################## START EDIT ##################################################</font>”;
echo “<br>”;
echo “if(isset($”.”_REQUEST[‘action’]) && $”.”_REQUEST[‘action’] == ‘edit’){“;
echo “<br>”;
echo “<br>”;
echo $tab.”$”.”id = $”.”_REQUEST[‘id’];”.”<br>”;
echo $tab.”$”.”form_action = \”update\”;”.”<br>”;
echo $tab.”$”.”button_value = \”Update\”;”.”<br>”;
echo $tab.”$”.”page_name = \”Edit “.ucfirst(str_replace(“_”,” “,$table_name)).”\”;”.”<br><br>”;
echo $tab.”$”.”query_string = (isset(\$_POST[‘query_string’])) ? \$_POST[‘query_string’] : \$_SERVER[‘QUERY_STRING’];”;

echo “<br><br>”;
echo “$tab$”.”sql= \”SELECT * FROM \$table_name WHERE id = ‘\$id’ \”;”;
echo “<br>”;
echo $tab.”$”.”result_edit = mysql_query($”.”sql);”;
echo “<br>”;
echo $tab.”$”.”row_edit = mysql_fetch_array($”.”result_edit);”;
echo “<br><br>”;
foreach($_REQUEST as $field=>$val){
if($field == “id”) continue;
if($field == “create”) continue;
if($field == “fields”) continue;
if($field == “table_name”) continue;

$field = trim($field);
if(empty($field)) continue;
if($val == “multiselect”){
echo “$tab$”.$field.”= explode(\”,\”,$”.”row_edit[‘$field’]);<br>”;
}else{
echo “$tab$”.$field.”= $”.”row_edit[‘$field’];<br>”;
}
$insert_values.=”‘$”.$field.”‘,”;
}
$insert_values = substr($insert_values,0,-1);
echo “}”;
echo “<br>”;
echo “<font color=’red’> ################################################# END EDIT #################################################</font>”;

echo “<font color=’red’> <br><br>################################################## START UPDATE ################################################## </font>”;
echo “<br>”;
echo “if(isset($”.”_REQUEST[‘action’]) && $”.”_REQUEST[‘action’] == ‘update’){“;
echo “<br>”;
echo $tab.”utility::addslashesOnAllInput(array(\”query_string\”));”.”<br>”;
echo “<br>”;
echo “$tab”.”\$id = \$_REQUEST[‘id’];<br>”;
foreach($_REQUEST as $field=>$val){
if($field == “id”) continue;
if($field == “create”) continue;
if($field == “fields”) continue;
if($field == “table_name”) continue;
$field = trim($field);
if(empty($field)) continue;

if($val == “date”){
echo “$tab$”.$field.” = utility::setFormatedDate($”.”_REQUEST[‘$field’]);<br>”;
}elseif($val == “multiselect”){
echo “$tab$”.$field.” = implode(\”,\”,$”.”_REQUEST[‘$field’]);<br>”;
}else{
echo “$tab$”.$field.” = $”.”_REQUEST[‘$field’];<br>”;
}
//echo “$tab$”.$field.”= $”.”_REQUEST[‘$field’];<br>”;
//$insert_values.=”‘$”.$field.”‘,”;

//echo “$”.$field.”= $”.”_REQUEST[‘$field’];<br>”;
$update_values.= “$field = ‘$”.$field.”‘, “;

}
echo “$tab\$updated_by = \$_SESSION[\”user_id\”];<br>”;

$update_values.= “updated_by = ‘\$updated_by’ “;
//$update_values = substr($update_values,0,-2);

echo “<br>”;
echo “$tab$”.”sql= \”UPDATE \$table_name SET $update_values WHERE id = ‘\$id’ \”;”;
echo “<br>”;
echo $tab.”mysql_query($”.”sql);”;
echo “<br><br>”;
echo $tab.”$”.”query_string = (isset(\$_REQUEST[‘query_string’]) && !empty(\$_REQUEST[‘query_string’])) ? \$_REQUEST[‘query_string’] : \”\”;”.”<br>”;
echo $tab.”$”.”query_string = str_ireplace(\”page=add\”, \”page=view\”, \$query_string);”.”<br>”;
echo $tab.”$”.”query_string = \”&\”.\$query_string;”.”<br>”;
echo “<br>”;
echo $tab.”\$_SESSION[‘info_message’] = \””.ucfirst(str_replace(“_”,” “,$table_name)).” updated sucessfully\”;”;
echo “<br>”;
echo $tab.”header(\”Location: $”.”file_name\”);”;
echo “<br>”;
echo $tab.”exit();”;
echo “<br>”;
echo “}”;
echo “<br>”;
echo “<font color=’red’> ################################################# END UPDATE #################################################</font>”;

echo “<font color=’red’> <br><br>################################################## START DELETE ##################################################</font>”;
echo “<br>”;
echo “if(isset($”.”_REQUEST[‘action’]) && $”.”_REQUEST[‘action’] == ‘delete’){“;
echo “<br>”;
echo “<br>”;
echo $tab.”$”.”id = $._REQUEST[‘id’];”;
echo “<br>”;
echo $tab.”$”.”sql= \”DELETE FROM \$table_name WHERE id = ‘\$id’ \”;”;
echo “<br>”;
echo $tab.”mysql_query($”.”sql);”;
echo “<br><br>”;
echo $tab.”\$_SESSION[‘info_message’] = \””.ucfirst(str_replace(“_”,” “,$table_name)).” deleted sucessfully\”;”;
echo “<br>”;
echo $tab.”header(\”Location: $”.”file_name\”);”;
echo “<br>”;
echo $tab.”exit();”;
echo “<br>”;
echo “}”;
echo “<br>”;
echo “<font color=’red’>################################################# END DELETE #################################################</font><br><br>”;

echo “<font color=’green’>################################################# START CREATE TABLE #################################################</font><br><br>”;
$create_table = “”;
$create_table .= “CREATE TABLE IF NOT EXISTS `$table_name` (“.”<br>”;
$create_table .= “`id` int(11) NOT NULL AUTO_INCREMENT,”.”<br>”;
foreach($_REQUEST as $field=>$val){
if($field == “id”) continue;
if($field == “create”) continue;
if($field == “fields”) continue;
//if($field == “table_name”) continue;
$field = trim($field);
if(empty($field)) continue;

if($val == “id_field”){
$create_table .= “`$field` int(11) NOT NULL,”.”<br>”;
}elseif($val == “text”){
$create_table .= “`$field` varchar(255) NOT NULL,”.”<br>”;
}elseif($val == “select”){
$create_table .= “`$field` int(11) NOT NULL,”.”<br>”;
}elseif($val == “multiselect”){
$create_table .= “`$field` varchar(100) NOT NULL,”.”<br>”;
}elseif($val == “radio”){
$create_table .= “`$field` tinyint(4) NOT NULL,”.”<br>”;
}elseif($val == “checkbox”){
$create_table .= “`$field` tinyint(4) NOT NULL,”.”<br>”;
}elseif($val == “textarea”){
$create_table .= “`$field` text NOT NULL,”.”<br>”;
}elseif($val == “date”){
$create_table .= “`$field` datetime NOT NULL,”.”<br>”;
}elseif($val == “keycode”){
$create_table .= “`$field` int(11) NOT NULL,”.”<br>”;
}elseif($val == “time”){
$create_table .= “`$field` time NOT NULL,”.”<br>”;
}elseif($val == “amount”){
$create_table .= “`$field` double NOT NULL,”.”<br>”;
}

}
//$create_table = substr($create_table,0,-2);
$create_table .= “`created_on` datetime NOT NULL,”.”<br>”;
$create_table .= “`updated_by` int(11) NOT NULL,”.”<br>”;
$create_table .= “`updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,”.”<br>”;
$create_table .= “PRIMARY KEY (`id`)”.”<br>”;
$create_table .= ” );”;
echo $create_table;
echo “<br><br><font color=’green’>################################################# END CREATE TABLE #################################################</font>”;
}
?>