How to avoid inserting duplicate data by inserting through array

I want to insert record though array…but i don’t want to insert duplicate record.

<?php mysql_connect("localhost","root",""); mysql_select_db('mumbai_darshan'); $getser_date=mysql_query("select * from bus_service where CAST(`effective_from` AS DATE)=CURDATE() and Cycle_days=2"); $arr=array(); while($row1=mysql_fetch_array($getser_date)) { array_push($arr,$row1); } $firstsunday=strtotime("first Sunday"); for($c=0;$c<=count($arr);$c++) { $tempno= $arr[$c]['bus_service_no']; $chbus=mysql_query("select * from bus_departure where bus_service_no =$tempno and bus_depart_time i='0000-00-00 00:00:00'"); if(mysql_num_rows($chbus)<0) { $sql="INSERT INTO bus_departure(bus_service_no,bus_depart_time) VALUES($tempno,'$sundate')"; $result=mysql_query($sql); echo "inserted"; } } ?>

First things first. Update your code to PDO. You can get a bump start on it here:

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

We can no longer support deprecated code.

Second. if you set up your database properly, it will be impossible to insert duplicate data. You need to setup a UNIQUE KEY on the columns you do not want duplicate data.

Third, your approach with arrays is wrong.

Once you update your code I will be happy to help you get going.

Never mind I was going to write something, but get the code straighten out first then tackle the problem (if there is one later). ::slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service