Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active August 18, 2023 02:36
Show Gist options
  • Save code-boxx/610c325d577c8a3af210e5c41ad7f0c4 to your computer and use it in GitHub Desktop.
Save code-boxx/610c325d577c8a3af210e5c41ad7f0c4 to your computer and use it in GitHub Desktop.
PHP save HTML form into MYSQL database

PHP SAVE HTML FORM INTO MYSQL DATABASE

https://code-boxx.com/save-form-php-mysql/

NOTES

  1. Create a test database and import 1-dummy.sql.
  2. Change the database settings in 3-save.php to your own.
  3. Launch 2-form.php in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `survey` (
`email` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`color` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `survey`
ADD PRIMARY KEY (`email`);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Favorite Color Survey</title>
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<?php
// (A) SAVE SURVEY FORM ON SUBMIT
if (isset($_POST["email"])) { require "3-save.php"; }
?>
<!-- (B) SURVEY FORM -->
<form method="post">
<label>Email</label>
<input type="email" name="email" required>
<label>Name</label>
<input type="text" name="name" required>
<label>Favorite Color</label>
<input type="text" name="color" required>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?php
// (A) DATABASE CREDENTIALS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (B) CONNECT TO DATABASE
$pdo = new PDO(
"mysql:host=".DB_HOST.";charset=".DB_CHARSET.";dbname=".DB_NAME,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (C) INSERT
$stmt = $pdo->prepare("INSERT INTO `survey` (`email`, `name`, `color`) VALUES (?, ?, ?)");
$stmt->execute([$_POST["email"], $_POST["name"], $_POST["color"]]);
/* (X) SEND EMAIL
$message = "";
foreach ($_POST as $k=>$v) { $message .= "$k : $v\r\n"; }
mail("[email protected]", "Survey Received", $message);
*/
// (D) RESULTS
echo "OK";
/* (X) NOT IMPORTANT - COSMETICS */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
form {
width: 400px;
padding: 20px;
border: 1px solid #f1f1f1;
background: #f5f5f5;
}
label, input {
display: block;
width: 100%;
}
label { margin: 10px 0; }
label:first-child { margin-top: 0; }
input {
border: 1px solid #e7e7e7;
padding: 10px;
}
input[type=submit] {
margin-top: 20px;
border: 0;
color: #fff;
background: #1766c9;
cursor: pointer;
}
@equiline
Copy link

This is NOT the code used in the CodeBoxx Video !!

@code-boxx
Copy link
Author

This is NOT the code used in the CodeBoxx Video !!

Of course. The tutorial is updated but the video is created years ago.

EDIT: Thank you for the aggressive response, Darren. I have deleted it since there is not a hint of intelligence. 🤣 Sorry that I cannot update several hundreds of posts and videos. How about you donate $40000 to Code Boxx? That will hire a full-timer for a year and keep things updated as you wanted. Cheers!

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