Skip to content

Instantly share code, notes, and snippets.

@LoganGray
Last active November 30, 2018 22:07
Show Gist options
  • Save LoganGray/428e33412e5357b5de2170fd1c951aaa to your computer and use it in GitHub Desktop.
Save LoganGray/428e33412e5357b5de2170fd1c951aaa to your computer and use it in GitHub Desktop.
Creates basic SQL script of a table based on a form post
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.0/themes/prism.min.css">
</head>
<body>
<pre>
<?php var_dump($_POST);
$xx = count($_POST);
echo "<strong> Number of items in POST: $xx </strong>";
?>
</pre>
<pre class="language-sql"><code class="language-sql">
<span class="token keyword">CREATE TABLE</span> `Untitled` (
`id` int(0) <span class="token keyword">NOT NULL AUTO_INCREMENT</span>,
<?php
//foreach ($_POST as $key => $value)
//{
// echo $key.'='.$value.'<br />';
//}
foreach ($_POST as $key => $value) {
if (strpos($key,'date') !== false) {
echo "`$key` " . "<span class='token operator'> date(0) NULL,</span><br />";
} else {
echo "`$key` " . " varchar(255) NULL,<br />";
}
}
?>
`created_at` <span class="token keyword">timestamp DEFAULT CURRENT_TIMESTAMP</span>,
`updated_at` <span class="token keyword">timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</span>,
PRIMARY KEY (`id`)
);
</code>
</pre>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.0/prism.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment