John Davidson

database - PHP pdo query insert twice sometime


I have a android app which submit data through php but sometime the data insert twice when I submit the data.
I'm sure the issue is not on the app as the button is disabled once I press the button. Please help me spot my mistake. Thanks!
Following is the data inserted:

Following is my query:

$area = $_POST['area'];
$date = $_POST['date'];
$batch = $_POST['batch'];
$workerName = $_POST['workerName'];
$remarks = $_POST['remarks'];
$time = $_POST['time'];
$tank = $_POST['tank'];
$sid= '';
$submissionDateTime = $_POST['submissionDateTime'];

require_once 'connectdb.php';

$stmt = $conn ->prepare("SELECT DISTINCT sid FROM feedingforms WHERE area = :area AND tank = :tank AND date = :date AND batch = :batch AND time = :time");
$stmt->bindParam(":area", $_POST['area']);
$stmt->bindParam(":tank", $_POST['tank']);
$stmt->bindParam(":date", $_POST['date']);
$stmt->bindParam(":batch", $_POST['batch']);
$stmt->bindParam(":time", $_POST['time']);
switch ($stmt->rowCount() > 0){
case "0":
$stmt2 = $conn->prepare("INSERT INTO feedingforms(sid, area, date, batch, workerName, submissionDateTime, remarks, time, tank) VALUES(:sid, :area, :date,:batch,:workerName,:submissionDateTime, :remarks, :time,:tank)");
$stmt2->bindValue(":sid", $sid);
$stmt2->bindValue(":area", $area);
$stmt2->bindValue(":date", $date);
$stmt2->bindValue(":batch", $batch);
$stmt2->bindValue(":workerName", $workerName);
$stmt2->bindValue(":submissionDateTime", $submissionDateTime);
$stmt2->bindValue(":remarks", $remarks);
$stmt2->bindValue(":time", $time);
$stmt2->bindValue(":tank", $tank);

if ($stmt2->execute()) {
$result["success"]= "1";
$result["message"] = "success";
echo json_encode($result);
$conn = null;

$result["success"]= "0";
$result["message"] = "error";
echo json_encode($result);
$conn = null;
$result["success"]= "2";
$result["message"] = "duplicate";
echo json_encode($result);
$conn = null;


My Answer:
> It looks like you are using PHP PDO to insert data into a database. One common reason for data being inserted twice is if the form is submitted multiple times quickly, before the first insert operation is completed. This can happen if there is a delay in the server processing the request.

To prevent this, you can add a check in your PHP code to ensure that the data is only inserted once. One way to do this is by checking if the data already exists in the database before inserting it again. You can use a unique identifier (such as a primary key) to check for existing data.

Here's an example of how you can modify your code to prevent duplicate inserts:

// Your existing code to retrieve form data

require_once 'connectdb.php';

// Check if the data already exists in the database
$stmt = $conn->prepare("SELECT * FROM your_table WHERE area = :area AND date = :date AND batch = :batch");
$stmt->bindParam(':area', $area);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':batch', $batch);

if($stmt->rowCount() == 0) {
// Data does not exist, insert it
$insertStmt = $conn->prepare("INSERT INTO your_table (area, date, batch, workerName, remarks, time, tank, submissionDateTime) VALUES (:area, :date, :batch, :workerName, :remarks, :time, :tank, :submissionDateTime)");
$insertStmt->bindParam(':area', $area);
$insertStmt->bindParam(':date', $date);
$insertStmt->bindParam(':batch', $batch);
$insertStmt->bindParam(':workerName', $workerName);
$insertStmt->bindParam(':remarks', $remarks);
$insertStmt->bindParam(':time', $time);
$insertStmt->bindParam(':tank', $tank);
$insertStmt->bindParam(':submissionDateTime', $submissionDateTime);

echo "Data inserted successfully";
} else {
echo "Data already exists";

This code first checks if the data already exists in the database before inserting it. If the data is not found, it proceeds with the insert operation. This should help prevent duplicate inserts.

Rate this post

5 of 5 based on 7359 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us