How can data from textboxes be retrieved and stored in a database using PHP?
To retrieve data from textboxes and store it in a database using PHP, you can use the $_POST superglobal to access the values submitted through a form. Then, you can use SQL queries to insert the data into the database.
<?php
// Retrieve data from textboxes
$textbox1 = $_POST['textbox1'];
$textbox2 = $_POST['textbox2'];
// Connect to the database
$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);
}
// Insert data into the database
$sql = "INSERT INTO table_name (column1, column2) VALUES ('$textbox1', '$textbox2')";
if ($conn->query($sql) === TRUE) {
echo "Data inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>