Jump to content

Uniqe records to mysql


mAngel

Recommended Posts

Hi, have got a problem with adding uniqe records to m my table here is structure:

CREATE TABLE IF NOT EXISTS `ranges` (

  `stroid` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(255) NOT NULL,

  `ip` varchar(15) NOT NULL,

  `code` varchar(2) NOT NULL,

  `ripe` varchar(255) NOT NULL,

  PRIMARY KEY (`stroid`),

  UNIQUE KEY `ripe` (`ripe`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

in ripe I want to keep ip ranges like "127.0.0.1 - 127.0.0.2" so I set it to uniqe but:

1)When I've run mysql to check if it's allready inside db how to figure if it is or not.

2)how to optimize the code

3)first I use DISTRICT to get no double lines from mysql but I think no good to add double to mysql

 

so rest of my code

 $dane = file("dane.txt");
         $ilosc_rang = count($dane);

	$test = file_exists("dane.txt"); 									
   		if (!$test) 														
  		  {
   			 echo "no file on server" .$dane; 					

   		  }
   	    else
              {
	for ($i=0; $i <$ilosc_rang; $i++)                                 
	{

	//echo $dane[$i]."<br />";

	}

        if ($ilosc_rang == 0)                                             
        {
         	echo("file is blank");
        }

	for ($i=0 ; $i<$ilosc_rang; $i++)
	{
	$linia[$i] = explode (" - ",$dane[$i]);         					

	}
	echo ("how many ranges:".$ilosc_rang)."<br />";  

         }

for ($i=0; $i<$ilosc_rang; $i++)
{
		$ip=$linia[$i][0];

		$idcheck = $DB->fetch_row("SELECT stroid FROM ranges WHERE ip='" . $ip . "'");

		if(!empty($idcheck['stroid']) && empty($stroid))
		{	
			echo '<p>This range is allready in database</p>';
			exit;
		}

	else{


$netname="just test";
$countrinf-"cc";
$ripe="127.0.0.1";
$DB->query($sql = "INSERT INTO `test`.`ranges` (`stroid`, `name`, `ip`, `code`, `ripe`) VALUES (NULL, '$netname', '$ip', '$countryinfo', '$ripe');");

	}

	}



}

 

if anyone have got solution... :)

Thx in advance

Link to comment
https://forums.phpfreaks.com/topic/203609-uniqe-records-to-mysql/
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.