Mailing List ID Issue

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
andy1212
Forum Commoner
Posts: 26
Joined: Sat Jan 22, 2011 2:30 pm

Mailing List ID Issue

Post by andy1212 »

I created my own method of having someone enter in their email into a "mailing list" because tutorials I've followed to create a mailing list with php never worked for me. The code I created works with just the one row for emails in the db but when the email is entered into the database I want an ID associated with it. I tried adding a new row to my table in the database called ID with is an INT and autoincrement and primary key and then when I test the mailing list by entering an email, the email doesn't get registered into the database. When I delete the ID row and try entering an email, it works.. I'm not sure where I'm going wrong and I provided my code below so someone can help me with this. Also is there a way to echo all the emails from the database table? Thanks in advance.

connect2.php

Code: Select all

<?PHP

$error = "Problem Connecting to the database.";

$connect = mysql_connect("localhost", "mydbusername", "mydbpassword") or die($error);
mysql_select_db("db_name") or die($error);

?>
mailinglist.php

Code: Select all

<?PHP
			require 'connect2.php';

			$submit = $_POST['submit'];

			// form data

			$email = addslashes(strip_tags($_POST['email']));

			if ($submit)
			{
  				// open database
  				include 'connect2.php';
  
  				$emailcheck = mysql_query("SELECT email FROM mailinglist WHERE email='$email'");
  				$count = mysql_num_rows($emailcheck);
  
  				if ($email)
				{
  					if ($count!=0)
  					{
   						echo "<div id='mailinglistmsg' class='mailingText4'>You're already subscribed.</div>";  
  					}
					else
					{
  							// register user
  							$queryreg = mysql_query("INSERT INTO mailinglist VALUES ('$email')");
							echo "<div id='mailinglistmsg' class='mailingText4'>You have been added to our mailing list</div>";
					}
				}
				else
					echo "<div id='mailinglistmsg' class='mailingText4'>Please enter your email address.</div>";
					
			}

?>
form on menu.php page

Code: Select all

<form action='menu.php' method='POST'>
<span class="mailingText5">Join our mailing list: </span><input name="email" id="email" type="text" size="20" maxlength="40" class="mailingText2" title="Join our Mailing List" />
<input name="submit" type="submit" class="mailingText3" id="submit" value="GO" />
</form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Mailing List ID Issue

Post by Celauran »

Code: Select all

$queryreg = mysql_query("INSERT INTO mailinglist (email) VALUES ('{$email}')");
Also is there a way to echo all the emails from the database table?

Code: Select all

$query = "SELECT email FROM mailinglist";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
    echo "{$row['email']}<br />";
}
Post Reply