html_form_to_table.html
<form action="data_insert.php" method="post">
<label>Name</label><br>
<input type="text" name="name">
<br><br>
<label>Last Name</label><br>
<input type="text" name="lastname">
<br><br>
<label>Telephon</label><br>
<input type="text" name="telephon">
<br><br>
<label>email</label><br>
<input type="text" name="email">
<br><br>
<label>Address</label><br>
<input type="text" name="address">
<br><br>
<input type="submit" value="Submit">
</form>
This is an HTML form that allows a user to input data to be inserted into a database table. The form uses the POST method to send the data to a script called "data_insert.php" for processing.
The form contains the following input fields:
- Name: a text field for the user's first name
- Last Name: a text field for the user's last name
- Telephon: a text field for the user's telephone number
- email: a text field for the user's email address
- Address: a text field for the user's address
There is also a "Submit" button that the user can click to submit the form data.
data_insert.php
<?php
$name = $_POST["name"];
$lastname = $_POST["lastname"];
$telephon = $_POST["telephon"];
$email = $_POST["email"];
$address = $_POST["address"];
//Config
$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";
//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);
//Check Connection
if (!$connection) {
die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
echo "Connection Successfull <br>";
}
//SQL Command
$sql_command = "INSERT INTO people (id, name, lastname, telephon, email, address)
VALUES (NULL, '$name', '$lastname', '$telephon', '$email', '$address')";
if (mysqli_query($connection, $sql_command)) {
echo "SQL Command OK";
} else {
echo "SQL Error " . mysqli_error($connection) ;
}
?>
This PHP code establishes a connection to a MySQL server and inserts the values from the HTML form into the people
table using an INSERT INTO
SQL command.
Just make sure that the database connection details (server, user, password, database) are correct and that the people
table exists in the database.
After we submit some data:
Connection Successfull
SQL Command OK
And when we check html_table_report.php:
Connection Successfull
Results:
ID Name Lastname Telephon email Address Data/Time
1 Slavoj Zizek 222555 slavojzizek@someserver.com Slavoj Zizek 123 2020-07-25 15:34:45
2 Jordan Peterson 888454 jordan@server.com Jordan P. 234 2020-07-25 15:35:43
No comments:
Post a Comment