12 Replies Latest reply: Sep 12, 2014 12:25 PM by Max Resnikoff RSS

    SQL Error Help

    Max Resnikoff Community Member

      Hi,

       

      I have been making forms to insert data into my database. I have had no trouble with the past 3, but this one keeps giving me an error. They are all exactly the same, and I have even retried remaking the page and table in the database, but no luck.

       

      This is the 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 'condition, image, notes, location, tagnumber) VALUES ('sdf', 'sdf', 'Shirt', 'sd' at line 1

       

       

      ------------------------------------------------------------------------------------------ -----------------------------------------------

       

      Here is my table code:


      -- phpMyAdmin SQL Dump

      -- version 3.5.5

      --

      -- Host: localhost

      -- Generation Time: Sep 09, 2014 at 08:41 AM

      -- Server version: 5.5.39-36.0

      -- PHP Version: 5.4.23

       

      SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

      SET time_zone = "+00:00";

       

       

      /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

      /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

      /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

      /*!40101 SET NAMES utf8 */;

       

      --

      -- Database: `mresnik_dramadatabase`

      --

       

      -- --------------------------------------------------------

       

      --

      -- Table structure for table `costume`

      --

       

      CREATE TABLE IF NOT EXISTS `costume` (

        `id` int(11) NOT NULL AUTO_INCREMENT,

        `name` text NOT NULL,

        `description` text NOT NULL,

        `type` text NOT NULL,

        `material` text NOT NULL,

        `size` text NOT NULL,

        `gender` text NOT NULL,

        `colour` text NOT NULL,

        `timeperiod` text NOT NULL,

        `accessories` text NOT NULL,

        `value` text NOT NULL,

        `condition` text NOT NULL,

        `image` text NOT NULL,

        `notes` text NOT NULL,

        `location` text NOT NULL,

        `tagnumber` int(50) NOT NULL,

        `datecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

        PRIMARY KEY (`id`)

      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

       

      /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

      /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

      /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

       

      ------------------------------------------------------------------------------------------ -------------------------------------------------------------------------

       

       

      And here is my page code where the form is:


       

      <?php require_once('Connections/drama_database.php'); ?>

      <?php

      if (!function_exists("GetSQLValueString")) {

      function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

      {

        if (PHP_VERSION < 6) {

          $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

        }

       

        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

       

        switch ($theType) {

          case "text":

            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

            break;  

          case "long":

          case "int":

            $theValue = ($theValue != "") ? intval($theValue) : "NULL";

            break;

          case "double":

            $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

            break;

          case "date":

            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

            break;

          case "defined":

            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

            break;

        }

        return $theValue;

      }

      }

       

      $editFormAction = $_SERVER['PHP_SELF'];

      if (isset($_SERVER['QUERY_STRING'])) {

        $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

      }

       

      if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

        $insertSQL = sprintf("INSERT INTO costume (name, `description`, type, material, `size`, gender, colour, timeperiod, accessories, `value`, condition, image, notes, location, tagnumber) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                             GetSQLValueString($_POST['name'], "text"),

                             GetSQLValueString($_POST['description'], "text"),

                             GetSQLValueString($_POST['type'], "text"),

                             GetSQLValueString($_POST['material'], "text"),

                             GetSQLValueString($_POST['size'], "text"),

                             GetSQLValueString($_POST['gender'], "text"),

                             GetSQLValueString($_POST['colour'], "text"),

                             GetSQLValueString($_POST['timeperiod'], "text"),

                             GetSQLValueString($_POST['accessories'], "text"),

                             GetSQLValueString($_POST['value'], "text"),

                             GetSQLValueString($_POST['condition'], "text"),

                             GetSQLValueString($_POST['image'], "text"),

                             GetSQLValueString($_POST['notes'], "text"),

                             GetSQLValueString($_POST['location'], "text"),

                             GetSQLValueString($_POST['tag'], "text"));

       

        mysql_select_db($database_drama_database, $drama_database);

        $Result1 = mysql_query($insertSQL, $drama_database) or die(mysql_error());

       

        $insertGoTo = "costumeinventory.php";

        if (isset($_SERVER['QUERY_STRING'])) {

          $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

          $insertGoTo .= $_SERVER['QUERY_STRING'];

        }

        header(sprintf("Location: %s", $insertGoTo));

      }

       

      mysql_select_db($database_drama_database, $drama_database);

      $query_cosadd = "SELECT * FROM costume";

      $cosadd = mysql_query($query_cosadd, $drama_database) or die(mysql_error());

      $row_cosadd = mysql_fetch_assoc($cosadd);

      $totalRows_cosadd = mysql_num_rows($cosadd);

      ?>

      <html>

      <title>Add Costume</title>

      </html>

       

      <?php session_start(); ?>

       

      <!--HEADER CONTENT START -->

      <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>

      <script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>

      <script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>

      <head>

      <link href="stylesheet.css" rel="stylesheet" type="text/css">

      <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">

      </head>

      </html>

      <style type="text/css">

      body{ margin:0px; background:#FFF;}

      .header {

          height:165px;

          background:#FFF;

          border:1px solid#CCC;

          position:fixed;

          width:100%;

          top:0px;

      }

       

      </style>

      </head>

       

       

      <div class="header"><?php include('defaultheader.php');?></div>

      <br>

      <br>

      <br>

      <br>

      <br>

      <br>

      <br>

      <br>

        <!--HEADER CONTENT END -->

      <div align="center">

          <h1>Add New Costume</h1>

      </div>

       

      <div align="center">

        <form name="form1" method="POST" action="<?php echo $editFormAction; ?>">

       

        <table width="350" border="0">

          <tr>

                <td width="118" class="formtag">Name</td>

                <td width="222"><span id="sprytextfield1">

                  <label for="name"></label>

                  <input name="name" type="text" class="formfield" id="name">

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Description</td>

                <td><span id="sprytextfield2">

                  <label for="description"></label>

                  <textarea name="description" class="formfielddesc" id="description"></textarea>

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Type</td>

                <td><label for="type"></label>

                  <select name="type" class="formfield" id="type">

                

      <!--Connection to Other Table-->          

                       <option id="0">-- Select Type --</option>

                      <?php

                          require("Connections/drama_database.php");

                        

                          $getallcostumetype = mysql_query("SELECT * FROM costumetype");                  

                          while($viewallcostumetype = mysql_fetch_array($getallcostumetype)){

                      ?>

                    

                      <option id="<?php echo $viewallcostumetype['id']; ?>"><?php echo $viewallcostumetype['type'] ?></option>

                      <?php } ?>

      <!--Connection to Other Table-->

                

                </select></td>

              </tr>

              <tr>

                <td class="formtag">Material/Fabric</td>

                <td><span id="sprytextfield3">

                  <label for="material"></label>

                  <input name="material" type="text" class="formfield" id="material">

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Size</td>

                <td><label for="size"></label>

                  <select name="size" class="formfield" id="size">

                

      <!--Connection to Other Table-->          

                       <option id="0">-- Select Size --</option>

                      <?php

                          require("Connections/drama_database.php");

                        

                          $getallcostumesize = mysql_query("SELECT * FROM costumesize");                  

                          while($viewallcostumesize = mysql_fetch_array($getallcostumesize)){

                      ?>

                    

                      <option id="<?php echo $viewallcostumesize['id']; ?>"><?php echo $viewallcostumesize['size'] ?></option>

                      <?php } ?>

      <!--Connection to Other Table-->

                

                </select></td>

              </tr>

              <tr>

                <td class="formtag">Gender</td>

                <td><label for="gender"></label>

                  <select name="gender" class="formfield" id="gender">

                

      <!--Connection to Other Table-->          

                       <option id="0">-- Select Gender --</option>

                      <?php

                          require("Connections/drama_database.php");

                        

                          $getallcostumegender = mysql_query("SELECT * FROM costumegender");                  

                          while($viewallcostumegender = mysql_fetch_array($getallcostumegender)){

                      ?>

                    

                      <option id="<?php echo $viewallcostumegender['id']; ?>"><?php echo $viewallcostumegender['gender'] ?></option>

                      <?php } ?>

      <!--Connection to Other Table-->

                

                </select></td>

              </tr>

              <tr>

                <td class="formtag">Colour/Pattern</td>

                <td><span id="sprytextfield4">

                  <label for="colour"></label>

                  <input name="colour" type="text" class="formfield" id="colour">

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Timeperiod</td>

                <td><span id="sprytextfield5">

                  <label for="timeperiod"></label>

                  <input name="timeperiod" type="text" class="formfield" id="timeperiod">

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Accessories</td>

                <td><span id="sprytextfield6">

                  <label for="accessories"></label>

                  <textarea name="accessories" class="formfielddesc" id="accessories"></textarea>

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Value</td>

                <td><label for="value"></label>

                  <select name="value" class="formfield" id="value">

                

                

      <!--Connection to Other Table-->          

                       <option id="0">-- Select Value --</option>

                      <?php

                          require("Connections/drama_database.php");

                        

                          $getallcostumevalue = mysql_query("SELECT * FROM costumevalue");                  

                          while($viewallcostumevalue = mysql_fetch_array($getallcostumevalue)){

                      ?>

                    

                      <option id="<?php echo $viewallcostumevalue['id']; ?>"><?php echo $viewallcostumevalue['value'] ?></option>

                      <?php } ?>

      <!--Connection to Other Table-->

                

                </select></td>

              </tr>

              <tr>

                <td class="formtag">Condition</td>

                <td><label for="condition"></label>

                  <select name="condition" class="formfield" id="condition">

                

      <!--Connection to Other Table-->          

                       <option id="0">-- Select Condition --</option>

                      <?php

                          require("Connections/drama_database.php");

                        

                          $getallcostumecondition = mysql_query("SELECT * FROM costumecondition");                  

                          while($viewallcostumecondition = mysql_fetch_array($getallcostumecondition)){

                      ?>

                    

                      <option id="<?php echo $viewallcostumecondition['id']; ?>"><?php echo $viewallcostumecondition['condition'] ?></option>

                      <?php } ?>

      <!--Connection to Other Table-->

                

                </select></td>

              </tr>

              <tr>

                <td class="formtag">Image</td>

                <td><span id="sprytextfield7">

                  <label for="image"></label>

                  <input name="image" type="text" class="formfield" id="image">

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Notes</td>

                <td><span id="sprytextfield8">

                  <label for="notes"></label>

                  <textarea name="notes" class="formfielddesc" id="notes"></textarea>

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td class="formtag">Location</td>

                <td><label for="location"></label>

                  <select name="location" class="formfield" id="location">

                

                

      <!--Connection to Other Table-->          

                       <option id="0">-- Select Location --</option>

                      <?php

                          require("Connections/drama_database.php");

                        

                          $getallcostumelocation = mysql_query("SELECT * FROM costumelocation");                  

                          while($viewallcostumelocation = mysql_fetch_array($getallcostumelocation)){

                      ?>

                    

                      <option id="<?php echo $viewallcostumelocation['id']; ?>"><?php echo $viewallcostumelocation['location'] ?></option>

                      <?php } ?>

      <!--Connection to Other Table-->

                

                </select></td>

              </tr>

              <tr>

                <td class="formtag">Tag Number</td>

                <td><span id="sprytextfield9">

                  <label for="tag"></label>

                  <input name="tag" type="text" class="formfield" id="tag">

                <span class="textfieldRequiredMsg">A value is required.</span></span></td>

              </tr>

              <tr>

                <td> </td>

                <td> </td>

              </tr>

              <tr>

                <td colspan="2" align="center"><input name="addcos" type="submit" class="button" id="addcos" value="Add Costume"></td>

              </tr>

            </table>

        <input type="hidden" name="MM_insert" value="form1">

        </form>

      </div>

      <?php

      mysql_free_result($cosadd);

      ?>

      <script type="text/javascript">

      var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");

      var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");

      var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");

      var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");

      var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5");

      var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6");

      var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7");

      var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8");

      var sprytextfield9 = new Spry.Widget.ValidationTextField("sprytextfield9");

      </script>

        • 1. Re: SQL Error Help
          bregent CommunityMVP

          Echo the value of $insertSQL after you populate it so you can check for syntax errors.

          • 2. Re: SQL Error Help
            Max Resnikoff Community Member

            After I populate what?

            • 3. Re: SQL Error Help
              osgood_ CommunityMVP

              Try changing this part of the code:

               

              GetSQLValueString($_POST['tag'], "text"));

               

              to int ( see below - as that is what your sql database column is assigned for - `tagnumber` int(50) NOT NULL,

               

              GetSQLValueString($_POST['tag'], "int"));

              • 4. Re: SQL Error Help
                bregent CommunityMVP

                >After I populate what?


                After you have populated that variable. You want to see the value of the variable that contains the complete SQL statement. But osgood has a good point that your datatypes do not match - that is probably the cause of the problem.

                • 5. Re: SQL Error Help
                  Max Resnikoff Community Member

                  I am still getting the error, even with your suggestion.

                  • 6. Re: SQL Error Help
                    bregent CommunityMVP

                    So, did you try my suggestion? You can't effectively troubleshoot an error message if you don't know the SQL string your are submitting.

                    • 7. Re: SQL Error Help
                      Max Resnikoff Community Member

                      I still dont get where you are suggesting to put the debug code?

                       

                      Sorry I am new to this (School project)

                       

                      I put it at the top of the code, and it says this now:

                       

                      Warning: mysql_pconnect(): MySQL server has gone away in C:\xampp\htdocs\dramadatabase\Connections\drama_database.php on line 9

                       

                      Notice: Undefined variable: insertSQL in C:\xampp\htdocs\dramadatabase\costumeadd.php on line 2

                      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 'condition, image, notes, location, code) VALUES ('asd', 'asd', '-- Select Type -' at line 1

                      • 8. Re: SQL Error Help
                        bregent CommunityMVP

                        Right, you need to put it AFTER you have assigned a value to the variable. See the code below. You'll also need to comment out any code that redirects to a new page. Post the results back here.

                         

                        if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

                          $insertSQL = sprintf("INSERT INTO costume (name, `description`, type, material, `size`, gender, colour, timeperiod, accessories, `value`, condition, image, notes, location, tagnumber) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                                               GetSQLValueString($_POST['name'], "text"),

                                               GetSQLValueString($_POST['description'], "text"),

                                               GetSQLValueString($_POST['type'], "text"),

                                               GetSQLValueString($_POST['material'], "text"),

                                               GetSQLValueString($_POST['size'], "text"),

                                               GetSQLValueString($_POST['gender'], "text"),

                                               GetSQLValueString($_POST['colour'], "text"),

                                               GetSQLValueString($_POST['timeperiod'], "text"),

                                               GetSQLValueString($_POST['accessories'], "text"),

                                               GetSQLValueString($_POST['value'], "text"),

                                               GetSQLValueString($_POST['condition'], "text"),

                                               GetSQLValueString($_POST['image'], "text"),

                                               GetSQLValueString($_POST['notes'], "text"),

                                               GetSQLValueString($_POST['location'], "text"),

                                               GetSQLValueString($_POST['tag'], "text"));

                         

                        echo $insertSQL;

                         

                        //  mysql_select_db($database_drama_database, $drama_database);

                        //  $Result1 = mysql_query($insertSQL, $drama_database) or die(mysql_error());

                         

                        //$insertGoTo = "costumeinventory.php";

                        //  if (isset($_SERVER['QUERY_STRING'])) {

                        //    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

                        //    $insertGoTo .= $_SERVER['QUERY_STRING'];

                        //  }

                        //  header(sprintf("Location: %s", $insertGoTo));

                        }

                        //

                        //mysql_select_db($database_drama_database, $drama_database);

                        //$query_cosadd = "SELECT * FROM costume";

                        //$cosadd = mysql_query($query_cosadd, $drama_database) or die(mysql_error());

                        //$row_cosadd = mysql_fetch_assoc($cosadd);

                        //$totalRows_cosadd = mysql_num_rows($cosadd);

                        • 9. Re: SQL Error Help
                          Max Resnikoff Community Member

                          Warning: mysql_pconnect(): MySQL server has gone away in C:\xampp\htdocs\dramadatabase\Connections\drama_database.php on line 9

                           

                          Notice: Undefined variable: insertSQL in C:\xampp\htdocs\dramadatabase\costumeadd.php on line 2

                          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 'condition, image, notes, location, code) VALUES ('asd', 'asd', '-- Select Type -' at line 1

                          • 10. Re: SQL Error Help
                            osgood_ CommunityMVP

                            'condition' must be a mysql 'reserved' word.

                             

                            I deconstructed the query and this is what is causing the error:

                             

                            GetSQLValueString($_POST['condition'], "text"),

                             

                            Change it to something else like 'conditions':

                             

                            GetSQLValueString($_POST['conditions'], "text"),

                             

                             

                            Dont forget to change it in your query string:

                             

                              $insertSQL = sprintf("INSERT INTO costume (name, `description`, type, material, `size`, gender, colour, timeperiod, accessories, `value`, conditions, image, notes, location, tagnumber)

                             

                             

                            and your form select tag:

                             

                             

                            <select name="conditions" class="formfield" id="condition">

                             

                             

                            and your database column name

                            • 11. Re: SQL Error Help
                              bregent CommunityMVP

                              >'condition' must be a mysql 'reserved' word.

                               

                              It certainly is...

                               

                              MySQL :: MySQL 5.5 Reference Manual :: 9.3 Reserved Words

                               

                               

                              >Change it to something else like 'conditions':

                               

                              I agree. You could use single quotes, but it's far better to avoid the use of reserved words. I would change all of the others too.

                              • 12. Re: SQL Error Help
                                Max Resnikoff Community Member

                                DONE!!! Thank you very much for your help!

                                Very much appreciated!