簡體   English   中英

mysql查詢中的SQL語法有什么問題?

[英]What is wrong with my SQL syntax in mysql query?

我已經獲得了此消息並對其進行了研究,並提出了建議的更改,但仍然無法正常工作。 我是一個初學者,所以請原諒看起來很混亂的地方。

這是錯誤消息:

您的SQL語法有誤; 檢查與您的MySQL服務器版本相對應的手冊,以在第1行的'','','','','','','')'附近使用正確的語法

這是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;
}

?>

您缺少$country參數的開頭報價。

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

另外,請注意,不建議使用mysql_*函數。 改用mysqli_*函數或PDO。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM