Hi all,

I am trying to write a function that will insert data into a table when a function is called but I am not sure on how is is done. What I have is:

function mainInsert(){

$insertSQL = sprintf("INSERT INTO LHRFlightStats24Test(AODBUniqueID, CodeShare, FlightDate,DayNumber,ArriveDepart, ScheduledDateTime, ScheduledTime,EstimatedTime,CurrentTime,Flight,AirportCode,City,IATALookup,StatusCode,RemarksWithTime,Gate,Terminal,FlightActive,FlightDisplay,RollOffTime, TimeStamp,Logo, ActualGateTime, ActualTime, AirlineCode, AirlineName,  AirportName, Baggage,   CurrentGateTime,  DestinationAirportName, DestinationCity, EstimatedGateTime,  LastUpdate, OriginAirportCode, OriginAirportName, OrigonCity, Remarks,  ScheduledGateTime, Status, FlightHistoryID, FlightNumber,DestinationAirportCode) VALUES (%s, %s, %s, %s, %s,%s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,  %s, %s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s,%s, %s, %s, %s, %s, %s, %s, %s, %s)",


                   GetSQLValueString($_POST['AODBUniqueID'] = $AODBUniqueID, "text"),
				   GetSQLValueString($_POST['CodeShare'] = $CodeShare, "text"),
				   GetSQLValueString($_POST['FlightDate'] = $FlightDate, "date"),
				   GetSQLValueString($_POST['DayNumber'] = $DayNumber, "text"),
				   GetSQLValueString($_POST['ArriveDepart'] = $I, "text"),
				   GetSQLValueString($_POST['ScheduledDateTime'] = $ScheduledDateTime, "date"),
				   GetSQLValueString($_POST['ScheduledTime'] = $ScheduledTime, "text"),
				   GetSQLValueString($_POST['EstimatedTime'] = $EstimatedTime, "text"),
				   GetSQLValueString($_POST['CurrentTime'] = $CurrentTime	, "text"),
				   GetSQLValueString($_POST['Flight'] = $FlightNo, "text"),
				   GetSQLValueString($_POST['AirportCode'] = $AirportCode , "text"),
				   GetSQLValueString($_POST['City'] = $City , "text"),
				   GetSQLValueString($_POST['IATALookup'] = $IATALookup , "text"),
				   GetSQLValueString($_POST['StatusCode'] = $StatusCode, "text"),
				   GetSQLValueString($_POST['RemarksWithTime'] = $RemarksWithTime	, "text"),
				   GetSQLValueString($_POST['Gate'] = $Gate, "text"),
				   GetSQLValueString($_POST['Terminal'] = $Terminal, "text"),
				   GetSQLValueString($_POST['FlightActive'] = $FlightActive, "text"),
				   GetSQLValueString($_POST['FlightDisplay'] = $FlightDisplay, "text"),
				   GetSQLValueString($_POST['RollOffTime'] = $RollOffTime, "date"),
				   GetSQLValueString($_POST['TimeStamp'] = $RecordTimeStamp, "date"),
				   GetSQLValueString($_POST['Logo'] = $LogoPath, "text"),
                   GetSQLValueString($_POST['ActualGateTime'] = $ActualGateTime , "text"),
                   GetSQLValueString($_POST['ActualTime'] = $ActualTime , "text"),
                   GetSQLValueString($_POST['AirlineCode'] = $AirlineCode , "text"),
                   GetSQLValueString($_POST['AirlineName'] = $AirlineName, "text"),
                   GetSQLValueString($_POST['AirportName'] = $AirportName , "text"),
                   GetSQLValueString($_POST['Baggage'] = $Baggage, "text"),
                   GetSQLValueString($_POST['CurrentGateTime'] = $CurrentGateTime, "text"),
                   GetSQLValueString($_POST['DestinationAirportName'] = $DestinationAirportName, "text"),
                   GetSQLValueString($_POST['DestinationCity'] = $DestinationCity	, "text"),
                   GetSQLValueString($_POST['EstimatedGateTime'] = $EstimatedGateTime, "text"),
                   GetSQLValueString($_POST['LastUpdate'] = $LastUpdate, "text"),
                   GetSQLValueString($_POST['OriginAirportCode'] = $OriginAirportCode, "text"),
                   GetSQLValueString($_POST['OriginAirportName'] = $OriginAirportName, "text"),
                   GetSQLValueString($_POST['OrigonCity'] = $OriginCity, "text"),
                   GetSQLValueString($_POST['Remarks'] = $Remarks	, "text"),
                   GetSQLValueString($_POST['ScheduledGateTime'] = $ScheduledGateTime, "text"),
                   GetSQLValueString($_POST['Status'] = $Status, "text"),
                   GetSQLValueString($_POST['FlightHistoryID'] = $FlightHistoryID, "text"),
                   GetSQLValueString($_POST['FlightNumber'] = $FlightNumber	, "text"),
                   GetSQLValueString($_POST['DestinationAirportCode'] = $DestinationAirportCode, "text"));

  mysql_select_db($database_flightq, $flightq);
  $Depart = mysql_query($insertSQL, $flightq) or die(mysql_error());
}

the at some point in the script call the function:

mainInsert();

Am I coding this correctly. Any pointers would be great.

    My suggestion to you is to use "SET" instead of "VALUES" so you aren't that dependant on the order of your data (since you obviously deal with a great amount of different columns). Otherwise it looks quite good.

      Why do you have things like:

      $_POST['AODBUniqueID'] = $AODBUniqueID

      inside your calls to GetSQLValueString() ? Since $AODBUniqueID is an undefined variable, you're overwriting $_POST['AODBUniqueID'] with NULL (and the result of that expression will thus be NULL). So not only are you inserting NULL values, but you're also overwriting (and thus losing) the POST'ed data.

      In addition, your mysql_*() functions reference an undefined variable $flightq.

        Hi Brad,

        Many thanks for your reply.

        All the variables insdie the insert function are set by a previous function as is the "$flightq" var.

        But I do appriciate your comments as I consider you to be one of the best coders on the forum.

          dcjones;10979751 wrote:

          All the variables insdie the insert function are set by a previous function as is the "$flightq" var.

          No they aren't.

          Functions have their own scope (man page: [man]variables.scope[/man]). Quick example:

          <?php
          
          $foo = 'Hello World!';
          
          function makeFoo2() {
            $foo2 = 'Hello World!';
          }
          
          function testme() {
            echo $foo;
            echo $foo2;
          }
          
          makeFoo2();
          testme();

          Result is:

          Notice: Undefined variable: foo in E:\php\test.php on line 10
          
          Notice: Undefined variable: foo2 in E:\php\test.php on line 11

          since neither $foo nor $foo2 were declared inside of (or passed into, e.g. via function arguments) testme()'s scope.

            Hi Brad,

            You are perfectly correct, my mistake.

            Again, many thanks.

              Write a Reply...