首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql查询中的SQL语法有什么问题?

mysql查询中的SQL语法有什么问题?
EN

Stack Overflow用户
提问于 2016-02-01 02:24:44
回答 1查看 59关注 0票数 1

我已经得到了这条信息,并对它进行了研究,并提出了一些修改建议,但仍然无法让它发挥作用。我是个初学者,所以请原谅什么看起来可能一团糟。

以下是错误消息:

您的SQL语法出现了错误;请检查与MySQL服务器版本对应的手册,以获得在第1行中使用“”、“”等正确的语法。

下面是php:

代码语言:javascript
复制
<?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;
}

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-01 02:28:12

您缺少了$country参数的开头引号。

代码语言:javascript
复制
VALUES ('$tmid', '$capt', $country', '$arrdate', '$retdate', '$type', '$destination', '$missionary')
                          ^
                         Here

另外,请注意,mysql_*函数已被废弃。使用mysqli_*函数或PDO代替。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35122150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档