www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] right syntax to use near '' at line 1

  1. #1
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88

    resolved [RESOLVED] right syntax to use near '' at line 1

    All,

    I am getting 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 '' at line 1 which seems to be a common error when using INSERT with a string of data. I checked various forums for a solution. Many solutions indicated the user hadn't set variables properly. As you can see, I echoed my variables to ensure they are set and calling correctly. Other forums recommended a user ensure quotes and ticks are set.

    Here is the output I am getting:

    Code:
    TimsLaptop
    TIMTACULAR
    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 '' at line 1
    
    Variables are set.

    Here is the entire code:

    PHP Code:
    <?phpsession_start();error_reporting(E_ALL); ini_set'display_errors','1');
    $ExpectedRig = ($_SESSION['ExpectedRig']);$compname = ($_SESSION['compname']);echo $ExpectedRig;echo "<br>";echo $compname;echo "<br>";$link_id mysql_connect("l****""****""****") or die("Could not connect."); if(!mysql_select_db("***",$link_id)) die("database was not selected.");


    $file_handle fopen("resultupload/$compname.csv""r");

    while ((
    $line_of_data fgetcsv($file_handle1000",")) !== FALSE) { $line_import_query="INSERT into bridges(MAC,DeviceName,SignalNoiseDBM,Distance,TxRxMbps,CCQ,ConnectionTime,LastIP,ComputerName,ExpectedRig) values('$line_of_data[0]','$line_of_data[1]','$line_of_data[2]','$line_of_data[3]','$line_of_data[4]','$line_of_data[5]','$line_of_data[6]','$line_of_data[7]','$compname','$ExpectedRig'"mysql_query($line_import_query) or die(mysql_error()); }
    $file = ("resultupload/$compname.csv");fclose($file_handle);unlink($file);
    ?>
    Any thoughts as to where I'm going wrong?

  2. #2
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    Found the issue. Missing a close parenthesis on my Insert.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    Looks like you were missing a closing parenthesis. Also, you do not appear to be escaping external inputs into your query, which is asking for trouble. At a minimum, I'd suggest something like:

    PHP Code:
    while (($line_of_data fgetcsv($file_handle1000",")) !== FALSE) {
        
    $line_import_query="
            INSERT into bridges (
              MAC,
              DeviceName,
              SignalNoiseDBM,
              Distance,
              TxRxMbps,
              CCQ,
              ConnectionTime,
              LastIP,
              ComputerName,
              ExpectedRig
            ) VALUES (
              '"
    .mysql_real_escape_string($line_of_data[0])."',
              '"
    .mysql_real_escape_string($line_of_data[1])."',
              '"
    .mysql_real_escape_string($line_of_data[2])."',
              '"
    .mysql_real_escape_string($line_of_data[3])."',
              '"
    .mysql_real_escape_string($line_of_data[4])."',
              '"
    .mysql_real_escape_string($line_of_data[5])."',
              '"
    .mysql_real_escape_string($line_of_data[6])."',
              '"
    .mysql_real_escape_string($line_of_data[7])."',
              '"
    .mysql_real_escape_string($compname)."',
              '"
    .mysql_real_escape_string($ExpectedRig)."'
            )
            "
    ;
        
    mysql_query($line_import_query) or die(mysql_error());

    Better would be to stop using the deprecated MySQL extension, and use either the PDO or MySQLi extension, making use of prepared statements and bound parameters to handle any necessary escaping of inputs.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    Thank you for providing that code; I have implemented it per your recommendation. However, ExpectedRig isn't being Inserted. I confirmed spelling, and we know the variable is set. Any thoughts?

  5. #5
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    Disregard. Had the column set up incorrectly. All is well.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles