In PHP file storage, how do I store results in a database?

In this guide, you'll learn how to store results in a database using JavaScript and PHP. This involves capturing data on the front-end with JavaScript, sending it to the server via an AJAX call, and inserting it into a database using PHP.

Keywords: PHP, JavaScript, AJAX, Database, MySQL, Web Development
Description: This tutorial covers the process of capturing user input through JavaScript, sending it to a PHP backend, and storing the data in a MySQL database.
<?php // Database connection $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Data from JavaScript $data = $_POST['data']; // SQL query to insert data $sql = "INSERT INTO results (data) VALUES ('$data')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>

Keywords: PHP JavaScript AJAX Database MySQL Web Development