jasraj Posted August 12, 2009 Share Posted August 12, 2009 I have a syntax error i been looking at the same code for the last hour and i still cant see the problem i keep getting: 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 3 and i don't have a clue why. could you please help thanks alot. <?php session_start(); include('../library/connectdb.php'); include('../library/selectdb.php'); $gigid = $_GET['id']; $notickets = $_POST['notickets']; $customer_id = $_SESSION['customerid']; $query = "INSERT INTO sales (sale_id, gig_id, customer_id, notickets) VALUES ('', '$gigid', '$customer_id', '$notickets')"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't insert a new customer"; echo mysql_error(); } //grab field names from cardholder form and insert date into table. $cardtype = $_POST['cardtype']; $card_number = $_POST['card_number']; $cardholder= $_POST['cardholder']; $expiry_date = $_POST['expiry_date']; $query = "INSERT INTO cardholder (card_id, cardtype, card_number, cardholder, expiry_date) VALUES ('','$cardtype', '$card_number','$cardholder','$expiry_date')"; if (mysql_query($query)) { // Success! $idcard = mysql_insert_id(); } else { // Display error message. echo "Couldn't insert a new card holder"; echo mysql_error(); } // update the customers table with the card id which i have made above. $query = "UPDATE customers SET card_id = '$idcard' WHERE customer_id = '$customer_id'"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't add card_id to customers table."; echo mysql_error(); } // grab gigs table, to put some filds into my email.. $result = "SELECT * FROM gigs LEFT JOIN artists ON gigs.artist_id=artists.artist_id LEFT JOIN venues ON gigs.venue_id=venues.venue_id WHERE gig_id = $gigid"; $result = mysql_query( $result ) or die ( mysql_error() ); $row = mysql_fetch_assoc($result) or die ( mysql_error() ); $current_artistname = $row['artistname']; $current_soldtickets = $row['soldtickets']; $current_venuename = $row['venuename']; $result = "SELECT * FROM customers LEFT JOIN gb_login ON customers.customer_id=gb_login.customer_id WHERE customer_id = $customer"; $result = mysql_query( $result ) or die ( mysql_error() ); $row = mysql_fetch_assoc($result) or die ( mysql_error() ); $current_email = $row['email']; //email $message = "Thank you, $forename for buy $notickets tickets for $current_artistname playing at $current_venuename."; ini_set ("sendmail_from","[email protected]"); mail ("$customer_email", "Thanks for joining Gig Bookers","$message"); // grab gigs table, run the results then fetch price from the table, then times the price by the number of tickets. Then update carholder table and add the amout charged for all tickets brought by the customer. $result = "SELECT * FROM gigs WHERE gig_id = $current_gigid"; $result = mysql_query( $result ) or die ( mysql_error() ); $row = mysql_fetch_assoc ($result); $current_price = $row['price']; $total = ($current_price * $notickets); $query = "UPDATE cardholder SET priceamount = '$total' WHERE card_id = '$idcard'"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't add amount charged to cardholders table."; echo mysql_error(); } // Add the numbers for tickets sold for that gig, then update the gigs table. $totalticketsold = ($sold_tickets + $notickets); $query = "UPDATE gigs SET soldtickets = '$totalticketsold' WHERE gig_id = '$current_gigid'"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't add amount charged to cardholders table."; echo mysql_error(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Gig Bookers</title> <link rel="stylesheet" type="text/css" href="../library/style.css"/> <!-- <meta http-equiv="refresh" content="2;URL=email.php"> --> </head> <body> <div id="wrap"> <div id="header"> <h1><a href="index">Gig Bookers</a></h1> <h2>Online Event Ticket Booking</h2> </div> <!-- End Header --> <div id="top"></div><!-- End Top --> <div id="menu"> <?php include("naviagation.php") ?> </div> <!-- End Menu --> <div id="content"> <div class="left"> <p>You have added a Gig successfully</p> </div> <!-- End Left --> <div class="right"> <?php include("rightside.php") ?> </div> <!-- End Right --> <div style="clear: both;"></div><!-- End Clear --> </div> <!-- End --> <div id="bottom"></div> <!-- End Bottem --> <div id="footer"> <p>Designed by Jasraj Gill</p> </div> <!-- End Footer --> </div> <!-- End Wrap / All Div Tags --> </body> </html> Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/ Share on other sites More sharing options...
TeNDoLLA Posted August 12, 2009 Share Posted August 12, 2009 If your id fields are auto_increment then you don't need to include it in your SQL clauses at all. Try removing the id fields and values from the insert. Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/#findComment-896794 Share on other sites More sharing options...
PFMaBiSmAd Posted August 12, 2009 Share Posted August 12, 2009 I'm going to take a wild guess that the query that is failing is the following one and that $customer does not exist (it's not anywhere in the code you posted) - $result = "SELECT * FROM customers LEFT JOIN gb_login ON customers.customer_id=gb_login.customer_id WHERE customer_id = $customer"; Since we don't know what you intended, you would need to proof read your logic and put the correct variable name in where $customer is now. You should also be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your php.ini to get php to help you. there would have been an undefined error message about the $customer variable that would have help point you in the right direction of finding why the code is producing an sql error. Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/#findComment-896798 Share on other sites More sharing options...
jasraj Posted August 12, 2009 Author Share Posted August 12, 2009 hey TeNDoLLA I just tried that but that was not the problem Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/#findComment-896800 Share on other sites More sharing options...
jasraj Posted August 12, 2009 Author Share Posted August 12, 2009 Hey PFMaBiSmAd i have now changed the variable to the right variable. But now i'm getting this error Column 'customer_id' in where clause is ambiguous <?php session_start(); include('../library/connectdb.php'); include('../library/selectdb.php'); $gigid = $_GET['id']; $notickets = $_POST['notickets']; $customer_id = $_SESSION['customerid']; $query = "INSERT INTO sales (sale_id, gig_id, customer_id, notickets) VALUES ('', '$gigid', '$customer_id', '$notickets')"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't insert a new customer"; echo mysql_error(); } //grab field names from cardholder form and insert date into table. $cardtype = $_POST['cardtype']; $card_number = $_POST['card_number']; $cardholder= $_POST['cardholder']; $expiry_date = $_POST['expiry_date']; $query = "INSERT INTO cardholder (card_id, cardtype, card_number, cardholder, expiry_date) VALUES ('','$cardtype', '$card_number','$cardholder','$expiry_date')"; if (mysql_query($query)) { // Success! $idcard = mysql_insert_id(); } else { // Display error message. echo "Couldn't insert a new card holder"; echo mysql_error(); } // update the customers table with the card id which i have made above. $query = "UPDATE customers SET card_id = '$idcard' WHERE customer_id = '$customer_id'"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't add card_id to customers table."; echo mysql_error(); } // grab gigs table, to put some filds into my email.. $result = "SELECT * FROM gigs LEFT JOIN artists ON gigs.artist_id=artists.artist_id LEFT JOIN venues ON gigs.venue_id=venues.venue_id WHERE gig_id = $gigid"; $result = mysql_query( $result ) or die ( mysql_error() ); $row = mysql_fetch_assoc($result) or die ( mysql_error() ); $current_artistname = $row['artistname']; $current_soldtickets = $row['soldtickets']; $current_venuename = $row['venuename']; $result = "SELECT * FROM customers LEFT JOIN gb_login ON customers.customer_id=gb_login.customer_id WHERE customer_id = $customer_id"; $result = mysql_query( $result ) or die ( mysql_error() ); $row = mysql_fetch_assoc($result) or die ( mysql_error() ); $current_email = $row['email']; //email $message = "Thank you, $forename for buy $notickets tickets for $current_artistname playing at $current_venuename."; ini_set ("sendmail_from","[email protected]"); mail ("$customer_email", "Thanks for joining Gig Bookers","$message"); // grab gigs table, run the results then fetch price from the table, then times the price by the number of tickets. Then update carholder table and add the amout charged for all tickets brought by the customer. $result = "SELECT * FROM gigs WHERE gig_id = $current_gigid"; $result = mysql_query( $result ) or die ( mysql_error() ); $row = mysql_fetch_assoc ($result); $current_price = $row['price']; $total = ($current_price * $notickets); $query = "UPDATE cardholder SET priceamount = '$total' WHERE card_id = '$idcard'"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't add amount charged to cardholders table."; echo mysql_error(); } // Add the numbers for tickets sold for that gig, then update the gigs table. $totalticketsold = ($sold_tickets + $notickets); $query = "UPDATE gigs SET soldtickets = '$totalticketsold' WHERE gig_id = '$current_gigid'"; if (mysql_query($query)) { // Success! } else { // Display error message. echo "Couldn't add amount charged to cardholders table."; echo mysql_error(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Gig Bookers</title> <link rel="stylesheet" type="text/css" href="../library/style.css"/> <!-- <meta http-equiv="refresh" content="2;URL=email.php"> --> </head> <body> <div id="wrap"> <div id="header"> <h1><a href="index">Gig Bookers</a></h1> <h2>Online Event Ticket Booking</h2> </div> <!-- End Header --> <div id="top"></div><!-- End Top --> <div id="menu"> <?php include("naviagation.php") ?> </div> <!-- End Menu --> <div id="content"> <div class="left"> <p>You have added a Gig successfully</p> </div> <!-- End Left --> <div class="right"> <?php include("rightside.php") ?> </div> <!-- End Right --> <div style="clear: both;"></div><!-- End Clear --> </div> <!-- End --> <div id="bottom"></div> <!-- End Bottem --> <div id="footer"> <p>Designed by Jasraj Gill</p> </div> <!-- End Footer --> </div> <!-- End Wrap / All Div Tags --> </body> </html> Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/#findComment-896813 Share on other sites More sharing options...
p2grace Posted August 12, 2009 Share Posted August 12, 2009 Not sure if this is directly related to your issue or not (that would depend on what's being posted), regardless you should clean your variables and protect the query from sql injection. $var = mysql_real_escape_string($var); Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/#findComment-896817 Share on other sites More sharing options...
TeNDoLLA Posted August 12, 2009 Share Posted August 12, 2009 The ambigous error is because you have used JOIN and both tables has the same named field. So when you just use directly in WHERE field name 'cusstomer_id' the server does not know which table you mean the one you joined or the one before it. So you have to give aliases to the tables and point which table's customer_id field you want to use in WHERE. <?php $result = "SELECT * FROM customers c LEFT JOIN gb_login gb ON c.customer_id=gb.customer_id WHERE c.customer_id = $customer_id"; Link to comment https://forums.phpfreaks.com/topic/170002-syntax-error/#findComment-896822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.