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

  • Try printing the variable $sql and see or post the query, I think you have syntax error in the query. – Pradeep Simha Nov 01 '12 at 10:08
  • First of all always use `mysqli_error()` and secondly this is not at all sanitized, atleast use `mysqli_real_escape_string()` – Mr. Alien Nov 01 '12 at 10:09
  • 1
    You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also exposing yourself to [SQL injection attacks](http://bobby-tables.com/) that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 01 '12 at 10:10
  • Try putting the fieldname `group` inside backticks. The error might be due to it being a keyword. – air4x Nov 01 '12 at 10:11
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Nov 02 '12 at 02:33
  • `Group` is a keyword in MySQL. Adding backslashes might be helpful -Updated –  Nov 01 '12 at 10:11
  • I don't believe State is a keyword: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html – CodePB Nov 01 '12 at 10:13
  • I copied the sql into notepad++ and changed the syntax to SQL. Both the group and state are highlighted. I know about group so assumed state is another such reserved word. –  Nov 01 '12 at 10:17
  • The syntax I selected is SQL and state is a reserved word in SQL. Thanks for pointing my mistake. –  Nov 01 '12 at 10:20
  • No problem, I looked it up a bit more and found the same! – CodePB Nov 01 '12 at 10:23

5 Answers5

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.

CodePB
  • 1,736
  • 12
  • 19
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]'

    )" ;
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

Deepak
  • 6,684
  • 18
  • 69
  • 121
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\"]'

            )" ;
Sibu
  • 4,609
  • 2
  • 26
  • 38
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

Zohra Gadiwala
  • 206
  • 1
  • 4
  • 17