0

I've been trying to learn PHP and have been given a simple task to help me.

I'm trying to get a user to complete a form which has their email address in it, then save it to a database.

Here's my code so far:

<html> 
    <body>

        <form action="postemail.php" method="post"> Email Address: <input type="text" name="emailaddress" /> <input type="submit" /> 

        </form>

    </body> 
</html>

<?php 
   $connection = mysql_connect("localhost","edwardHost","password"); 
     if (!$connection) {
        die('Could not connect: ' . mysql_error());
     }
    mysql_select_db("my_database", $connection);

    $sql="INSERT INTO Subscribers (EmailAddress) VALUES ('$_POST[emailaddress]')";

    if (!mysql_query($sql,$connection)) { 
      die('Error: ' . mysql_error()); 
    }

    mysql_close($connection); 
?>

Thanks in advance!

5
  • 5
    If you're starting out, avoid using the mysql_* function family as they're now deprecated. You'd be better to start with PDO or MySQLi. Commented Oct 21, 2014 at 9:48
  • what issue you face in this code? Commented Oct 21, 2014 at 9:48
  • Your query is wide open to SQL injection. Commented Oct 21, 2014 at 9:51
  • what is the error you are getting? Commented Oct 21, 2014 at 9:51
  • I'm getting this error: Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\yes.php on line 15 Commented Oct 21, 2014 at 9:59

4 Answers 4

1

Change your query to this

One more thing i forget last time you are missing single quete around $_POST[emailaddress]. In your query

$sql="INSERT INTO Subscribers (EmailAddress) VALUES ('".$_POST['emailaddress']."')";

Dont use mysl function as the are deprciated

Learn mysqli_ function or PDO Or both

Check this link for mysql identifier http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html

Sign up to request clarification or add additional context in comments.

Comments

1

Try this example using PDO in your postemail.php

define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'dbname');
define('DB_USER', 'root');
define('DB_PASS', 'password');

try {
    // create a new instance of a PDO connection
    $db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
    // if the connection fails, display an error message
    echo 'ERROR: ' . $e->getMessage();
}

if(isset($_POST['emailaddress']) && !empty($_POST['emailaddress'])) {

    $emailaddress = $_POST['emailaddress'];

    $sql = 'INSERT INTO Subscribers (EmailAddress) VALUES (:emailaddress )';

    $stmt = $db->prepare($sql);
    $stmt->bindValue('emailaddress ', $emailaddress);
    $stmt->execute();
}

Comments

0

After you have totaly filled in the form, it first needs to check if the submit button is clicked, then it has to send it to a database.

You also need to give you submit button a name=""

HTML code:

<html> 
    <body>
        <form action="postemail.php" method="post">
            Email Address: <input type="text" name="emailaddress" />
            <input type="submit" name="submit" value="add to database" /> 
        </form>
    </body> 
</html>

PHP code:

<?php 
    if(isset($_POST['submit'])){
        $connection = mysqli_connect("localhost","edwardHost","password","my_database"); 
        if (!$connection) {
           die('Could not connect: ' . mysql_error());
        }

        $email = $_POST['emailaddress'];            

        $sql = "INSERT INTO Subscribers (EmailAddress) VALUES ('$email')";

        if (!mysqli_query($connection,$sql)) { 
          die('Error: ' . mysql_error()); 
        }

        mysql_close($connection); 
    }
?>

Comments

-1
     <html> <body>
    <form action="postemail.php" method="post">  
    Email Address: <input type="text" name="emailaddress" />      
     <input type="submit" />
       </form>
      </body> </html>
  <?php $connection = mysql_connect("localhost","username","password");
     if (!$connection) { die('Could not connect: ' . mysql_error());   
   }
   mysql_select_db("my_database", $connection);
   $sql="INSERT INTO Subscribers (EmailAddress) VALUES ('$_POST[emailaddress]')";
   if (!mysql_query($sql,$connection)) { die('Error: ' . mysql_error()); }
   mysql_close($connection);
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.