Skip to content

Instantly share code, notes, and snippets.

@RGKrish183
Created February 10, 2016 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RGKrish183/f6fa4d36320f6075cf68 to your computer and use it in GitHub Desktop.
Save RGKrish183/f6fa4d36320f6075cf68 to your computer and use it in GitHub Desktop.
Simple Backend Form Generation and Validation
<?php
if($_SERVER['HTTP_HOST'] == 'localhost')
{
$server_hostname = "localhost:3306";
$server_username = "root";
$server_password = "";
}
else
{
$server_hostname = "";
$server_username = "";
$server_password = "";
}
$connection = mysql_connect($server_hostname, $server_username, $server_password);
try
{
if($connection)
mysql_select_db("work");
else
throw new Exception("Error : ".mysql_error());
$echo = '';
$Qry_ST = mysql_query("show tables");
if(mysql_num_rows($Qry_ST) > 0)
{
echo '<form method="post" action="" enctype="multipart/form-data">';
echo '<label for="table_name">Choose Table:</label><br>';
echo '<h3><select name="table_name" id="table_name" style="height:25px; min-width:500px;" >';
while($table = mysql_fetch_array($Qry_ST))
{
$selected_table = ($_REQUEST['table_name'] == $table[0]) ? 'selected':'';
echo '<option value="'.$table[0].'" '.$selected_table.' >'.$table[0].'</option>';
}
echo '</select></h3>';
echo '<input type="submit" name="submit_action" id="submit_action" value="Create Frame" />';
echo '</form>';
$SQry_Frm = "select * from ".$_POST['table_name']." ";
$Rslt_Frm = mysql_query($SQry_Frm);
if(mysql_num_fields($Rslt_Frm))
{
$echo .= '<fieldset>
<legend>Report</legend>
<form method="post" action="" enctype="multipart/form-data">
<table width="100%" >';
for ($i = 0; $i < mysql_num_fields($Rslt_Frm); ++$i)
{
$field_name = mysql_field_name($Rslt_Frm, $i);
$field_type = mysql_field_type($Rslt_Frm, $i);
$flags = mysql_field_flags($Rslt_Frm, $i);
$label = ucfirst(str_replace('_', ' ', $field_name));
$values_arr = set_and_enum_values($_POST['table_name'], $field_name);
if($field_type == 'string' && $flags == 'binary')
{
$echo .= '<tr height="30">
<td><label for="'.$field_name.'">'.$label.'</label></td>
<td><input type="text" id="'.$field_name.'" name="'.$field_name.'" placeholder="'.$label.'" required autocomplete="off" /></td>
</tr>';
}
elseif($field_type == 'blob' && in_array('text', $values_arr))
{
$echo .= '<tr height="30">
<td><label for="'.$field_name.'">'.$label.'</label></td>
<td><textarea id="'.$field_name.'" name="'.$field_name.'" placeholder="'.$label.'" required></textarea></td>
</tr>';
}
elseif($field_type == 'real')
{
$echo .= '<tr height="30">
<td><label for="'.$field_name.'">'.$label.'</label></td>
<td><input type="text" id="'.$field_name.'" name="'.$field_name.'" placeholder="'.$label.'" required autocomplete="off" /></td>
</tr>';
}
elseif($field_type == 'string' && $flags == 'binary enum')
{
$echo .= '<tr height="30">
<td><label for="'.$field_name.'">'.$label.'</label></td>
<td>';
foreach($values_arr as $enum_values)
$echo .= '<input name="'.$field_name.'" type="radio" value="'.$enum_values.'" />&nbsp;'.$enum_values;
$echo .= '</td>
</tr>';
}
elseif($field_type == 'int' && $flags <> 'not_null primary_key auto_increment')
{
$echo .= '<tr height="30">
<td><label for="'.$field_name.'">'.$label.'</label></td>
<td><input type="number" id="'.$field_name.'" name="'.$field_name.'" placeholder="'.$label.'" required autocomplete="off" /></td>
</tr>';
}
$subQry = $field_name;
}
$echo .= '<tr height="30">
<td>&nbsp;</td>
<td><input type="submit" id="Submit" name="Submit" value="Accept" /></td>
</tr>';
$echo .= '</form>
</table>
</fieldset>';
$table_name = 'report_'.$_POST['table_name'];
$file_name = "$table_name.php";
$fp=fopen($file_name,'w');
fwrite($fp, $echo);
fclose($fp);
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
exit();
}
function set_and_enum_values( $table , $field )
{
$query = "SHOW COLUMNS FROM `$table` LIKE '$field'";
$result = mysql_query( $query ) or die( 'Error getting Enum/Set field ' . mysql_error() );
$row = mysql_fetch_array($result);
if(stripos(".".$row[1],"enum(") > 0)
$row[1]=str_replace("enum('","",$row[1]);
else
$row[1]=str_replace("set('","",$row[1]);
$row[1]=str_replace("','","\n",$row[1]);
$row[1]=str_replace("')","",$row[1]);
$ar = split("\n",$row[1]);
for($i=0;$i<count($ar);$i++)
$arOut[str_replace("''","'",$ar[$i])]=str_replace("''","'",$ar[$i]);
return $arOut ;
}
?>
/*
SQLyog Community Edition- MySQL GUI v8.12
MySQL - 5.5.20-log : Database - work
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`work` /*!40100 DEFAULT CHARACTER SET utf16 COLLATE utf16_bin */;
USE `work`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf16_bin DEFAULT NULL,
`email` varchar(20) COLLATE utf16_bin DEFAULT NULL,
`password` varchar(20) COLLATE utf16_bin DEFAULT NULL,
`mobile` double DEFAULT NULL,
`address` text COLLATE utf16_bin,
`gender` enum('Male','Female') COLLATE utf16_bin DEFAULT NULL,
`age` tinyint(11) NOT NULL DEFAULT '0',
`created_date` datetime DEFAULT NULL,
`modified_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf16 COLLATE=utf16_bin CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
@RGKrish183
Copy link
Author

Hi,
Here I've created simple back end form generation and validation, in this module have to create database name of work and table name of user and run the above mentioned _creative_webform.php, it will show table list in the dropdown and choose table name and get the designed php file. it complete works based on table data type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment