/    Sign up×
Community /Pin to ProfileBookmark

PHP: write to file if a string doesn’t exist there

Hi,

I have a PHP file that writes form inputs into a csv file. I redirect user to a thank-you.php page when they fill the form for the first time. If user tries to fill the form again, I want to redirect them to another page (already-there.php). I recognize users based on their emails. So if an email already exists in the csv file, do not write the entry into the file again and redirect the person to the ‘already-there.php’ page. But the problem is that it keeps going to the ‘thank-you.php page’. Here is my code. I appreciate it if any one could tell me where the problem is:

[CODE]
<?php
$keys = array(‘Name’,’Email’,’Phone’);
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,” . $_POST[$key]);
}

$fname = ‘form_inputs.csv’;
$csv_line = implode(‘,’,$csv_line);
if(!file_exists($fname)){$csv_line = “rn” . $csv_line;}
$fcon = fopen($fname,’a’);
$file_content_separated_by_colon = explode(“,”, $fname);
echo $file_content_separated_by_colon;
if( strpos($file_content_separated_by_colon, $_POST[‘Email’]) == false) {

fwrite($fcon,$csv_line . “n”);
fclose($fcon);
header(‘Location: thank-you.php’);
}
else {
header(‘Location: already-there.php’);
}

exit();
?>
[/CODE]

Tried it with 3 equal signs too like this: === false, no help.

Thanks in advance!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 09.2014 — I don't see anywhere where you actually read the contents of the file?
Copy linkTweet thisAlerts:
@Atrisa_MilaniauthorDec 09.2014 — Added that part too. Don't know if that's the right way to do it or not, but it's not working yet. Here is the code with the fix:

[CODE]<?php
$keys = array('Name','Email','Phone');
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_POST[$key]);
}

$fname = 'form_inputs.csv';
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "rn" . $csv_line;}
$fcon = fopen($fname,'a');
$getText = file_get_contents($fname, true);
$file_content_separated_by_colon = explode(",", $getText);
if( strpos($file_content_separated_by_colon, $_POST['Email']) == false) {

fwrite($fcon,$csv_line . "n");
fclose($fcon);
header('Location: thank-you.php');
}
else {
header('Location: already-there.php');
}

exit();
?>[/CODE]
Copy linkTweet thisAlerts:
@NogDogDec 09.2014 — I might do something like this (well, actually I'd probably use a database ? ):
[code=php]
<?php

$fname = 'form_inputs.csv';
$found = false;
$fh = fopen($fname, 'r');
while(($line = fgetcsv($fh)) != false)
{
if(isset($line[1]) and strtolower(trim($_POST['Email'])) == strtolower(trim($line[1])))
{
$found = true;
break;
}
}
fclose($fh);
if($found)
{
header('Location: already-there.php');
exit;
}
$fh = fopen($fname, 'a');
fputcsv($fh, array(
$_POST['Name'],
$_POST['Email'],
$_POST['Phone']
));
fclose($fh);
header('Location: thank-you.php');
[/code]

Warning: completely untested.
Copy linkTweet thisAlerts:
@Atrisa_MilaniauthorDec 10.2014 — Thanks a lot. With one else statement for the writing part, the code did the trick. You are right. Using database would have been a better choice, but the person for whom I made this app wanted the data printed to a csv file, which made my task harder ?
×

Success!

Help @Atrisa_Milani spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...