How can PHP be used to process data from a textarea and insert it into a database as separate entries?
To process data from a textarea and insert it into a database as separate entries, you can use PHP to split the textarea input into separate lines and then insert each line into the database as a separate entry. This can be achieved by using the explode() function to split the textarea input into an array of lines, and then iterating over each line to insert it into the database.
// Assuming you have already established a database connection
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$textarea_input = $_POST['textarea_input'];
$lines = explode("\n", $textarea_input);
foreach ($lines as $line) {
$sql = "INSERT INTO your_table_name (column_name) VALUES ('$line')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
Keywords
Related Questions
- What are the potential pitfalls of trying to delete constants in PHP?
- What is the best practice for calling a function defined in one PHP file from another PHP file without including unnecessary code?
- How can PHP beginners effectively handle form submissions and data processing using superglobal arrays like $_POST?