简体   繁体   中英

What is wrong with my SQL syntax in mysql query?

I have gotten this message and researched it, and made suggested changes, and still can't get it to work. I'm a bit of a beginner, so please excuse what probably looks a mess.

Here is the error message:

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 1

Here is the php:

<?php
if (isset($_POST['submit'])) {
   if (empty($_POST["fname"])) {
     $fnameErr = "First name is required";
   } else {
     $fname = test_input($_POST["fname"]);
     // check if first name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
       $fnameErr = "Only letters and white space allowed";
     }
   }

   if (empty($_POST["lname"])) {
     $lnameErr = "Last name is required";
   } else {
     $lname = test_input($_POST["lname"]);
     // check if last name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
       $lnameErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["country"])) {
     $countryErr = "Country is required";
   } else {
     $country = test_input($_POST["country"]);
     // check if country is well-formed
     if (!preg_match("/^[a-zA-Z ]*$/",$country)) {
       $countryErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["arrdate"])) {
     $arrdate = "";
   } else {
     $arrdate = test_input($_POST["arrdate"]);
     // check if arrival date syntax is valid
     if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$arrdate)) {
       $arrdateErr = "Use Date Format mm/dd/yyyy"; 
     }
   }

   if (empty($_POST["retdate"])) {
     $retdate = "";
   } else {
     $retdate = test_input($_POST["retdate"]);
     // check if arrival date syntax is valid
     if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$retdate)) {
       $retdateErr = "Use Date Format mm/dd/yyyy"; 
     }
   }

   if (empty($_POST["type"])) {
     $typeERR = "Country is required";
   } else {
     $type = test_input($_POST["type"]);
     // check if type is well-formed
     if (!preg_match("/^[a-zA-Z ]*$/",$type)) {
       $typeErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["destination"])) {
     $destination = "";
   } else {
     $destination = test_input($_POST["destination"]);
     // check if destination is well-formed
     if (!preg_match("/^[a-zA-Z ]*$/",$destination)) {
       $destinationERR = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["missionary"])) {
     $missionary = "";
   } else {
     $missionary = test_input($_POST["missionary"]);
     // check if missionary is well-formed
     if (!preg_match("/^[a-zA-Z ]*$/",$missionary)) {
       $missionaryERR = "Only letters and white space allowed"; 
     }
   }

   $con = mysql_connect('********', '*****', '*******');
   mysql_select_db(dbbmdmi);


   $fname = mysql_real_escape_string($fname);
   $lname = mysql_real_escape_string($lname);
   $country = mysql_real_escape_string($country);
   $arrdate = mysql_real_escape_string($arrdate);
   $retdate = mysql_real_escape_string($retdate);
   $type = mysql_real_escape_string($type);
   $destination = mysql_real_escape_string($destination);
   $missionary = mysql_real_escape_string($missionary);


   if ($con) {
   $tmid = "$lname". "$arrdate";
   $capt = "$fname ". "$lname";
   $sql = "INSERT INTO Teams ". "(TeamID, Captain, CountryID, ArrDate, DepDate, Type, DestID, MsnyID) ". "VALUES ('$tmid', '$capt', $country', '$arrdate', '$retdate', '$type', '$destination', '$missionary')";
   $query = mysql_query($sql) or die(mysql_error());
   if($query)
   {
    header("Location:/teams.php");
    echo'Team successfully added!';
   }
   else 
   {
    echo 'problem occured adding record';
   }}
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>

You're missing the opening quote for the $country parameter.

VALUES ('$tmid', '$capt', $country', '$arrdate', '$retdate', '$type', '$destination', '$missionary')
                          ^
                         Here

Also, note that the mysql_* functions have been deprecated. Use the mysqli_* functions or PDO instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM