Jump to content

Recommended Posts

I have an email form that can have comma separated email addresses. I need to modify my code such that initially if the form does not contain any value, it should let me add email addresses to it. When I enter the web page next time it should display and let me edit the existing email addresses. Here is my code below. Right now it does not insert anything the first time.

<?
error_reporting(E_ALL & ~E_NOTICE);
$conn = mysql_connect('localhost','test','*****') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('test',$conn) or trigger_error("SQL", E_USER_ERROR);
//$sql=mysql_query("SELECT * from new_database") or die(mysql_error());
$getemail=mysql_query("SELECT * from email") or die(mysql_error());
if(mysql_num_rows($getemail) > 0)
{
   while($getemail_results=mysql_fetch_assoc($getemail))
   {
      $getemailadd=$getemail_results['email'];
   }
}
else
{
  $getemailadd='';
}
   if (!isset($_POST['submit']))
   {
   ?>
   <b>ADDING A Email</b><br>
   <form action="<?php echo $PHP_SELF;?>" method="post">
   Email:<br> <input type="text" size ="80" name="email" value="<?PHP if(isset($getemailadd)){ echo $getemailadd; } ?>" /><br>
   <input type="submit" name="submit" value="submit" />
   </form>
   <?
   }
   else
  {

    // Get values from form
    $email=$_POST['email'];
    //$sql=mysql_query("INSERT into email(email)VALUES('$email')") or die (mysql_error());
    $sql=mysql_query("UPDATE email SET email='$email'") or die (mysql_error());
    header('Location: index.php');

}
?>

Link to comment
https://forums.phpfreaks.com/topic/247474-addingediting-same-form/
Share on other sites

your code searches a database and shows a form. If the database has a value, it shows in the form, otherwise the form is blank. when you add a new value, you're doing the same process all over again and adding the new email to the database at the end of the file... that's why it does not show up when you POST a new address.

 

Move the part where you add the new email to the database, to the beginning of the file instead og the end. That way when your code searched the database for existing emails, the one you just inserted will already be there.

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.