• 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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hmm not sure you should be posting your url, and database username and password on an open forum – bottom of first bit of code, suggest you edit it out asap!

    Thread Starter Larsmir

    (@larsmir)

    Edited.
    Thank you ^^

    I realize now that a copypasting mistake also caused some bit of redundant code to appear in what I posted.

    No probs !

    Sorry this is not my area of expertise, but thought I should jump in before someone started hacking you.

    Actually my $con has four elements

    $con=mysqli_connect($host,$user,$password,$database);

    Looks like you missing the database name !

    But have seen that you do this later, I’d try doing it all in one,

    This code would also check connection to database I think

    $con=mysqli_connect($host,$user,$password,$database);
     //Check connection
     if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }

    I then have as an example

    $update= 'UPDATE wp_posts SET post_content =\''.$post2.'\' WHERE ID='.$id2 ;
    mysqli_query($con,$update)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using a PHP form to add emails to a database’ is closed to new replies.