使用HTML表单和PHP在MySQL数据库中存储电子邮件

时间:2012-11-02 23:55:00

标签: php html mysql forms

我正在制作一个表单,我可以收集姓名和电子邮件地址,并将它们发送到MySQL数据库进行存储。我写了HTML和PHP文件,似乎工作正常。它回应了“感谢您输入电子邮件!”。当我查看数据库时,它会为数据创建一行,但所有字段都是空白的。我不确定发生了什么,这是我第一次使用数据库。谢谢你的帮助!

HTML

<form action='/submitEmail.php' action='POST'>
  <p>First name: <input type='text' id="firstname" name='firstname' /></p>
  <p>Last name: <input type='text' id="lastname" name='lastname' /></p>
  <p>Email: <input type='text' name='email' /></p>
  <input type='submit' value='Submit Email' />
</form>

PHP

<?php
  // Connecting to the MySQL server
  $host="myHost";
  $user_name="myUsername";
  $pwd="myPassword";
  $database_name="myDatabase"; //assuming you created this
  $db=mysql_connect($host, $user_name, $pwd);
  if (mysql_error() > "") print mysql_error() . "<br>";
  mysql_select_db($database_name, $db);
  if (mysql_error() > "") print mysql_error() . "<br>";
  // Storing form values into PHP variables
  $firstname = $_POST["firstname"]; // Since method="post" in the form
  $lastname = $_POST["lastname"];
  $email = $_POST["email"];
  // Inserting these values into the MySQL table
  // we created above
  $query = "insert into email_list (firstname, lastname, email) values ('" . $firstname . "', '" . $lastname . "', '" . $email . "')";
  $result = mysql_query($query);
  // mysql_query() is a PHP function for executing
  // MySQL queries
  echo "<p>Thank you for entering an email!</p>";
?>

2 个答案:

答案 0 :(得分:5)

属性method应为post

<form action='/submitEmail.php' method='POST'>

答案 1 :(得分:0)

你写了action='POST'而不是method='POST'。尝试稍后使用。它应该工作。

快乐的编码!!