dqhr76378 2014-10-21 01:45
浏览 87
已采纳

将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 2014-10-21 02:10
    关注

    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); 
        }
    ?>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部