Jump to content

Recommended Posts

Hello, I'm trying to create an 'insert into' SQL statement but I keep getting the same error message and I don't know why.

 

The error I get is:

SQL 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 ''EventTypeID', 'EventPictureDescription', 'EventPictureTakenLocation', 'EventPic' at line 1

 

The SQL I'm using (well, whats being sent) is as follows:

 INSERT INTO events ( 'EventTypeID', 'EventPictureDescription', 'EventPictureTakenLocation', 'EventPictureTakenDate', 'EventPictureImagePath') VALUES ('1', 'Description 01', '', '1981-07-05', 'images/uploads/00000020.jpg' )

 

The PHP code I'm using to send this SQL is:

 

$PHPDate = strtotime( $_POST['datetaken'] );
$MySQLDate = ( $PHPDate == "" ) ? "" : date( 'Y-m-d', $PHPDate );
   
include( "startmysql.inc" );
   
$Sql = "INSERT INTO events ( 'EventTypeID', 'EventPictureDescription', 'EventPictureTakenLocation', 'EventPictureTakenDate', 'EventPictureImagePath')
VALUES ('".$_POST['SelectEvent']."', '".$_POST['description']."', '".$_POST['location']."', '".$MySQLDate."', '$largeimage' )";

$Returned = startmysql( $Sql );
endmysql();

 

(Contents of startmysql.inc, which works fine for retrieving records, just not for appending them)

<?php
   $MySQLConnection = "";

   function startmysql( $Sql )
   {
      $Username = "UName";
      $Password = "PWord";
      $Database = "DBase";
      $Hostname = "HName";
      global $MySQLConnection;
      $MySQLConnection =   mysql_connect( $Hostname, $Username, $Password ) or die("Unable to connect to MySQL Database!!<br>\n");
      $MySQLSelectedDB = mysql_select_db( $Database, $MySQLConnection ) or die("Could not Set the Database!!<br>\n");
      
      $Returned = mysql_query( $Sql );
  if ($Returned == false)
      {
         // Remove following line from production servers 
         die("SQL error: ".mysql_error()."<br>\n<br>Original query: $Sql \n<br>\n<br>");
      }   
  return( $Returned );
   }
   function endmysql()
   {
      global $MySQLConnection;
      mysql_close( $MySQLConnection );
   }
?>

 

I'll mention that the SQL I'm sending contains all column headers except the primary key, which is an auto incrementing integer.

I know it's a lot to ask, but does anyone have any idea what I'm doing wrong as the syntax looks fine to me. (I'm using MySQL 5.0.51a on a Windows XP based Apache system)

Link to comment
https://forums.phpfreaks.com/topic/234176-i-hve-an-error-in-my-sql-syntax/
Share on other sites

you are using ' instead of backtics here:

$Sql = "INSERT INTO events ( 'EventTypeID', 'EventPictureDescription', 'EventPictureTakenLocation', 'EventPictureTakenDate', 'EventPictureImagePath')

 

your options:

a) change the ' for backtics as in:

$Sql = "INSERT INTO events ( `EventTypeID`, `EventPictureDescription`, `EventPictureTakenLocation`, `EventPictureTakenDate`, `EventPictureImagePath`)   

or, considering that none of those fieldname is a mysql reserver word you can eliminate the backtics and just use the field name

 

$Sql = "INSERT INTO events ( EventTypeID, EventPictureDescription, EventPictureTakenLocation, EventPictureTakenDate, EventPictureImagePath)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.