Send html form data to sql database via php (using mysqli)

To send HTML form data to a SQL database using PHP, you will need to use the mysqli (MySQL Improved) extension in PHP. Here is an example of how to do this:

  1. Connect to the database using mysqli_connect() function
<?php

$conn = mysqli_connect("hostname","username","password","database_name");
  1. Check for connection errors using mysqli_connect_errno() function
<?php

if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

Watch a course Learn object oriented PHP

  1. Retrieve the form data using the $_POST or $_GET superglobal
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
  1. Create a SQL query to insert the form data into the database
<?php

$sql = "INSERT INTO table_name (name, email, message) VALUES ('$name', '$email', '$message')";
  1. Execute the query using mysqli_query() function
<?php

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
  1. Close the connection using mysqli_close() function
<?php

mysqli_close($conn);

It is important to note that this is a very basic example and you should take security measures such as using prepared statements and escaping user input to prevent SQL injection attacks.