以下是我的代码,其中我有 2 个名为fyear
和的文本框ayear
。此代码不起作用,因为我无法将它与 MySQL 数据库连接。可能是什么问题,我该如何克服它?我正在使用 PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Home</title>
</head>
<body style="background-color:#708090;">
<?php
if (isset($_POST['submit']))
{
$mysqli= new mysqli("localhost","admin","admin", "nba" );
if($mysqli === false)
{
die("could not connect:" . mysqli_connect_error());
}
if ($inputError != true && empty($_POST['ayear']) )
{
echo 'ERROR: Please enter a valid year';
$inputError = true;
}
else
{
$ayear = $mysqli-> escape_string($_POST['ayear']);
}
if ($inputError != true && empty($_POST['fyear']) )
{
echo 'ERROR: Please enter a valid year';
$inputError = true;
}
else
{
$fyear = $mysqli-> escape_string($_POST['fyear']);
}
if ($inputError != true)
{
$sql="INSERT INTO year VALUES ('$ayear','$fyear')";
if ($mysqli-> query($sql)==true)
{
echo'Added';
}
else
{
echo"ERROR: Could not execute query : $sql. " . $mysqli-> error;
}
}
$mysqli-> close();
}
?>
<h1 style="font-family:Verdana;text-align:center;">National Board of Accrediation <br>of<br> Master of Computer Applications Programme</h1>
<br>
<div id="menu" style="background-color:#800000;height:25px;width:1000px">
<b><font "style="font-family:Verdana"><a href="part1.html" title="Institutional Summary"style="color: #000000">Part I</a>               <a href="part2.html"title="Departmental/Program Summary"style="color: #000000">Part II</a>               <a href="part3.html"title="Curricula,Syllabi,PEOs and POs"style="color: #000000">Part III</a>               <a href="part4.html" title="List of Documents to be made available during the visit"style="color: #000000">Part IV</a></font></b>
</div>
<form method="post" action="home.php"> <p> This Report is prepared for the Current Acadamic Year(<input type="text" size="9" name="ayear" id="ayear" onChange="a('ayear');">) and the Current Financial Year (<input type="text" size="9" name="fyear" id="fyear" onChange="a('fyear');">) on behalf of the Institution.</p>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
问题出在这部分:
else
{
$ayear = $mysqli-> escape_string($_POST['ayear']);
}
在“>”符号之后,所有内容都打印在网页中。可能的解决方案是什么?我已经浏览了这段代码,但无法找出错误所在。