mysqli插入不起作用,没有错误消息,连接成功

时间:2012-10-30 02:56:41

标签: php mysql mysqli connect

我不喜欢编码。我建立了一个名为contacts的MYSQL数据库,一个名为contactstable的表,包含字段id,firstname,lastname,emailaddress,postalcode和phonenumber。除了id之外,每个都是text或varchars,它是一个auto_increment,pk字段。连接不会产生任何错误,并且不会通过mysqli_connect_error()方法中继错误。他们的查询没有经过,也没有执行任何查询。我无法弄清楚原因。

<html>
<head>
    <title>Registration</title>     
</head>
<body>

      <h1>Register with Us!</h1>

      <h2>Registration Complete!</h2>
      <div class="feedback-container" <?= isset($_REQUEST["first-name"])? "style=\"display:block\"": "style=\"display:none\""; ?>>
         <?php

          $firstname = $lastname = $emailaddress = $postalcode = $phonenumber = NULL;

          if (isset($_REQUEST["first-name"])){
           $firstname = $_REQUEST["first-name"];
           $lastname = $_REQUEST["last-name"];
           $emailaddress = $_REQUEST["email-address"];
           $postalcode = $_REQUEST["postal-code"];
           $phonenumber = $_REQUEST["phone-number"];

           $dbconn = new mysqli();
           $dbconn->connect("localhost","root","","contacts");

           if(mysqli_connect_error()){
             echo "Connection Failed";
           }else{
             echo "Connection Established";
           }   

           $query = "INSERT INTO 'contactstable' ('firstname', 'lastname', 'emailaddress','postalcode','phonenumber') VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')";             

           if ($dbconn->query($query) == TRUE){
              echo ("Thank you for registering with us. We will shortly send a confirmation email to $emailaddress.");
           }else{
              echo ("<p>Your contact information was not added to our database. Please try again later or contact our webadmin at webadmin@gmail.com</p>");
           }

          }

         ?>
      </div>
</body>

运行时,输出以下内容: “连接已建立” “您的联系信息未添加到我们的数据库中。请稍后再试或通过webadmin@gmail.com与我们的webadmin联系” 没有错误消息。 没有数据更新。

3 个答案:

答案 0 :(得分:1)

我会做这个

  $query = "INSERT INTO 'contactstable' ('firstname', 'lastname', 'emailaddress','postalcode','phonenumber') VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')"; 

看起来像这样

  $query = "INSERT INTO contactstable (firstname, lastname, emailaddress,postalcode,phonenumber) VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')"; 

答案 1 :(得分:0)

查询应该如下所示。

    INSERT INTO `contactstable`
 (`firstname`, `lastname`, `emailaddress`,`postalcode`,`phonenumber`)
 VALUES
 ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')

答案 2 :(得分:0)

如果要引用字段名称,请使用反引号。

$query = "INSERT INTO `contactstable` (`firstname`, `lastname`, `emailaddress`,`postalcode`,`phonenumber`) VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')";

如果您设置"

,也可以使用SET sql_mode='ANSI_QUOTES'

http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html