Jump to content

csv file validation


deepson2

Recommended Posts

Hello,

 

I want to do csv file validation, like i dont want insert blank field. and others validations as well.

 

but now, the status is with blank field also data is getting inserted. i have tried a lot. can anyone have look and please tell me how can i prevent my database from blank fields

 

here is my code

 

if(isset($_POST['Submit'])=="Upload")
  {
$handle = fopen("c:../../files/abbr_details.csv","r");



while ($data = fgetcsv ($handle, 1000, ","))
{

	if ((empty($abbreviation)) or (empty($full_name)) or (empty($significance)) or (empty($used))){
			 	 	 		echo "You forgot to enter your some one of the value, please go back and try again";
			 	 	 		exit();
			 		     }
			 		     else{


	   $query = mysql_query("SELECT * FROM abbr_details WHERE (full_name='$full_name' AND abbreviation='$abbreviation' AND significance='$significance' AND used='$used')") or die(mysql_error());

	   if(mysql_num_rows($query) >= 1)
		  {

		 }

		 else
		   {
			   $query1 = "INSERT INTO abbr_details (abbreviation,full_name,significance ,used) values ('".$abbreviation."','".$full_name."','".$significance ."','".$used."')";
			   $result1 = mysql_query($query1) or die('Query failed: ' . mysql_error());

			   if(mysql_affected_rows() > 0)
			   {
					echo "inserted";
			   }
			  else
			  {
					echo "not inserted";
			  }
		   }

 }}

 fclose ($handle);
 mysql_free_result($query);


}
?>

 

please help me.

 

thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/162895-csv-file-validation/
Share on other sites

I am so sorry priti. here is my proper code. and insertion is pretty much happening. the thing is only validation. please help me.

 

if(isset($_POST['Submit'])=="Upload")
  {
$handle = fopen("c:/abbr_details.csv","r");

while ($data = fgetcsv ($handle, 1000, ","))
{
	$abbreviation  =  $data[0];
	$full_name  =  $data[1];
	$significance =  $data[2];
	$used  =  $data[3];

	   $query = mysql_query("SELECT * FROM abbr_details WHERE (full_name='$full_name' AND abbreviation='$abbreviation' AND significance='$significance' AND used='$used')") or die(mysql_error());

	   if(mysql_num_rows($query) >= 1)
		  {

		 }

		 else
		   {
			   $query1 = "INSERT INTO abbr_details (abbreviation,full_name,significance ,used) values ('".$abbreviation."','".$full_name."','".$significance ."','".$used."')";
			   $result1 = mysql_query($query1) or die('Query failed: ' . mysql_error());

			   if(mysql_affected_rows() > 0)
			   {
					echo "inserted";
			   }
			  else
			  {
					echo "not inserted";
			  }
		   }

 }

 fclose ($handle);
 mysql_free_result($query);


}
?>

Link to comment
https://forums.phpfreaks.com/topic/162895-csv-file-validation/#findComment-859513
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.