Concatenating variables to post to mysql no longer working.

  • lasercorp
    Asked on January 19, 2017 at 5:21 PM

    Hi,

    I have php code to post form submissions to a mysql database.

    It was working fine 6 months ago and when I went to use it today, I got an error with this line

    $ServiceDate = $_POST['servicedate'][2]."-".$_POST['servicedate'][0]."-".$_POST['servicedate'][1];

     

    it says to check the PHP syntax.

     

    Any ideas what would have changed?

     

    Thanks,

     

    Eric

  • Chriistian Jotform Support
    Replied on January 19, 2017 at 10:07 PM

    The error may be due to the array index not being enclosed in ( ' ' ). Can you please try the code below:

    $ServiceDate = $_POST['servicedate']['2']."-".$_POST['servicedate']['0']."-".$_POST['servicedate']['1'];

    If the issue persists, please let us know.
    Regards.

  • lasercorp
    Replied on January 23, 2017 at 12:43 PM

    Hi,

    I tried enclosing the array index is  ' ', but still received the same error.

    The error I am getting is 

    '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 '`ServiceDate` = '2017/01/23' `PrinterNo` = '1' `TimeStarted` = '12:36:00' `TimeF' at line 4"

     

    There is no code at line 4.

     

    Here is the first part of the code

    // This function will run within each post array including multi-dimensional arrays 

    function ExtendedAddslash(&$params)

            foreach ($params as &$var) {

                // check if $var is an array. If yes, it will start another ExtendedAddslash() function to loop to each key inside.

                is_array($var) ? ExtendedAddslash($var) : $var=addslashes($var);

                unset($var);

            }

     

    // Initialize ExtendedAddslash() function for every $_POST variable

    ExtendedAddslash($_POST);      

     

    $SubmissionID = $_POST['submission_id']; 

    $EmployeeID = $_POST['techid'];

    $ServiceDate = $_POST['servicedate']['2']."/".$_POST['servicedate']['0']."/".$_POST['servicedate']['1'];

    $PrinterNo = $_POST['printerid'];

    $TimeStarted = $_POST['starttime']['0'].":".$_POST['starttime']['1'].":00";

    $TimeFinished = $_POST['endtime']['0'].":".$_POST['endtime']['1'].":00";

    $ID = $_POST['submission_id'];

    $drDispatchID = $_POST['dispatchid'];

    $ServiceType = $_POST['servicecode'];

    $CartName = $_POST['cartname'];

    $CartSerialNo = $_POST['cartserial'];

    $DefectCartName = $_POST['defectivecart'];

    $DefectCartSerial = $_POST['defectiveserial'];

    $PageCount = $_POST['pagecount'];

    $StatusSupplyCount = $_POST['statussupply'];

    $PagesRemaining = $_POST['pagesremaining'];

    $TechNote = $_POST['callnotes'];

    $TechLocation = $_POST['locationnote'];

    $TechPrinterNote = $_POST['printernote'];

    $image0 = $_POST['uploadsample'][0];

    $image1 = $_POST['uploadsample'][1];

    $image2 = $_POST['uploadsample'][2];

    $image3 = $_POST['uploadsample'][3];

    $image4 = $_POST['uploadsample'][4];

  • AIDAN
    Replied on January 23, 2017 at 2:30 PM

    Hi,

    Please note that the code you posted does not contain the routine/block that creates the currently erroneous SQL request, so I could not inspect what led to that SQL related error as a result.

    Could you please attach the relevant code too? Thank you in advance.

  • lasercorp
    Replied on January 23, 2017 at 3:01 PM

    Hi,

    Here is the entire code

    i tried to upload a file but there is no option

    <?php

    // This function will run within each post array including multi-dimensional arrays 

    function ExtendedAddslash(&$params)

            foreach ($params as &$var) {

                // check if $var is an array. If yes, it will start another ExtendedAddslash() function to loop to each key inside.

                is_array($var) ? ExtendedAddslash($var) : $var=addslashes($var);

                unset($var);

            }

     

    // Initialize ExtendedAddslash() function for every $_POST variable

    ExtendedAddslash($_POST);      

     

    $SubmissionID = $_POST['submission_id']; 

    $EmployeeID = $_POST['techid'];

    $ServiceDate = $_POST['servicedate']['2']."/".$_POST['servicedate']['0']."/".$_POST['servicedate']['1'];

    $PrinterNo = $_POST['printerid'];

    $TimeStarted = $_POST['starttime']['0'].":".$_POST['starttime']['1'].":00";

    $TimeFinished = $_POST['endtime']['0'].":".$_POST['endtime']['1'].":00";

    $ID = $_POST['submission_id'];

    $drDispatchID = $_POST['dispatchid'];

    $ServiceType = $_POST['servicecode'];

    $CartName = $_POST['cartname'];

    $CartSerialNo = $_POST['cartserial'];

    $DefectCartName = $_POST['defectivecart'];

    $DefectCartSerial = $_POST['defectiveserial'];

    $PageCount = $_POST['pagecount'];

    $StatusSupplyCount = $_POST['statussupply'];

    $PagesRemaining = $_POST['pagesremaining'];

    $TechNote = $_POST['callnotes'];

    $TechLocation = $_POST['locationnote'];

    $TechPrinterNote = $_POST['printernote'];

    $image0 = $_POST['uploadsample'][0];

    $image1 = $_POST['uploadsample'][1];

    $image2 = $_POST['uploadsample'][2];

    $image3 = $_POST['uploadsample'][3];

    $image4 = $_POST['uploadsample'][4];

     

    mysql_connect( $db_host, $db_username, $db_password) or die(mysql_error());

    mysql_select_db($db_name); 

     

    // search submission ID

     

    $query = "SELECT * FROM `tblTechDailyMySQL` WHERE `SubmissionID` = '$submission_id'";

    $sqlsearch = mysql_query($query);

    $resultcount = mysql_numrows($sqlsearch);

     

    if ($resultcount > 0) {

     

        mysql_query("UPDATE `tblTechDailyMySQL` SET 

                                   

    `EmployeeID` = '$techid'

    `ServiceDate` = '$ServiceDate'

    `PrinterNo` = '$PrinterNo'

    `TimeStarted` = '$TimeStarted'

    `TimeFinished` = '$TimeFinished'

    `ID` = '$submission_id'

    `drDispatchID` = '$dispatchid',

    `ServiceType` = '$servicecode',

    `CartName` = '$cartname',

    `CartSerialNo` = '$cartserial',

    `DefectCartName` = '$defectivecart',

    `DefectCartSerial` = '$defectiveserial',

    `PageCount` = '$pagecount',

    `StatusSupplyCount` = '$statussupply',

    `PagesRemaining` = '$pagesremaining',

    `TechNote` = '$callnotes',

    `TechLocation` = '$locationnote',

    `TechPrinterNote` = '$printernote',

    `image0` = '$image0',

    `image1` = '$image1',

    `image2` = '$image2',

    `image3` = '$image3',

    `image4` = '$image4'

     

     WHERE `SubmissionID` = '$submission_id'") 

         or die(mysql_error());

        

    } else {

     

        mysql_query("INSERT INTO `tblTechDailyMySQL` (SubmissionID,EmployeeID,ServiceDate,PrinterNo,ID,TimeStarted,TimeFinished,drDispatchID,ServiceType,CartName,CartSerialNo,DefectCartName,DefectCartSerial,PageCount,StatusSupplyCount,PagesRemaining,TechNote,TechLocation,TechPrinterNote,image0,image1,image2,image3,image4) 

    VALUES ('$SubmissionID','$EmployeeID','$ServiceDate','$PrinterNo','$ID','$TimeStarted','$TimeFinished','$drDispatchID','$ServiceType','$CartName','$CartSerialNo','$DefectCartName','$DefectCartSerial','$PageCount','$StatusSupplyCount','$PagesRemaining','$TechNote','$TechLocation','$TechPrinterNote','$image0','$image1','$image2','$image3','$image4') ") 

        or die(mysql_error());  

     

    }

    ?>

  • AIDAN
    Replied on January 23, 2017 at 4:39 PM

    Hi again,

    Thanks to the code that you kindly provided, I managed to spot the problem immediately:

    Your code is doing:

    mysql_query("UPDATE `tblTechDailyMySQL` SET 

    `EmployeeID` = '$techid'

    `ServiceDate` = '$ServiceDate'

    `PrinterNo` = '$PrinterNo'

    `TimeStarted` = '$TimeStarted'

    `TimeFinished` = '$TimeFinished'

    `ID` = '$submission_id'

    `drDispatchID` = '$dispatchid',

    `ServiceType` = '$servicecode', ... and so on

     

    Please notice how it's missing a comma between the first 6 values, which results in the SQL error.

    It should look like:

     

    mysql_query("UPDATE `tblTechDailyMySQL` SET 

    `EmployeeID` = '$techid',

    `ServiceDate` = '$ServiceDate',

    `PrinterNo` = '$PrinterNo',

    `TimeStarted` = '$TimeStarted',

    `TimeFinished` = '$TimeFinished',

    `ID` = '$submission_id',

    `drDispatchID` = '$dispatchid',

    `ServiceType` = '$servicecode', ... and so on

     

    If you need further assistance please let us know. We will be happy to help. Thank you.

  • lasercorp
    Replied on January 23, 2017 at 5:58 PM

    I'm not sure what happened, but yes, it was the missing commas.

    Thanks alot !!!