We are developing an application for our college and in the fees module we need to check whether the student record is already present. If the student record does not exist we need to insert the record else we need to update the record.The code i have used is listed below:
if (isset($_POST['submit'])) {
for($i=0; $i<count($_POST['mca_1_autogen_id']); ++$i){
$sql=mysqli_query($conn,"UPDATE `fees_mca_1` set `Fees_paid`='{$_POST['Fp'][$i]}' where Register_number='{{$_POST['Regno'][$i]}'");
}
header("Location:mca1_fees_report.php");
if(mysqli_affected_rows($conn) == 0){
for($i=0; $i<count($_POST['mca_1_autogen_id']); ++$i){
$sql=mysqli_query($conn,"INSERT INTO `fees_mca_1`(`Register_number`,`Student_name`,`Total_fees`,`Fees_paid`) VALUES ('{$_POST['Regno'][$i]}','{$_POST['Sname'][$i]}','{$_POST['Fees'][$i]}','{$_POST['Fp'][$i]}')") or die("Error: ".mysqli_error($dbc));
}}
header("Location:mca1_fees_report.php");
echo "Record Added Sucessfully";
}
I have also tried this but no avail
if (isset($_POST['submit'])) {
for($i=0; $i<count($_POST['mca_1_autogen_id']); ++$i){
$sql=mysqli_query($conn,"INSERT INTO `fees_mca_1`(`Register_number`,`Student_name`,`Total_fees`,`Fees_paid`) VALUES ('{$_POST['Regno'][$i]}','{$_POST['Sname'][$i]}','{$_POST['Fees'][$i]}','{$_POST['Fp'][$i]}') ON DUPLICATE KEY UPDATE `Fees_paid` = VALUES({$_POST['Fp'][$i]})");
}
header("Location:mca1_fees_report.php");
echo "Record Added Sucessfully";
}
ON DUPLICATE KEYin mysql?