hi please see my code inserting first two records into database twice
<?php
// connect to DB
$host_name = "localhost";
$db_user = "root";
$db_password = "sachi";
$db_name = "fundgroup";
$table = 'ecoup_coupon_subscribers4';
switch ($_POST['act']) {
case 'insert':
$db = mysql_connect("$host_name","$db_user","$db_password") or die("Could not connect to database!");
$result = mysql_select_db($db_name) or die("Could not select database!");
// open file
$fp = fopen($_FILES['userfile']['tmp_name'], 'r') or die('Could not open file!');
while ($data = fgetcsv($fp, 1024))
{
$x=0;
$sql_ = "SELECT * FROM $table ";
$result_=mysql_query($sql_, $db) or die("This selected file Could not contain required fields! Please Select the file again");
$id_=mysql_fetch_row($result_);
if($id_[0]!="") {
// update record in case id was already existed/
$file_content = file($_FILES['userfile']['tmp_name']);
for($i=1; $i<count($file_content); $i++){
//echo $file_content[$i].'<br/>';
$varia= explode(",", $file_content[$i]);
if($varia[1]!="" && $varia[3]!="" && $varia[60]!=""){
$update = "UPDATE $table SET FirstName='$varia[0]', LastName='$varia[1]',BusinessStreet='$varia[2]',BusinessStreet2='$varia[3]',BusinessStreet3='$varia[4]',BusinessCity='$varia[5]',BusinessState='$varia[6]',BusinessPostalCode='$varia[7]',BusinessPhone='$varia[8]',EmailAddress='$varia[9]' WHERE FirstName='$varia[0]' ";
$res_update = mysql_query($update, $db) or die("Could not update DB!");
echo "Row #".$num." was updated!<p>";
$num++;
}
}
}
else
{
$file_content = file($_FILES['userfile']['tmp_name']);
for($i=1; $i<count($file_content); $i++){
# echo $file_content[$i].'<br/>';
//print_r($file_content);
//die;
$varia= explode(",", $file_content[$i]);
if($varia[1]!="" && $varia[3]!="" && $varia[58]!=""){
// insert new record into database
$insert = "INSERT INTO $table ( FirstName, LastName,BusinessStreet,BusinessStreet2,BusinessStreet3,BusinessCity,BusinessState,BusinessPostalCode,BusinessPhone,EmailAddress)
VALUES('".$varia[1]."','".$varia[3]."','".$varia[11]."','".$varia[12]."','".$varia[13]."','".$varia[14]."','".$varia[15]."','".$varia[16]."','".$varia[34]."','".$varia[58]."')";
$res_insert = mysql_query($insert, $db) or die("Could not INSERT query!");}
}
echo "Row #".$num." was inserted!<p>";
$i++;
if($x>0){
# $x++;
}
}
}
fclose($fp);
break;
default:
echo <<<EOD
<center>
<h3>CSV file upload form</h3>
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="act" value="insert">
CSV file <input name="userfile" type="file">
<input type="submit" value="Submit">
</form>
</center>
EOD;
break;
}
function validate_CSV( $fileName ) {
$valid = TRUE;
$f = file( $fileName ) or die( "Unable to get file information." );
foreach( $f as $lineNum => $lineContents ) {
if( !preg_match( "/(.*)(,)(.*)/iu", $lineContents ) ) {
echo "Line $lineNum has an error: ($lineContents)<br />";
$valid = FALSE;
}
}
return $valid;
}
?>
my csv file is as follows
Title First Name Middle Name Last Name Suffix Company Department Job Title Business Street Business Street 2 Business Street 3 Business City Business State Business Postal Code Business Country Home Street Home Street 2 Home Street 3 Home City Home State Home Postal Code Home Country Other Street Other Street 2 Other Street 3 Other City Other State Other Postal Code Other Country Assistant's Phone Business Fax Business Phone Business Phone 2 Callback Car Phone Company Main Phone Home Fax Home Phone Home Phone 2 ISDN Mobile Phone Other Fax Other Phone Pager Primary Phone Radio Phone TTY/TDD Phone Telex Account Anniversary Assistant's Name Billing Information Birthday Business Address PO Box Categories Children Directory Server E-mail Address E-mail Type E-mail Display Name E-mail 2 Address E-mail 2 Type E-mail 2 Display Name E-mail 3 Address E-mail 3 Type E-mail 3 Display Name Gender Government ID Number Hobby Home Address PO Box Initials Internet Free Busy Keywords Language Location Manager's Name Mileage Notes Office Location Organizational ID Number Other Address PO Box Priority Private Profession Referred By Sensitivity Spouse User 1 User 2 User 3 User 4 Web Page
Mr. Raja Gopal K Sachi Infotech Pvt. Ltd., Project Manager Sachi Infotech Pvt. Ltd. Hyderabad Andhra Pradesh 500082 India 501 Leacroft Way Morrisville NC 27560 USA 1-336-793-4188 919-647-9029 +91 98-6677-1424 0/0/00 5/5/2006 t_saigopal@yahoo.com Unspecified R.G.K. Low Normal
Saicharan. M 0/0/00 0/0/00 saicharan.m@gmail.com Unspecified S.M. High FALSE Confidential
Ranga Raj Sachi Infotech Pvt. Ltd., Designer 0/0/00 0/0/00 rangs.des@yahoo.com Unspecified R.R. High FALSE Confidential
Ajay K Sachi Infotech Pvt. Ltd. Hyderabad Andhra Pradesh 500082 1-336-793-4188 ajay.pro@yahoo.com
Jagadeesh P Sachi Infotech Pvt. Ltd PHP Programmer Sachi Infotech Pvt. Ltd. Hyderabad Andhra Pradesh 500082 1-336-793-4188 jagadeesh.pro@yahoo.com
anybody please help me