我是PHP和MySQL的新手,但不是一般的编码。我正在尝试将Access数据库迁移到MySQL中。
在提交表单信息并尝试向表中添加记录后,我收到以下错误:
用户访问被拒绝'' localhost'到数据库' FltLogbook'
为了构建表单,我访问数据库以提取下拉菜单的某些值。所有这一切都很好。点击"添加条目"按钮并使用Post方法,显示上述错误。
我很困惑,因为以前的查询运行得很好,但是一旦我尝试"添加"记录,我得到错误。上述错误似乎是匿名连接。 FWIW,我连接的用户名具有数据库的所有权限。我已经尝试在代码的表单构建部分中添加具有该用户名的记录并且成功了。您将在下面的代码中看到我已经注释掉了我的实际插入代码。我正在对数据类型进行故障排除,并尝试简化insert命令以消除导致错误的其他字段。当前插入命令代码在代码的表单构建部分中工作正常,所以我知道我有正确的代码,现在我只是为了解决连接问题。
在此网站和Google上进行大量搜索后,对上述错误的大多数回复通常都涉及授予用户权限。我不认为这是问题,因为我可以在代码的表单构建部分中添加记录。看来我是从经过身份验证的用户变为匿名用户,因为错误中没有传递用户名。我在代码的两个部分使用了connection_status(),它返回0(正常)。
任何帮助当然值得赞赏。干杯,希德
代码:
$conn = mysqli_connect("localhost", $_POST[user], $_POST[password], "FltLogbook");
if ($_POST[op] != "add") {
//Form not shown, show it
$display="<h1>Add Logbook Entry</h1>";
$CnxSt = connection_status();
$display .= "Connection Status: $CnxSt<br><br>";
$display .= "
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<strong>Details:</strong>
<table border=\"1\" width=\"520\">
<tr>
<td><input type=\"checkbox\" name=\"Sim\">Simulator</td>
<td colspan=\"2\">SimType: <select name=\"SimType\">
<option value=\"\"> NA </option>
<option value=\"Miltary Visual\"> Military Visual </option>
<option value=\"Military Non-Visual\"> Military Non-Visual </option>
<option value=\"ATD\"> ATD </option>
<option value=\"FTD\"> FTD </option>
<option value=\"FFS\"> FFS </option></select></td>
</tr>
<tr>
<td>Date: <input type=\"date\" name=\"FltDate\"></td>
<td>Aircraft: <select name=\"Aircraft\">
<option selected=\"selected\" value=\"26\">C-172M</option>";
$AcftQuery="SELECT Aircraft.AirKey,Aircraft.Aircraft, Aircraft.ME, Aircraft.Complex, Aircraft.HighPerf
FROM Aircraft";
$AcftResult=mysqli_query($conn, $AcftQuery) or die ("Aircraft Query Failed");
While ($AcftRow=mysqli_fetch_array($AcftResult)) {
$AcftKey=$AcftRow['AirKey'];
$AcftAircraft=$AcftRow['Aircraft'];
$AcftME=$AcftRow['ME'];
$AcftComplex=$AcftRow['Complex'];
$AcftHP=$AcftRow['HighPerf'];
$display .= "<option value=\"$AcftKey\"> $AcftAircraft </option>";
}
$display .= "
</select>
</td>
<td>Aircraft ID: <input type=\"text\" name=\"ID\" size=\"8\"></td>
</tr>
<tr>
<td>From: <select name=\"From\">
<option selected=\"selected\" value=\"PVG\">PVG</option>";
$ArfldQuery="SELECT AirfieldID FROM Airfields ORDER BY AirfieldID";
$ArfldResult=mysqli_query($conn, $ArfldQuery) or die ("Airfield Query Failed");
While ($ArfldRow=mysqli_fetch_array($ArfldResult)) {
$Arfld=$ArfldRow['AirfieldID'];
$display .= "<option value=\"$Arfld\"> $Arfld </option>";
}
$display .= "
</select>
</td>
<td>To: <select name=\"To\">
<option selected=\"selected\" value=\"PVG\">PVG</option>";
$ArfldQuery="SELECT AirfieldID FROM Airfields ORDER BY AirfieldID";
$ArfldResult=mysqli_query($conn, $ArfldQuery) or die ("Airfield Query Failed");
While ($ArfldRow=mysqli_fetch_array($ArfldResult)) {
$Arfld=$ArfldRow['AirfieldID'];
$display .= "<option value=\"$Arfld\"> $Arfld </option>";
}
$display .= "
</select>
</td>
<td>Leg: <select name=\"Leg\">
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
<option value=\"6\">6</option>
</select>
</td>
</tr>
</table><br>
<strong>Flight Time Info:</strong>
<table border=\"1\" width=\"520\">
<tr>
<td>TPT: <input type=\"text\" name=\"TPT\" size=\"4\"></td>
<td>PIC: <input type=\"text\" name=\"PIC\" size=\"4\"></td>
<td>SIC: <input type=\"text\" name=\"SIC\" value=\"0.0\" size=\"4\"></td>
</tr>
<tr>
<td>Solo: <input type=\"text\" name=\"Solo\" value=\"0.0\" size=\"4\"></td>
<td colspan=\"2\">Dual Received: <input type=\"text\" name=\"Dual\" value=\"0.0\" size=\"4\"></td>
</tr>
<tr>
<td>Cross Country: <input type=\"text\" name=\"XC\" value=\"0.0\" size=\"4\"></td>
<td><input type=\"checkbox\" name=\"FARXCntry\" size=\"4\">FAR X-Cntry</td>
<td><input type=\"checkbox\" name=\"ATPXCntry\" size=\"4\">ATP X-Cntry</td>
</tr>
<tr>
<td>Single Engine: <input type=\"text\" name=\"SE\" size=\"4\"></td>
<td>Multiengine: <input type=\"text\" name=\"ME\" size=\"4\"></td>
<td>Night: <input type=\"text\" name=\"NT\" value=\"0.0\" size=\"4\"></td>
</tr>
<tr>
<td>Complex: <input type=\"text\" name=\"Complex\" value=\"0.0\" size=\"4\"></td>
<td colspan=\"2\">High Performance: <input type=\"text\" name=\"HP\" value=\"0.0\" size=\"4\"></td>
</tr>
<tr>
<td>Instrument: <input type=\"text\" name=\"Inst\" value=\"0.0\" size=\"4\"></td>
<td colspan=\"2\">Sim Instrument: <input type=\"text\" name=\"SimInst\" value=\"0.0\" size=\"4\"></td>
</tr>
<tr>
<td>Instructor: <input type=\"text\" name=\"Instructor\" value=\"0.0\" size=\"4\"></td>
<td>Pre-Post: <input type=\"text\" name=\"PrePost\" value=\"0.0\" size=\"4\"></td>
<td>Ground: <input type=\"text\" name=\"Ground\" value=\"0.0\" size=\"4\"></td>
</tr>
</table><br>
<strong>Actual Approaches:</strong>
<table border=\"1\" width=\"520\">
<tr>
<td>Precision: <input type=\"text\" name=\"PrecAct\" value=\"0\" size=\"3\"></td>
<td>Non-Precision: <input type=\"text\" name=\"NonPrecAct\" value=\"0\" size=\"3\"></td>
</tr>
</table><br>
<strong>Simulated Approaches:</strong>
<table border=\"1\" width=\"520\">
<tr>
<td>Precision: <input type=\"text\" name=\"PrecSim\" value=\"0\" size=\"3\"></td>
<td>Non-Precision: <input type=\"text\" name=\"NonPrecSim\" value=\"0\" size=\"3\"></td>
</tr>
</table><br>
<strong>Landings:</strong>
<table border=\"1\" width=\"520\">
<tr>
<td>Day Landings: <input type=\"text\" name=\"DayLdg\" size=\"3\"></td>
<td>Night Landings: <input type=\"text\" name=\"NtLdg\" size=\"3\"></td>
</tr>
</table><br>
<strong>Remarks:</strong><br>
<textarea name=\"Remarks\" rows=\"5\" cols=\"70\"></textarea>
<input type=\"hidden\" name=\"op\" value=\"add\">
<p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
</FORM>";
} else if ($_POST[op] == "add") {
if ($_POST[Sim] == "") {
$_POST[Sim] = "0";
} else {
$_POST[Sim] = "1";
}
if ($_POST[FARXCntry] == "") {
$_POST[FARXCntry] = "0";
} else {
$_POST[FARXCntry] = "1";
}
if ($_POST[ATPXCntry] == "") {
$_POST[ATPXCntry] = "0";
} else {
$_POST[ATPXCntry] = "1";
}
// $add_flt = "INSERT INTO FlightData (Date, MakeModel, AircraftID, Simulator, SimType, AfldFrom, AfldTo,
// Leg, TPT, PIC, SIC, Solo, Dual, Night, XCntry, SE, ME, Complex, HighPerf, Inst_Act, Inst_Sim,
// Instructor, PrePost, Ground, Prec_Act, Prec_Sim, NonPrec_Act, NonPrec_Sim, DCL, NCL,
// FAR_XCntry, ATP_XCntry, Remarks)
// VALUES ('$_POST[FltDate]', '$_POST[Aircraft]', '$_POST[ID],$_POST[Sim]', '$_POST[SimType]', '$_POST[From]',
// '$_POST[To]', '$_POST[Leg]', '$_POST[TPT]', '$_POST[PIC]', '$_POST[SIC]', '$_POST[Solo]', '$_POST[Dual]',
// '$_POST[NT]', '$_POST[XC]', '$_POST[SE]', '$_POST[ME]', '$_POST[Complex]', '$_POST[HP]', '$_POST[Inst]',
// '$_POST[SimInst]', '$_POST[Instructor]', '$_POST[PrePost]', '$_POST[Ground]', '$_POST[PrecAct]',
// '$_POST[PrecSim]', '$_POST[NonPrecAct]', '$_POST[NonPreSim]', '$_POST[DayLdg]', '$_POST[NtLdg]',
// '$_POST[FARXCntry]', '$_POST[ATPXCntry]', '$_POST[Remarks]')";
$add_flt = "INSERT INTO FlightData (Date, MakeModel) VALUES ('$_POST[FltDate]', '$_POST[Aircraft]')";
mysqli_query($conn, $add_flt) or die('Error: '. mysqli_connect_error());
$display = "<h1>Entry Added</h1>
<p>Your entry was added.</p><br>
<p>$_POST[FltDate] | $_POST[Aircraft] </p>";
// $CnxSt = connection_status();
// $display .= "Connection Status: $CnxSt<br><br>";
// $display .= "$user / $pwd<br>$add_flt<br>";
// $display .= "$_POST[FltDate] / $_POST[Aircraft] / $_POST[ID] / $_POST[Sim] / $_POST[FARXCntry] / $_POST[ATPXCntry]";
}
?>
<html>
<head>
<title>Add Logbook Entry></title>
</head>
<body>
<?php echo $display; ?>
</body>
</html>
答案 0 :(得分:-1)
您必须在第1行检查您的mysql用户名和密码
如果username =“root”&amp;密码为空
$ conn = mysqli_connect(“localhost”,“root”,“”,“FltLogbook”);