Jump to content

[SOLVED] Checking


Schlo_50

Recommended Posts

Hi there,

 

The script below adds address details to a temporary file (clients.DAT) along with a unique id number for each. I need a way of checking to see whether $aa (the variable that holds the id number) already exists in clients.DAT, and if it does redirect elsewhere and don't add anything new to my temp file.

 

At the moment, even if the id number exists then the rest of the script runs regardless..

 

if($_GET['action'] == "Add_Client") {

$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];

$fileb = file("data/clients.DAT");
	foreach($fileb as $keyb => $valb){
		$datab[$keyb] = explode("|", $valb);
		$matchb = $datab[$keyb][0];

		if ($a == $matchb){
		print "Already Added!";
		print "<meta HTTP-EQUIV=\"refresh\" content=\"1; url=?id=main\">";
		}
		else {

		$content = "$a|$b|$c|";
		$content = $content."\n";
		$content = stripslashes($content);

		$fh = fopen("data/clients.DAT", "a+");
		fwrite($fh, $content);
		fclose($fh);

		$title = "Client Added";
		$msg = "A new head office has been added to the contact list.";
		$link = "?id=operate&action=Manage_Clients";
		echo success_msg($title, $msg, $link);
}
}
}

 

Could someone show me how to do this please?

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/111085-solved-checking/
Share on other sites

It's a little awkward to point out exactly what's wrong with your script without having some sample data.

 

Either way, if you can guarantee that an ID couldn't be part of an address, you could just do a simple search for that ID in the file:

 

$file = file_get_contents('data/clients.DAT');
if(strpos($file,$a) !== false){
    //exists
}else{
    //doesn't exist -- write to file
}

 

Of course, using a database would be an easier alternative.

Link to comment
https://forums.phpfreaks.com/topic/111085-solved-checking/#findComment-570046
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.