Ninjakreborn Posted November 20, 2006 Share Posted November 20, 2006 I have been trying to get this to work for awhile.I have a query that is giving me a syntax error, I think because of my sessions.[code]<?phpif (!isset($_SESSION['temp']['flightnumber'])) { $_SESSION['temp']['flightnumber'] = "none";}if (!isset($_SESSION['temp']['flightnumberselect'])) { $_SESSION['temp']['flightnumberselect'] = "none";}if (!isset($_SESSION['temp']['actiontaken']['abortedtakeoff'])) { $_SESSION['temp']['actiontaken']['abortedtakeoff'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['canceledflight'])) { $_SESSION['temp']['actiontaken']['canceledflight'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['declaredemergency'])) { $_SESSION['temp']['actiontaken']['declaredemergency'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['delayedflight'])) { $_SESSION['temp']['actiontaken']['delayedflight'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['diversion'])) { $_SESSION['temp']['actiontaken']['diversion'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['goaround'])) { $_SESSION['temp']['actiontaken']['goaround'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['groundaircraftatoutstation'])) { $_SESSION['temp']['actiontaken']['groundaircraftatoutstation'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['inflightshutdown'])) { $_SESSION['temp']['actiontaken']['inflightshutdown'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['missedapproach'])) { $_SESSION['temp']['actiontaken']['missedapproach'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['returnaircrafttomaintenance'])) { $_SESSION['temp']['actiontaken']['returnaircrafttomaintenance'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['returntogate'])) { $_SESSION['temp']['actiontaken']['returntogate'] = "no";}if (!isset($_SESSION['temp']['actiontaken']['turnback'])) { $_SESSION['temp']['actiontaken']['turnback'] = "no";} $insert = "INSERT INTO eventreport (name, t_number, flighttype, aircrafttype, aircraftnumber, eventdate, eventtime, flightnumber, flightnumberselect, scheduleddepartureairport, scheduledarrivalairport, captain, firstofficer, flightattendant, additionalcrewmembers, weather, flightplan, pax, lapchildren, actiontaken_abortedtakeoff, actiontaken_canceledflight, actiontaken_declaredemergency, actiontaken_delayedflight, actiontaken_diversion, actiontaken_goaround, actiontaken_groundaircraftatoutstation, actiontaken_inflightshutdown, actiontaken_missedapproach, actiontaken_returnaircrafttomaintenance, actiontaken_returntogate, actiontaken_turnback) VALUES (" . $_SESSION['temp']['name'] . ", " . $_SESSION['temp']['t-number'] . ", " . $_SESSION['temp']['flighttype'] . ", " . $_SESSION['temp']['aircrafttype'] . ", " . $_SESSION['temp']['aircraftnumber'] . ", " . $_SESSION['temp']['eventdate'] . ", " . $_SESSION['temp']['eventtime'] . ", " . $_SESSION['temp']['flightnumber'] . ", " . $_SESSION['temp']['flightnumberselect'] . ", " . $_SESSION['temp']['scheduleddepartureairport'] . ", " . $_SESSION['temp']['scheduledarrivalairport'] . ", " . $_SESSION['temp']['captain'] . ", " . $_SESSION['temp']['firstofficer'] . ", " . $_SESSION['temp']['flightattendant'] . ", " . $_SESSION['temp']['additionalcrewmembers'] . ", " . $_SESSION['temp']['weather'] . ", " . $_SESSION['temp']['flightplan'] . ", " . $_SESSION['temp']['pax'] . ", " . $_SESSION['temp']['lapchildren'] . ", " . $_SESSION['temp']['actiontaken']['abortedtakeoff'] . ", " . $_SESSION['temp']['actiontaken']['canceledflight'] . ", " . $_SESSION['temp']['actiontaken']['declaredemergency'] . ", " . $_SESSION['temp']['actiontaken']['delayedfligh'] . ", " . $_SESSION['temp']['actiontaken']['diversion'] . ", " . $_SESSION['temp']['actiontaken']['goaround'] . ", " . $_SESSION['temp']['actiontaken']['grountaircraftatoutstation'] . ", " . $_SESSION['temp']['actiontaken']['inflightshutdown'] . ", " . $_SESSION['temp']['actiontaken']['missedapproach'] . ", " . $_SESSION['temp']['actiontaken']['returnaircrafttomaintenance'] . ", " . $_SESSION['temp']['actiontaken']['returntogate'] . ", " . $_SESSION['temp']['actiontaken']['turnback'] . ")"; if (mysql_query($insert)) { echo "Record entered successfully.<br />"; }else { echo "There was a problem inserting the record, please hit the back button "; echo "and try again.<br />"; echo mysql_error(); echo $insert; } }else { echo "You came to this page from an incorrect location.<br />"; }?>[/code]This is giving me an error instead of working.It's returning[quote]There was a problem inserting the record, please hit the back button and try again.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 '121, ATR42, N14834, 12/05/1992, 1231, dfg, none, sdfgfds, okm, lkm, lkm, lkm, lk' at line 1INSERT INTO eventreport (name, t_number, flighttype, aircrafttype, aircraftnumber, eventdate, eventtime, flightnumber, flightnumberselect, scheduleddepartureairport, scheduledarrivalairport, captain, firstofficer, flightattendant, additionalcrewmembers, weather, flightplan, pax, lapchildren, actiontaken_abortedtakeoff, actiontaken_canceledflight, actiontaken_declaredemergency, actiontaken_delayedflight, actiontaken_diversion, actiontaken_goaround, actiontaken_groundaircraftatoutstation, actiontaken_inflightshutdown, actiontaken_missedapproach, actiontaken_returnaircrafttomaintenance, actiontaken_returntogate, actiontaken_turnback) VALUES (west, asdasd, Part 121, ATR42, N14834, 12/05/1992, 1231, dfg, none, sdfgfds, okm, lkm, lkm, lkm, lkm, lkm, VFR, kjnl, km, Aborted Takeoff, no, no, , no, Go Around, , no, no, no, no, no)[/quote]I tried writing the query multiple way's, so I started using concatenations last, I tried putting sessions just in the string, adn everything else, then I finally had to create those things above the querie on different variables I knew that might be empty, if there empty then I name it no, because I knew that was one of the problem's, but after all that debugging and fixing there are still syntax problems somewhere, and other than that, that's only have to query, I cut it in half to test it ebfroe doing the whole thing, the query qill actually double when I am done, I just wanted to test if it was going to work up to this point and it'sn ot. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 20, 2006 Share Posted November 20, 2006 I don't see a "session_start()" statement in your code. Do you have one at the start of this script?Ken Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 20, 2006 Author Share Posted November 20, 2006 Yes it's in all pages, and when I echo out the insert statement it is showing all the variables, as I showed in the output, it's strange why it's not work?Any advice? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 20, 2006 Share Posted November 20, 2006 You need single quotes around all string values in the SQL statement. You also should be using the [url=http://www.php.net/mysql_real_escape_string]mysql_real_escape_string()[/url] function on all string values to ensure there are nothing in the values that could cause problems.I would use PHP to build the query, since doing it by hand can get tedious and error prone.This is one way of building the query using the alternative syntax for the insert statement:[code]<?php$dbkeys = array('name', 't_number', 'flighttype', 'aircrafttype', 'aircraftnumber', 'eventdate', 'eventtime', 'flightnumber','flightnumberselect', 'scheduleddepartureairport', 'scheduledarrivalairport', 'captain', 'firstofficer', 'flightattendant','additionalcrewmembers', 'weather', 'flightplan', 'pax', 'lapchildren', 'actiontaken_abortedtakeoff', 'actiontaken_canceledflight','actiontaken_declaredemergency', 'actiontaken_delayedflight', 'actiontaken_diversion', 'actiontaken_goaround','actiontaken_groundaircraftatoutstation', 'actiontaken_inflightshutdown', 'actiontaken_missedapproach','actiontaken_returnaircrafttomaintenance', 'actiontaken_returntogate', 'actiontaken_turnback');$qtmp = array();foreach($dbkeys as $key) $qtmp[] = $key . " = '" . mysql_real_escape_string($_SESSION['temp'][$key]) . "'";$insert = "INSERT INTO eventreport set " . implode(', ',$qtmp);if (mysql_query($insert)) echo "Record entered successfully.<br />";else { echo "There was a problem inserting the record, please hit the back button "; echo "and try again.<br />"; echo mysql_error()."<br />"; echo $insert;}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 20, 2006 Author Share Posted November 20, 2006 I used mysql_real_escape_string on all variables while I was working with validation and setting the sessions.I did that one each and every variable.I am learning a lot of new stuff1. more with functions2. Array's a lot more than i use to 3. Readyh pre-built functions ( I use to have some problems reading some of them)plus countless other things along with this one project. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 20, 2006 Author Share Posted November 20, 2006 I tried it that way, it worked some but had troubles with the arrays it wasn't reading them, so I redid it the other way based on what you said[code]<?php $insert = "INSERT INTO eventreport (name, t_number, flighttype, aircrafttype, aircraftnumber, eventdate, eventtime, flightnumber, flightnumberselect, scheduleddepartureairport, scheduledarrivalairport, captain, firstofficer, flightattendant, additionalcrewmembers, weather, flightplan, pax, lapchildren, actiontaken_abortedtakeoff, actiontaken_canceledflight, actiontaken_declaredemergency, actiontaken_delayedflight, actiontaken_diversion, actiontaken_goaround, actiontaken_groundaircraftatoutstation, actiontaken_inflightshutdown, actiontaken_missedapproach, actiontaken_returnaircrafttomaintenance, actiontaken_returntogate, actiontaken_turnback) VALUES ('$_SESSION[temp][name]', '$_SESSION[temp][t-number]', '$_SESSION[temp][flighttype]', '$_SESSION[temp][aircrafttype]', '$_SESSION[temp][aircraftnumber]', '$_SESSION[temp][eventdate]', '$_SESSION[temp][eventtime]', '$_SESSION[temp][flightnumber]', '$_SESSION[temp][flightnumberselect]', '$_SESSION[temp][scheduleddepartureairport]', '$_SESSION[temp][scheduledarrivalairport]', '$_SESSION[temp][captain]', '$_SESSION[temp][firstofficer]', '$_SESSION[temp][flightattendant]', '$_SESSION[temp][additionalcrewmembers]','$_SESSION[temp][weather]', '$_SESSION[temp][flightplan]', '$_SESSION[temp][pax]', '$_SESSION[temp][lapchildren]','$_SESSION[temp][actiontaken][abortedtakeoff]', '$_SESSION[temp][actiontaken][canceledflight]', '$_SESSION[temp][actiontaken][declaredemergency]', '$_SESSION[temp][actiontaken][delayedfligh]', '$_SESSION[temp][actiontaken][diversion]', '$_SESSION[temp][actiontaken][goaround]', '$_SESSION[temp][actiontaken][grountaircraftatoutstation]', '$_SESSION[temp][actiontaken][inflightshutdown]', '$_SESSION[temp][actiontaken][missedapproach]', '$_SESSION[temp][actiontaken][returnaircrafttomaintenance]', '$_SESSION[temp][actiontaken][returntogate]', '$_SESSION[temp][actiontaken][turnback]')"; if (mysql_query($insert)) { echo "Record entered successfully.<br />"; }else { echo "There was a problem inserting the record, please hit the back button "; echo "and try again.<br />"; echo mysql_error(); echo $insert; }?>[/code]It fixed my syntax error's, but it moved me into another error, now all it's returning for the array's is array[something]inside the database, it's not databasing the variable, but some wierd name instead, called array.Before I was getting a syntax errors but my array values were coming through, now there is not syntax errors but hte arrays are not coming through? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 20, 2006 Share Posted November 20, 2006 [code]<?php $arr = Array(); echo $arr;?>[/code]Will output something like:arrayThe same thing is happening with your query. You can't just give it an array, you have to give it the values inside the array. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.