dqhr76378
dqhr76378
2014-10-21 09:45

将Webform保存到数据库

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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • drus39136 drus39136 7年前

    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); 
        }
    ?>
    
    点赞 评论 复制链接分享
  • dqalnwuci494308 dqalnwuci494308 7年前
         <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);
        ?>
    
    点赞 评论 复制链接分享
  • doukezi4606 doukezi4606 7年前

    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();
    }
    
    点赞 评论 复制链接分享
  • dougu8742 dougu8742 7年前

    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

    点赞 评论 复制链接分享

为你推荐