2

This is the code for the form:

   <div id="regform">
   <div id="regform-top">
   <h2>User Registration</h2>
   <p>Please complete this form</p>
   </div>

   <form id="register-form" name="register-form" action="submit.php"  method="post"    
   class="validation">
   <fieldset>

   <table>

  <tr>
  <td>
  <div class="fieldgroup">
  <label for="user-name">User name*: </label>
  <input type="text" id="name" name="name" value="" size="12" class="inpt" /><br    
  class="clear" />
  </div>
  <div class="fieldgroup">
  <label for="password">Password*: </label>

  <input type="password" id="password" name="password" value="" size="12" class="inpt"     
   /><br class="clear" />

  </div>
  </td>
  <td id="form-note">
  <br />

  <p><strong>Form  Instructions</strong></p>

  <p>*Required Field</p>
  </td>
  </tr>

  </table>


  <table id="bottom-reg">

  <tr>
  <td><br />
  <div class="fieldgroup">
  <label for="firstname">First name*</label>
  <input type="text" id="firstname" name="firstname" value="" size="12" class="inpt" />  
  <br class="clear" />

  </div>

   <div class="fieldgroup">
  <label for="email">Email Address* </label>
  <input type="text" id="email" name="email" value="" size="12" class="inpt" /><br 
  class="clear" />
  </div>


  <div class="fieldgroup">
  <label for="address1">Address 1* </label>
  <input type="text" id="address1" name="address1" value="" size="12" class="inpt" />   
  <br class="clear" />
  </div>
  <label for="address2">Address 2</label>
  <input type="text" id="address2" name="address2" value="" size="12" class="inpt" />   
  <br class="clear" />
  <label for="address2">Address 3</label>
  <input type="text" id="address3" name="address3" value="" size="12" class="inpt" />   
  <br class="clear" />

  <div class="fieldgroup">
  <label for="country">Country*</label>
  <input type="text" id="country" name="country" value="" size="12" class="inpt" /><br    
  class="clear" />
  </div>
  </td>
  <td><br />
  <div class="fieldgroup">
  <label for="lastname">Last name*</label>
  <input type="text" id="lastname" name="lastname" value="" size="12" class="inpt" />   
  <br class="clear" />
  </div>

  <div class="fieldgroup">
  <label for="group">Group* </label>
  <input type="text" id="name" name="group" value="" size="12" class="inpt" />


  <br class="clear" />
  </div>

  <div class="fieldgroup">
   <label for="city">City* </label>
  <input type="text" id="city" name="city" value="" size="12" class="inpt" /><br  
   class="clear" />
   </div>

   <div class="fieldgroup">
   <label for="city">State* </label>
  <input type="text" id="state" name="state" value="" size="12" class="inpt" /><br    
  class="clear" />
  </div>

  <div class="fieldgroup">
  <label for="zip">Zip*</label>
  <input type="text" id="zip" name="zip" value="" size="12" class="inpt" /><br    
  class="clear" />
  </div>

  </td>

  </tr>

  </table>

  <input type="submit" value="Register" class="submit-btn" />
  </fieldset>
  </form>

This is the code for submit.php:

     <?php

     $con = mysql_connect("localhost","viatechp_invacar","storefront72");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }


  mysql_select_db("viatechp_invacare", $con);



    $sql="INSERT INTO registration(username,password,fname,lname,group,
    address1,address2,address3, email,city,state,zip,country )
    VALUES
    ('$_POST[name]','$_POST[password]', '$_POST[firstname]','$_POST[lastname]','  
    $_POST[group]','$_POST[address1]','$_POST[address2]','$_POST[address3]',                                                           
   '$_POST[email]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]'

    )" ;

  $query = mysql_query($sql) or die(mysql_error());

  $results = mysql_fetch_assoc($query);

   if ($results) { 
  echo 'The query returned ' . $results[ 'registration' ];
  } else {
  echo 'The query did not return any results';
  } ?>


  echo $sql;


  ?>

It is showing an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group,address1,address2,address3,email,city,state,zip,country ) VALUES ( 'sdfdsf' at line 1

4

5 回答 5

1

group 是一个 SQL 关键字。如果这是您的某个字段的名称,则必须用 ` 将其括起来,如下所示:

`group`,`address1`,...

这告诉 mySQL 它是一个字段名而不是关键字。最好将所有字段包含在 ` 中,以防止出现任何您可能没有注意到的错误。

于 2012-11-01T10:09:05.357 回答
0

由 pburgess 发现。GROUP、ORDER 是我们在编程时使用的一些常见字段名称。确保使用反引号将它们括起来group,这始终是一个好习惯。

$sql="INSERT INTO registration(username,password,fname,lname,`group`,
    address1,address2,address3, email,city,state,zip,country )
    VALUES
    ('$_POST[name]','$_POST[password]', '$_POST[firstname]','$_POST[lastname]','  
    $_POST[group]','$_POST[address1]','$_POST[address2]','$_POST[address3]',                                                           
   '$_POST[email]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]'

    )" ;
于 2012-11-01T10:11:58.113 回答
0

尝试这个

$sql="INSERT INTO registration(`username`,`password`,`fname`,`lname`,`group`,
       `address1`,`address2`,`address3`, `email`,`city`,`state`,`zip`,`country` )
       VALUES
      ('$_POST[name]','$_POST[password]', '$_POST[firstname]','$_POST[lastname]',
      '$_POST[group]','$_POST[address1]','$_POST[address2]','$_POST[address3]',                                                           
      '$_POST[email]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]')" ;

因为你有一些字段名称是保留关键字,例如你有group

于 2012-11-01T10:12:24.590 回答
0

您的$_POST值缺少引号,请尝试更新它们

 $sql="INSERT INTO registration
     (`username`,`password`,`fname`,`lname`,`group`,`address1`,`address2`,`address3`, `email`,`city`,`state`,`zip`,`country`)
     VALUES
     ('$_POST[\"name\"]','$_POST[\"password\"]',$_POST[\"firstname\"]','$_POST[\"lastname\"]','  
            $_POST[\"group\"]','$_POST[\"address1\"]','$_POST[\"address2\"]','$_POST[\"address3\"]',                                                           
           '$_POST[\"email\"]','$_POST[\"city\"]','$_POST[\"state\"]','$_POST[\"zip\"]','$_POST[\"country\"]'

            )" ;
于 2012-11-01T10:13:31.343 回答
0

好吧,为时已晚,但这可能会对某人有所帮助。每当您想检查为什么您的查询不起作用时.. 始终尝试回显您的查询并将其粘贴到 phpmyadmin,它会抛出 mySQL 错误,这更容易理解查询中的问题

于 2017-04-22T21:38:40.520 回答