I have multiple If statements that if true, inserts data into the db. The Problem is that the 2nd one of the INSERT statements inserts it 4 times. How would i stop it from adding duplicates. Also the returned $_POST[‘tshirt’] contains only one value which is being inserted 4 times.
if(isset($_POST['distance'])) {
$dist = $_POST['distance'];
$sql="INSERT INTO sportevent_parameters.Distance(value, user_ref, event) VALUES ('$dist', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['tshirt'])) {
$tshrt = $_POST['tshirt'];
$sql="INSERT INTO sportevent_parameters.T_Shirt(value, user_ref, event) VALUES ('$tshrt', '$id', '$event') ON DUPLICATE KEY UPDATE";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['partnerid'])) {
$sql="INSERT INTO sportevent_parameters.Partner_ID(value, user_ref, event) VALUES ('$partnerid', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
if(isset($_POST['racecategory'])) {
$sql="INSERT INTO sportevent_parameters.Race_Category(value, user_ref, event) VALUES ('$category', '$id', '$event')";
}
mysql_query($sql, $con)
or die("MySQL Error: ".mysql_error());
What would cause it to insert multiple copies into the DB?
Firstly, please sanitise your user input before using it directly in a query!!! – also read this.
Secondly – the problem is that your calls to
mysql_query()should be inside theifstatements. With what you have done,mysql_query()is executed regardless, only the SQL statement itself is changed by theif. This means that if the firstifcondition is true, but all the others are false, the first statement will be executed 4 times.Change your code to: