WordPress.org

Ready to get started?Download WordPress

Forums

Using a PHP form to add emails to a database (7 posts)

  1. Larsmir
    Member
    Posted 8 months ago #

    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.

  2. Robin W
    Member
    Posted 8 months ago #

    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!

  3. Larsmir
    Member
    Posted 8 months ago #

    Edited.
    Thank you ^^

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

  4. Robin W
    Member
    Posted 8 months ago #

    No probs !

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

  5. Robin W
    Member
    Posted 8 months ago #

    Actually my $con has four elements

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

    Looks like you missing the database name !

  6. Robin W
    Member
    Posted 8 months ago #

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

  7. Robin W
    Member
    Posted 8 months ago #

    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)

Reply

You must log in to post.

About this Topic