@HexAndBugs's answer is incorrectly quoting the table name and columns in the query. Backticks should replace these single quotes. That said, even if the syntax is corrected, the query is still insecure and unstable. An entry like Seamus O'Brien
will cause the script to fail, not to mention the query is vulnerable to more sinister injection attacks. That answer simply should not be used by anyone for any reason.
Here is a complete solution assuming your form only includes name fields:
<input type="text" name="name[]">
<input type="text" name="name[]">
...repeat as many times as you like inside the form
Because the submission is intended to write data into the database, POST is the appropriate method. (GET should only be used when the intention is to fetch/read data from the database.)
In your submission receiving script, use the following snippet to:
- Check that there was a submission containing
name
data.
- Use a prepared statement with bound parameters and iterated executions for stability/security. I will not include a sanitizing step because I don't know your requirements, but this would be advisable so that your database data remains clean.
Code:
if (!empty($_POST['name'])) {
$conn = new mysqli("localhost", "root", "", "yourDB"); //apply your credentials
$stmt = $conn->prepare("INSERT INTO `tableName` (`name`) VALUES (?)");
$stmt->bind_param('s', $name);
foreach ($_POST['name'] as $name) {
// to sanitize the name string, perform that action here
// to disqualify an entry, perform a conditional continue to avoid the execute call
$stmt->execute();
}
}