I am trying to bulk insert some data in PHP from a textarea form, and then prevent duplications from already existing data. I tried to code something but right now it just inserts everything that I insert in the textarea in same "value" in the column. But I want each line from the textarea separated in each and own column.
Here is what I have so far, and this works but it inserts the whole row:
<?php
include '../includes/config.php';
if (isset($_POST["data"])) {
$_POST["ban"] = $value;
$line_data = explode("\n", $_POST["data"]);
foreach ($line_data as $key => $value) {
$checkQuery = "SELECT ip FROM blacklist WHERE ip='" . $_POST["ip"] . "'";
$checkResult = mysqli_query($con, $checkQuery);
if (mysqli_num_rows($checkResult) != 0){
// do nothing
} else {
$sql = "INSERT IGNORE INTO blacklist (ip) VALUES ('{$value}')";
$result = mysqli_query($con, $sql);
}
}
}
?>
<form action="bulk.php" method="post">
<textarea type="text" name="data" placeholder="IP address"></textarea><br/>
<button type="submit" name="ban">Ban</button>
</form>
So my question is, what am I doing wrong and is there good way to handle this? If so, how?
Edit: It inserts into it's own column now, but it still duplicates. Like it's still possible insert same thing many times even if data exists in database.