Adding a New Record to the Database in PHP
Let's now create a page new.php
for adding a new user to our database.
Let's create a form for this:
<form action="" method="POST">
<input name="name">
<input name="age">
<input name="salary">
<input type="submit">
</form>
After submitting the form, we will save its data to the database. First, let's catch the moment of form submission itself:
<?php
if (!empty($_POST)) {
// here will be the form processing code
}
?>
Inside the condition, let's get our data into variables:
<?php
$name = $_POST['name'];
$age = $_POST['age'];
$salary = $_POST['salary'];
?>
Let's form an INSERT query for the data:
<?php
$query = "INSERT INTO users SET name='$name', age='$age', salary='$salary'";
?>
Let's execute this query:
<?php
mysqli_query($link, $query) or die(mysqli_error($link));
?>
On the page new.php implement a form
for adding a new user.
Modify the previous task so that after submitting the form, the values from it are not cleared.