Sparrow Posted August 29, 2008 Share Posted August 29, 2008 I get this error message when I run this php script in my web browser: unexpected $end on line 51. What is the solution? here is the code (line 51 is bolded, last line) <?php session_start(); include 'session_checker.php'; session_checker(); include 'db.php'; //Convert posted info to variables $route_number = $_POST['route_number']; $username = $_POST['username']; //Get info from flights $sql = "SELECT * FROM flights WHERE route_number = $route_number"; $result = mysql_query($sql); $array = mysql_fetch_array($result); //Turn into variables $type = $array['type']; $dep_airport = $array['dep_airport']; $arr_airport = $array['arr_airport']; $aircraft = $array['aircraft']; $Rank = $array['Rank']; $dep_day = $array['dep_day']; $duration = $array['duration']; $route_number = $array['route_number']; // Enter info into booked_flights. $info2 = htmlspecialchars($info); $sql = mysql_query("INSERT INTO booked_flights (type, dep_airport, arr_airport, aircraft, Rank, dep_day, duration, route_number) VALUES('$type', '$dep_airport', '$arr_airport', '$aircraft', '$Rank', '$dep_day', '$duration', '$route_number')") or die (mysql_error()); if(!$sql){ echo 'There has been an error in booking your flight. Please try again.'; } else { $route_number = mysql_insert_id(); //Delete info from flights to prevent double booking mysql_query("DELETE FROM flights WHERE route_number = '$route_number'"); mysql_close($connection); include 'flight_booked.php'; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 You left off a } when you get the error. In this case, it's the } for the last else. Quote Link to comment Share on other sites More sharing options...
peranha Posted August 29, 2008 Share Posted August 29, 2008 This has no closing } to it { $route_number = mysql_insert_id(); //Delete info from flights to prevent double booking mysql_query("DELETE FROM flights WHERE route_number = '$route_number'"); mysql_close($connection); include 'flight_booked.php'; Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 29, 2008 Author Share Posted August 29, 2008 Thanks that problem is solved but now I get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/unitedai/public_html/book_flight.php on line 20 Duplicate entry '' for key 1 here is the new code: (line 20 is bolded) <?php session_start(); include 'session_checker.php'; session_checker(); include 'db.php'; //Convert posted info to variables $route_number = $_POST['route_number']; $username = $_POST['username']; //Get info from flights $sql = "SELECT * FROM flights WHERE route_number = $route_number"; $result = mysql_query($sql); $array = mysql_fetch_array($result); //Turn into variables $type = $array['type']; $dep_airport = $array['dep_airport']; $arr_airport = $array['arr_airport']; $aircraft = $array['aircraft']; $Rank = $array['Rank']; $dep_day = $array['dep_day']; $duration = $array['duration']; $route_number = $array['route_number']; // Enter info into booked_flights. $info2 = htmlspecialchars($info); $sql = mysql_query("INSERT INTO booked_flights (type, dep_airport, arr_airport, aircraft, Rank, dep_day, duration, route_number) VALUES('$type', '$dep_airport', '$arr_airport', '$aircraft', '$Rank', '$dep_day', '$duration', '$route_number')") or die (mysql_error()); if(!$sql){ echo 'There has been an error in booking your flight. Please try again.'; } else { $route_number = mysql_insert_id(); } //Delete info from flights to prevent double booking mysql_query("DELETE FROM flights WHERE route_number = '$route_number'"); mysql_close($connection); include 'flight_booked.php'; echo "<p> </p>"; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 PLEASE use code tags when posting code on the forums, as it makes it much more readable. Anyway, change: $result = mysql_query($sql); To: $result = mysql_query($sql) or die(mysql_error()); The error that you got usually means that your query failed, so this adds rudimentary error checking and should be used on ALL queries during the development stage. Show us the new output please. P.S: For anyone who wants to correct me, I know that it's better handled by exceptions or error handlers, but he's clearly just developing. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 29, 2008 Share Posted August 29, 2008 it's better handled by exceptions or error handlers Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 it's better handled by exceptions or error handlers Gah, foiled again. I would have gotten away with it too if it weren't for you meddling kids. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 29, 2008 Share Posted August 29, 2008 j/k. i don't use exceptions or error handlers. i still get paid a lot. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 j/k. i don't use exceptions or error handlers. i still get paid a lot. Me too. Except I do use exceptions when developing serious applications. =( Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 29, 2008 Share Posted August 29, 2008 in other news; dark night are awesome Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 in other news; dark night are awesome ...At least our posts were slightly more on topic. And I'm actually waiting for the guy to get back and post the new error. D: Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 29, 2008 Author Share Posted August 29, 2008 I fixed my problem by putting ' ' around $route_number. No errors anymore. However I noticed that the delete msql query that is supposed to delete the entriee from the flights table dosen't seem to work and that the username of the user who booked that flight isn't recorded. Any way to fix these problems? Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 29, 2008 Author Share Posted August 29, 2008 I modified the code slightly so that the username of the person who is logged in gets recorded on the booked flights table. But now I get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/unitedai/public_html/book_flight.php on line 36. Why? Here is the new code: (line 36 is bolded) <?php session_start(); include 'session_checker.php'; session_checker(); include 'db.php'; //Convert posted info to variables $route_number = $_POST['route_number']; $username = $_POST['username']; //Get info from flights $sql = "SELECT * FROM flights WHERE route_number = '$route_number'"; $result = mysql_query($sql) or die(mysql_error()); $array = mysql_fetch_array($result) or die(mysql_error()); //Turn into variables $type = $array['type']; $dep_airport = $array['dep_airport']; $arr_airport = $array['arr_airport']; $aircraft = $array['aircraft']; $Rank = $array['Rank']; $dep_day = $array['dep_day']; $duration = $array['duration']; $route_number = $array['route_number']; $username = $array['username']; // Enter info into booked_flights. $info2 = htmlspecialchars($info); $sql = mysql_query("INSERT INTO booked_flights (type, dep_airport, arr_airport, aircraft, Rank, dep_day, duration, route_number, username) VALUES('$type', '$dep_airport', '$arr_airport', '$aircraft', '$Rank', '$dep_day', '$duration', '$route_number', '.$_SESSION['$username'].')") or die (mysql_error()); // line 36 if(!$sql){ echo 'There has been an error in booking your flight. Please try again.'; } else { $route_number = mysql_insert_id(); } //Delete info from flights to prevent double booking mysql_query("DELETE FROM flights WHERE route_number = '$route_number'"); mysql_close($connection); include 'flight_booked.php'; echo "<p> </p>"; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 You exited out of the quotes with ' and not ". Look carefully at the quotes used for the mysql_query() input. Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 30, 2008 Author Share Posted August 30, 2008 I don't seee it? where? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 30, 2008 Share Posted August 30, 2008 $sql = mysql_query("INSERT INTO booked_flights (type, dep_airport, arr_airport, aircraft, Rank, dep_day, duration, route_number, username) VALUES('$type', '$dep_airport', '$arr_airport', '$aircraft', '$Rank', '$dep_day', '$duration', '$route_number', '{$_SESSION['$username']}')") or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 30, 2008 Author Share Posted August 30, 2008 Thank-you very much . One last problem. At the end of my code as you may have noticed I have a MSQL query to (after the entriee has been added to the booked flights table) delete the entriee from the flights table. however it dosen't seem to be working. because after I try the script out yes the "flight" is added to the booked flights table but it is not deleted from the flights table. Any idea why? here is the new code: <?php session_start(); include 'session_checker.php'; session_checker(); include 'db.php'; //Convert posted info to variables $route_number = $_POST['route_number']; $username = $_POST['username']; //Get info from flights $sql = "SELECT * FROM flights WHERE route_number = '$route_number'"; $result = mysql_query($sql) or die(mysql_error()); $array = mysql_fetch_array($result) or die(mysql_error()); //Turn into variables $type = $array['type']; $dep_airport = $array['dep_airport']; $arr_airport = $array['arr_airport']; $aircraft = $array['aircraft']; $Rank = $array['Rank']; $dep_day = $array['dep_day']; $duration = $array['duration']; $route_number = $array['route_number']; $username = $array['username']; // Enter info into booked_flights. $info2 = htmlspecialchars($info); $sql = mysql_query("INSERT INTO booked_flights (type, dep_airport, arr_airport, aircraft, Rank, dep_day, duration, route_number, username) VALUES('$type', '$dep_airport', '$arr_airport', '$aircraft', '$Rank', '$dep_day', '$duration', '$route_number', '{$_SESSION['username']}')") or die (mysql_error()); if(!$sql){ echo 'There has been an error in booking your flight. Please try again.'; } else { $route_number = mysql_insert_id(); } //Delete info from flights to prevent double booking mysql_query("DELETE FROM flights WHERE route_number = '$route_number'"); mysql_close($connection); include 'flight_booked.php'; echo "<p> </p>"; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Add the whole or die(mysql_error()); thing to the end of that query as well. Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 30, 2008 Author Share Posted August 30, 2008 I don't get any error it is just that it dosen't seem to run. I tested the query out in phpadmin btw and it works. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 I see. You're overwriting $route_number on this line: $route_number = mysql_insert_id() So it's using a different route number than the original one. I doubt that you actually want to use the mysql insert id for the second query. Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 30, 2008 Author Share Posted August 30, 2008 So I should take that line out? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 30, 2008 Share Posted August 30, 2008 Well if you want to use the same $route_number then yes. It never hurts to try something anyway. Quote Link to comment Share on other sites More sharing options...
Sparrow Posted August 30, 2008 Author Share Posted August 30, 2008 Thank-you very much everyone. Everything works fine now. 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.