Source: sqlform.phps - download
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sqlform.phps 0.1
* by daif alotaibi (http://daif.net)
* daif@daif.net
*
* Link:
* http://daif.net/script/sqlform.phps
*
* Examples:
* $forms = [
* 'form1' => 'INSERT INTO `table1` (`id`,`column1`,`column2`, ...) VALUES (NULL, :column1, :column2, ...)',
* 'form2' => 'INSERT INTO `table2` (`id`,`date`,`column1`, ...) VALUES (NULL, NOW(), :column1, ...)',
* ];
* sqlform.php?form=form1
* sqlform.php?form=form2
*
*/
// Configurations
$config = [
// Database
'db_dsn'=> 'mysql:host=localhost;dbname=wordpress',
'db_usr'=> 'root',
'db_pwd'=> '',
'db_opt'=> [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"],
];
// A list of SQL statements to generate forms
$forms = [
'wp_users' => 'INSERT INTO `wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES (NULL, :user_login, :user_pass, :user_nicename, :user_email, :user_url, :user_registered, :user_activation_key, :user_status, :display_name)',
'wp_options' => 'INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES (NULL, :option_name, :option_value, :autoload)',
];
// Connecting to database
$conn = new PDO($config['db_dsn'], $config['db_usr'], $config['db_pwd'], $config['db_opt']);
if(isset($_GET['form'])) {
if(isset($forms[$_GET['form']])){
$sql = $forms[$_GET['form']];
if(preg_match_all('/:(.+)(,|\))/Uis', $sql, $matches)) {
$form = $matches[1];
} else {
$error = 'Requested form is not valid.';
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$sth = $conn->prepare($sql);
foreach ($_POST as $key => $value) {
$sth->bindValue(':'.$key, $value);
}
if(!$sth->execute()) {
$error = 'Error: '.$sth->errorInfo()[2];
}
}
} else {
$error = 'Requested form is not available.';
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SQLForm</title>
<style type="text/css">
body{
margin: 0.5em;
font-family: Arial, Tahoma, sans-serif;
color: #222;
}
.buttons{
border-bottom:1px solid #ccc;
margin-bottom: 15px;
padding-bottom: 8px;
}
.container{
max-width: 960px;
}
.form-group {
margin-bottom: 1rem;
width: 100%;
}
.form-group label {
display: inline-block;
padding: .3rem 0 ;
font-size: 1rem;
line-height: 1.5;
}
.form-control {
width: 100%;
padding: .3rem;
font-size: 1rem;
line-height: 1.5;
border: 1px solid #ced4da;
border-radius: .25rem;
}
.form-control:focus {
border-color: #5cb3fd;
outline: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="buttons">
<?php foreach ($forms as $name => $var) { ?>
[<a href="sqlform.php?form=<?php echo $name?>"><?php echo $name?></a>]
<?php } ?>
</div>
<?php if(isset($error)) { ?>
<?php echo $error?>
<?php } ?>
<?php if(isset($sql)) { ?>
<form method="POST">
<h4 style="margin-top: 5px; margin-bottom: 15px;"><?php echo $_GET['form']?></h4>
<?php foreach ($form as $key => $input) { ?>
<div class="form-group">
<label for="<?php echo $input?>"><?php echo str_replace('_', ' ', $input)?></label>
<input type="text" class="form-control" name="<?php echo $input?>" id="<?php echo $input?>" />
</div>
<?php } ?>
<div>
<button type="submit">Send</button>
</div>
</form>
<?php } ?>
</div>
</body>
</html>