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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.