dqhr76378 2014-10-21 09: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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog