User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,526 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,807 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2100 | Replies: 2
Reply
Join Date: Sep 2007
Posts: 6
Reputation: becraig is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
becraig becraig is offline Offline
Newbie Poster

Question PHP query CSV

  #1  
Sep 29th, 2007
I have an issue I want to have a php file set on my server that appends to a csv file and also reads and queries that csv file for a column.

eg:

I want a csv file with the following columns
Email, First, name

I want the php to query the csv and evaluate if form.email is the same as any email in the csv file then throw and error.

If the email is not in the list and is a valid email then go ahead and append a line to the csv file with the required columns.

I am a php novice so if anyone can help me I appreciate it.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: PHP query CSV

  #2  
Sep 29th, 2007
i have a script i just made for you but i have to ask if you need me to add email validation to it. also, does it need to have input fields for the first name column?
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: PHP query CSV

  #3  
Sep 29th, 2007
here it is:

<form action="csv.php" method="post">
<p>Email:<input type="text" name="email" /></p>
<p>Name:<input type="text" name="name" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
function check($fieldname)
{
if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\@\ ]+$/s",$fieldname)) {
return TRUE;
}
else {
return FALSE;
}
}
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$error = 0;
if ($email == NULL) {
$error_msg .= "Email field is not filled out\\n\\n";
$error++;
}
if (!$email == NULL && !check($email)) {
$error_msg .= "Email field contains illegal characters\\n\\n";
$error++;
}
if ($name == NULL) {
$error_msg .= "Name field is not filled out\\n\\n";
$error++;
}
if (!$name == NULL && !check($name)) {
$error_msg .= "Name field contains illegal characters\\n\\n";
$error++;
}
if ($error > 0) {
$error_msg .= "There currently are " . $error . " errors";
echo '<script type="text/javascript">alert ("' . $error_msg . '");</script>';
}
if ($error == 0) {
$row = 1;
$open = fopen("file.csv", "r");
while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
if ($data[0] == $email) {
die('email already exists');
}
}
fclose($open);
$csv = $email . ", " . $name . "\n";
$open = fopen("file.csv", "a");
$write = fwrite($open, $csv);
fclose($open);
}
}
?>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC