Inserting Data into a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Inserting Data into a file

Post by Maluendaster »

I have this code

Code: Select all

<?php
if(!isset($_POST['email'])){
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="email">Email
<input type="submit" name="submit">
<?php
}
else{
// Fila a Escribir
$file = 'email.db';
// Abrir Fila
$fh = fopen($file, 'a') or die('No se pudo abrir la fila!');
// Escribir en Fila
fwrite($fh, $_POST['email']) or die('No se pudo escribir en la fila');
// Cerrar Fila
fclose($fh);
}
@header("location:http://www.metallica.com");
exit;
?>
It works fine, but what I want is to write to the file email.db like this
email@1.com
emial@2.com
etc..

and it writes like this:
email@1.comemial@2.cometc
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Add a newline to the output:

Code: Select all

fwrite($fh, $_POST['email'] . "\n") or die('No se pudo escribir en la fila');
(#10850)
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

arborint wrote:Add a newline to the output:

Code: Select all

fwrite($fh, $_POST['email'] . "\n") or die('No se pudo escribir en la fila');
thanks, it worked... I have another question, i want to tell the user something if he/she didn't write anything in the form... how do i do that?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Normally by using something like

Code: Select all

if(!isset($_POST['email'])){
, but you're already doing that.

May I suggest

Code: Select all

if(!$_POST['submitted']) {
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="email">Email
<input type="hidden" name="submitted" value="1">
<input type="submit" name="submit">
<?php
} else if ($_POST['submitted'] && !$_POST['email']) {
?>Enter your email!<?php
} else {

//the rest of the code

}
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

ok, i have this now, and aparently is working fine... the next step is to check in email.db if the email is already in the file or not, if it is, i want to print something like "Your email is already in the DB", and if it's not, i want to write it on the file..

here's the code...

Code: Select all

<?php
$email= "$_POST[email]\n";
$redireccion ="http://www.google.cl";
if(empty($_POST['email'])){
echo "No has ingresado ningun correo, haz <a href=javascript:history.back()>click aqui</a> e intentalo nuevamente.";
exit;
}
else{

// Fila a Escribir
$file = 'email.db';
// Abrir Fila
$fh = fopen($file, 'a') or die('No se pudo abrir la fila!');
// Escribir en Fila
fwrite($fh, $email) or die('No se pudo escribir en la fila');
// Cerrar Fila
fclose($fh);
}
@header("location:$redireccion");
exit;
?>
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

Grim... wrote:Normally by using something like

Code: Select all

if(!isset($_POST['email'])){
, but you're already doing that.

May I suggest

Code: Select all

if(!$_POST['submitted']) {
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="email">Email
<input type="hidden" name="submitted" value="1">
<input type="submit" name="submit">
<?php
} else if ($_POST['submitted'] && !$_POST['email']) {
?>Enter your email!<?php
} else {

//the rest of the code

}
thank you, i didnt ignore you, i was writting the previous post at the time you were writting it too...
Post Reply