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

10
  • Try printing the variable $sql and see or post the query, I think you have syntax error in the query. Commented Nov 1, 2012 at 10:08
  • First of all always use mysqli_error() and secondly this is not at all sanitized, atleast use mysqli_real_escape_string() Commented Nov 1, 2012 at 10:09
  • 1
    You are using an obsolete database API and should use a modern replacement. You are also exposing yourself to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Nov 1, 2012 at 10:10
  • Try putting the fieldname group inside backticks. The error might be due to it being a keyword. Commented Nov 1, 2012 at 10:11
  • Group is a keyword in MySQL. Adding backslashes might be helpful -Updated Commented Nov 1, 2012 at 10:11

5 Answers 5

1

group is an SQL keyword. If this is the name of one of your fields you must enclose it with ` like so:

`group`,`address1`,...

This tells mySQL that it is a field name and not the keyword. It would be good practice to enclose all your fields within ` to prevent any errors like this you may not have noticed.

Sign up to request clarification or add additional context in comments.

Comments

0

Spot on by pburgess. GROUP, ORDER are some common field names that we use while programming. Make sure to enclose these using backticks group, always a good practice.

$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]'

    )" ;

1 Comment

Thank you for reminding me the basic issues and it is now working.
0

Try this

$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]')" ;

Because you got some field names which is reserved keywords like for example you have group

Comments

0

Your $_POST values is missing quotes, Try updating them

 $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\"]'

            )" ;

Comments

0

Well it is too late but this might help someone. Whenever you want to check why your query doesnt' work.. Always try to echo your query and paste it to the phpmyadmin and it will throw the mySQL error which are more easy to understand that what's the issue in query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.