What could be causing the issue where the desired data is not being inserted into the database table?
The issue of desired data not being inserted into the database table could be caused by incorrect SQL syntax, missing input data, or a problem with the database connection. To solve this issue, ensure that the SQL query is correctly formatted, all necessary input data is being passed to the query, and the database connection is established successfully.
<?php
// Establish database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve input data
$data1 = $_POST['data1'];
$data2 = $_POST['data2'];
// Prepare and execute SQL query
$sql = "INSERT INTO table_name (column1, column2) VALUES ('$data1', '$data2')";
if ($conn->query($sql) === TRUE) {
echo "Record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close database connection
$conn->close();
?>