Using a PHP form to add emails to a database
-
Hello!
I’m working on a site (www.bavoice.com.ar). When you enter, you’ll notice a gray div with a text box. That box is meant for typing in an e-mail address, then clicking the black submit button, and having that address be added to a column named “email” in the table called “newsletter” in the database.
This is the code I’m working with (it’s all part of home.php)
<div id="agenda"> <form action="" method="post" id="subForm" class="clearfix"> <h2><a href="">Newsletter BAVoice</a></h2> <div> <label>Recibila en tu e-mail:</label> <input type="varchar(255)" name="email" id="formemail" /> <input type="submit" value="Suscribirme" /> <?php if (isset($_POST['submit'])) { include 'db.php'; $email=$_POST["email"] ; mysql_query("INSERT INTO <code>newsletter</code>(email) VALUES ('$email')"); <?php $conn = mysql_connect('DB IP', 'DB name', 'DB pass'); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("newsletter", $conn); ?> }
The db.php file has the following code (and yes, I’m sure the credentials are correct)
<?php $conn = mysql_connect('DB IP', 'DB name', 'DB password'); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("newsletter", $conn); ?>
What happens is that when you click submit, the home page reloads and the database is never updated. The “email” column is set as primary key in the table.
Is there a problem with the code?
Or is there something about WordPress I’m not quite grasping?Thanks for your help.
- The topic ‘Using a PHP form to add emails to a database’ is closed to new replies.