Jump to content

Updating database


Canman2005

Recommended Posts

Hi all

 

I have a script which uploads a CSV file and then updates any information contained in a SQL database, so if there is a new address for a user in the CSV file, then it updates the SQL database with the new information.

 

The code I use is

 

		if (isset($_FILES['documentfile']['size']) && $_FILES['documentfile']['size'] > 100)
	{
		$upfile = ''.$random."_".$_FILES['documentfile']['name'];

		if (!copy($_FILES['documentfile']['tmp_name'], $upfile))
		{
			print "Could not move file into upload directory";
			exit;
		}


$link = mysql_connect("localhost","***","***");
$selection = mysql_select_db("md_update", $link);

$filename = $upfile;

$id = fopen($filename, "r");
while ($data = fgetcsv($id, filesize($filename)))
$table[] = $data;
fclose($id);

foreach($table as $row)
{

$sql ="UPDATE md_update SET id='".$row['0']."',name='".$row['1']."',member_no='".$row

['2']."',tradingname='".$row['3']."',street='".$row['4']."',town='".$row['5']."',county='".$row

['6']."',post_code='".$row['7']."',phone='".$row['8']."',board_area='".$row['9']."',username='".$row

['10']."',password='".$row['11']."',validity='".$row['12']."',type_desc='".$row['13']."' WHERE id='".$row['0']."'";
@mysql_query($sql, $connection) or die(mysql_error());

}


unlink($upfile);			

}

}

 

Where abouts would I put an "else" statement? So if there is any new data in the CSV file, then it will "INSERT" the new data.

 

Any help would be great

 

Thanks in advance

 

Ed

Link to comment
https://forums.phpfreaks.com/topic/51982-updating-database/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.