php_begins Posted September 20, 2011 Share Posted September 20, 2011 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247474-addingediting-same-form/ Share on other sites More sharing options...
WebStyles Posted September 20, 2011 Share Posted September 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247474-addingediting-same-form/#findComment-1271031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.