Basically it look through the mp3 files in the folder and then insert into mysql database if number does not exist.
Problem is INSERT (mysql) has been inserted more than 1 time but with different member_id‘s with same number. This shouldn’t happen.
4 staffs are executing this scripts. What causing this and how to fix?
if ($handle = opendir($audioPath)) {
while (false !== ($entry = readdir($handle))) {
$file = pathinfo($entry);
if ($entry != "." && $entry != ".." && !is_dir($audioPath . "/" . $entry) && $file['extension'] == 'mp3') {
$number = $file['filename'];
$SQL = "SELECT mobile, status, member_id, count(*) as Total from result WHERE number = :number";
$query = $this->db->prepare($SQL);
$query->bindValue(":number", $number);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
// Check if number is exist in the table
if ($row['Total'] == 0) {
$SQL = "INSERT INTO result (number, status, status_date, member_id) VALUES ('$number', 'pending', now(), '$memberId')";
$queryInsert = $this->db->prepare($SQL);
$queryInsert->execute();
break;
}
}
}
}
after break it play mp3 via media player plugin.
You are probably running into a race condition where two people execute the script simultaneously. When the select is performed, the entry does not exist.
The best way to solve this is to add a UNIQUE constraint on your number field in MySQL. This will make sure the condition is matched at insertion. The query will fail, so you will need to handle the error.