简体   繁体   English

表单数据不会插入数据库

[英]Form Data will not insert to database

I have connected my website to my database successfully when i submit the form it goes through but nothing is getting inserted into the database. 提交表单时,我已经成功将网站连接到数据库,但是没有任何内容插入数据库。

Code Below: 下面的代码:

    <?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 = database buycruisesdb =数据库
website_subscribers = table inside the database website_subscribers =数据库内的表
name_sub/email_sub = columns inside the table name_sub / email_sub =表内的列

the form html is below: 表单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>

Not sure exactly why this is not inputing anyone have an idea? 不确定为什么不输入任何人有想法吗?

it says that it is inserting the proper values and into the proper tables Image 它说,它是将合适的值,进入正确的表图片

Square brackets are not valid in MySQL queries. 方括号在MySQL查询中无效。 You should be using quotes around the strings. 您应该在字符串周围使用引号。

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

change it to: 更改为:

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

just copy the code and try it out 只需复制代码并尝试一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM