表单数据不会插入数据库

时间:2016-04-22 20:00:00

标签: php html mysql

当我提交表单时,我已成功将我的网站连接到我的数据库,但没有任何内容插入到数据库中。

以下代码:

    <?php
if( $_POST )
{
  $con = mysql_connect("server","user","pass");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("buycruisesdb", $con);

  $users_name = $_POST['name'];
  $users_email = $_POST['email'];

  $users_name = mysql_real_escape_string($users_name);
  $users_email = mysql_real_escape_string($users_email);

  $query = "
  INSERT INTO `website_subscribers`(`name_sub`, `email_sub`) VALUES ([$users_name],[$users_email])";

  mysql_query($query);

  echo "<h2>Thank you for subscribing!</h2>";
  echo $query;
  echo $users_name;
  echo $users_email;

  mysql_close($con);
}
?>

buycruisesdb =数据库
website_subscribers =数据库内的表
name_sub / email_sub =表格内的列

表单html如下:

!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    <form action="php/subscriber.php" id="form" method="post" name="form">
       <input id="name_sub" name="name" placeholder="Name" type="text">
       <input id="email_sub" name="email" placeholder="Email" type="text">
       <input type="submit" value="Submit" name="f_submit">
    </form>
</body>
</html>

不确定为什么这不输入任何人有想法?

它说它正在插入适当的值并进入适当的表 Image

2 个答案:

答案 0 :(得分:1)

方括号在MySQL查询中无效。你应该在字符串周围使用引号。

$query = "INSERT INTO `website_subscribers` (`name_sub`, `email_sub`) VALUES ('$users_name', '$users_email')";

答案 1 :(得分:1)

将其更改为:

&#13;
&#13;
$query = "
  INSERT INTO website_subscribers (name_sub,email_sub) VALUES ('".$users_name."','".$users_email."') ";
&#13;
&#13;
&#13;

只需复制代码并试用即可