is there a way i can stop a row of data being entered into the database while i’m importing from a csv file, so as to avoid duplication in the database..
NOTE: while the duplictates are not allowed entry othere are entered into to the database.
Here’s the importing script i’m working on….
<?php session_start(); ?>
<?php require('includes/dbconnect.php'); ?>
<?php require 'includes/header.inc.php'; ?>
<?php
if(isset($_POST['SUBMIT']))
{
$fname = $_FILES['csv_file']['name'];//Acquire the name of the file
$chk_ext = explode(".",$fname);
$filename = $_FILES['csv_file']['tmp_name'];
$handle = fopen($filename, "r");//Open the file for readability
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into biodata (student_number, fname, lname, level) values('$data[0]','$data [1]','$data[2]','$data[3])";
mysql_query($sql) or die (mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
?>
If the student_number column is the primary key, this script won’t let you insert rows with the same number and will die as soon as you find one.
If that is indeed the case, you can just change
INSERT INTO table VALUES (...)toINSERT INTO table VALUES (...) ON DUPLICATE KEY UPDATE student_number = student_numberto just ignore the error when that happens.Reference: http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html