gpt4 book ai didi

php - 我在 mysql 查询中的 SQL 语法有什么问题?

转载 作者:行者123 更新时间:2023-11-28 23:38:26 24 4
gpt4 key购买 nike

我收到此消息并进行了研究,并提出了建议的更改,但仍然无法正常工作。我是一个初学者,所以请原谅我可能看起来一团糟。

这是错误信息:

您的 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。

关于php - 我在 mysql 查询中的 SQL 语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com