Send Form Fields Values to PHPMYADMIN.
First, it is an example code for various input fields like text, email, password, checkbox, radio, etc. So try to understand this operation and then insert your fields according to the input type. We will send all form field values to the MySQL database with the following requirements.
- Each record should have its unique ID (automatically).
- Get the date and time when the record was added.
- Save text, number, and email as it is.
- Store the password in a hash value for security.
- Store multiple checkbox values and separate them with commas.
Follow the given steps.
- First, create a database in phpmyadmin. Here is an example of what you can create.
CREATE DATABASE savetodb
- Select the database and create a table under it according to your form, here is an example of what we are creating.
CREATE TABLE `savetodb`.`formfields`
(
`auto id` INT NOT NULL auto_increment,
`text field` TEXT NOT NULL,
`number field` INT NOT NULL,
`email field` VARCHAR(50) NOT NULL,
`password field` VARCHAR(50) NOT NULL,
`url field` VARCHAR(255) NOT NULL,
`select field` VARCHAR(50) NOT NULL,
`date field` DATETIME NOT NULL,
`textarea field` TEXT NOT NULL,
`radio field` VARCHAR(50) NOT NULL,
`checkbox field` TEXT NOT NULL,
`submit date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`auto id`)
)
engine = innodb;
- Now add these attributes to your HTML, you can change the name but you should know that you have to change it in PHP as well.
<!-- Link PHP file in action and method will be POST -->
<form action="saveToDB.php" method="POST">
<!-- For Text Field -->
<input type="text" name="hcText">
<!-- For Number Field -->
<input type="number" name="hcNumber">
<!-- For Email Field -->
<input type="email" name="hcEmail">
<!-- For Password Field -->
<input type="password" name="hcPassword">
<!-- For URL Field -->
<input type="url" name="hcURL">
<!-- For Select Field -->
<select name="hcSelect">
<option value="First Option">First Option</option>
<option value="Second Option">Second Option</option>
<option value="Third Option">Third Option</option>
<option value="Fourth Option">Fourth Option</option>
<option value="Fifth Option">Fifth Option</option>
</select>
<!-- For Date Field -->
<input type="date" name="hcDate">
<!-- For Textarea -->
<textarea name="hcTextarea"></textarea>
<!-- For Radio "Name will be same and one must be checked otherwise it will show the error kind of array..."-->
<input type="radio" name="hcRadio" value="Option 01">
<label>Option 01</label>
<input type="radio" name="hcRadio" value="Option 02">
<label class="form-check-label">Option 02</label>
<!-- For Checkbox "[ ] <- dont change it, it's helping getting multiple values"-->
<input type="checkbox" value="Option 01" name="hcCheckbox[ ]">
<label>Option 01</label>
<input type="checkbox" value="Option 02" name="hcCheckbox[ ]">
<label>Option 02</label>
<input type="checkbox" value="Option 03" name="hcCheckbox[ ]">
<label>Option 03</label>
- Now add the PHP script that is attached to your form action action="saveToDB.php". Don't forget to set the name of the variables, and form fields in the INSERT query.
<?php
// Creating root Variables
$server_name = "localhost";
$user_name = "root";
$password = "";
$db_name = "saveToDB";
// Creating Connection
$conn = new mysqli($server_name, $user_name, $password, $db_name);
// Checking Connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Getting Records
$hcText = $_POST['hcText'];
$hcNumber = $_POST['hcNumber'];
$hcEmail = $_POST['hcEmail'];
$hcPassword = $_POST['hcPassword'];
$hcPasswordHash = password_hash($hcPassword, PASSWORD_DEFAULT); // Hashing Password for security
$hcURL = $_POST['hcURL'];
$hcSelect = $_POST['hcSelect'];
$hcDate = $_POST['hcDate'];
$hcTextarea = $_POST['hcTextarea'];
$hcRadio = $_POST['hcRadio'];
$hcCheckbox = $_POST['hcCheckbox'];
// For adding Multiple Values in checkbox
$hcCheckboxMult="";
foreach($hcCheckbox as $chkBx)
{
$hcCheckboxMult .= $chkBx.",";
}
// Inserting to Database
$sql = "INSERT INTO `formfields`
(`auto id`,
`text field`,
`number field`,
`email field`,
`password field`,
`url field`,
`select field`,
`date field`,
`textarea field`,
`radio field`,
`checkbox field`,
`submit date`)
VALUES (NULL,
'$hcText',
'$hcNumber',
'$hcEmail',
'$hcPasswordHash',
'$hcURL',
'$hcSelect',
'$hcDate',
'$hcTextarea',
'$hcRadio',
'$hcCheckboxMult',
CURRENT_TIMESTAMP()); ";
// Checking Excuting Queries
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
- All set :)
0 Comments