1

我不喜欢编码。我建立了一个名为contacts 的MYSQL 数据库,一个名为contacttable 的表,其中包含id、firstname、lastname、emailaddress、postalcode 和phonenumber 字段。每个都是文本或 varchars,除了 id,它是一个 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 联系我们的网络管理员” 没有错误消息。没有更新数据。

4

3 回答 3

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')"; 
于 2012-10-30T03:03:11.917 回答
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

于 2012-10-30T03:14:27.290 回答
0

查询应该是这样的。

    INSERT INTO `contactstable`
 (`firstname`, `lastname`, `emailaddress`,`postalcode`,`phonenumber`)
 VALUES
 ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')
于 2012-10-30T03:09:42.277 回答