我正在编写一个访问本地SQLite数据库的PHP / HTML页面,并显示/更改该数据库中的信息。我在连接到数据库时遇到问题,所以我按照here的说明进行操作,现在SEEMS正在连接。现在我遇到的问题是,当我运行代码时,从下拉菜单中选择一个选项,然后单击“提交”,我会收到以下错误:
Notice: Undefined index: formFlowerNameQuery in C:\Users\Aubrey\PhpstormProjects\Assignment5\src\main.php on line 45
Fatal error: Call to a member function query() on null in C:\Users\Aubrey\PhpstormProjects\Assignment5\src\main.php on line 46
我不知道为什么会出现第一个错误,因为我在点击提交按钮之前选择了一个选项。第二个也让我感到困惑,因为数据库不应该为空。这是我正在运行的HTML和PHP。这也是我对这两种语言的第一次体验,因此我可能会通过谷歌搜索我想要做的事情将它们整合在一起。该错误与名为“最新瞄准”的第一部分有关。
<!DOCTYPE html>
<html>
<head>
<title>Assignment 5</title>
</head>
<?php
global $database;
function openDatabase(){
try{
if($this->database==null){
$this->database =new PDO("sqlite:flowers.db","","",array(
PDO::ATTR_PERSISTENT => true
));
}
return $this->database;
}catch(PDOException $e){
print "Error: ".$e->getMessage();
}
}
?>
<body>
<header>
<h1>SSWC Database</h1>
</header>
<hr noshade width=75% align=left>
<h3>Latest Sightings</h3>
<p>To view the last 10 sightings of a flower, select it from the dropdown menu below and click submit.<p>
<p>
<select name="formFlowerNameQuery">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<form action="main.php" method="post">
<input type="submit" name="submitQuery" value="Submit" />
</form>
<?php
if(isset($_POST['submitQuery'])){
querySubmission($database);
}
function querySubmission($database){
$varFlowerName = $_POST['formFlowerNameQuery'];
$result = $database->query("SELECT PERSON, LOCATION, SIGHTED
FROM SIGHTINGS
WHERE NAME = $varFlowerName
ORDER BY SIGHTED DESC
LIMIT 10");
while($row = $result->fetchArray(SQLITE3_ASSOC)){
echo "NAME: " . $row['NAME'] . "\n";
echo "PERSON: " . $row['PERSON'] . "\n";
echo "LOCATION: " . $row['LOCATION'] . "\n";
echo "SIGHTED ON: " . $row['SIGHTED'] . "\n";
}
}
?>
<hr noshade width=75% align=left>
<h3>Update Flower Information</h3>
<p>To update information on a specific flower, select the flower from the dropdown menu <br />
and input the new information into the provided text boxes. Click submit to update.</p>
<p>
<select name="formFlowerNameUpdate">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<form>
Genus:<br>
<input type="text" name="genus"><br>
Species:<br>
<input type="text" name="species"><br>
Common Name<br>
<input type="text" name="comname"><br>
<br />
</form>
<button name="submitUpdate">Submit</button>
<hr noshade width=75% align=left>
<h3>Add Sighting</h3>
<p>To add a new flower sighting to the database, select the flower from the drop down menu <br />
and input the sighting information. Click submit to add the sighting to the database.</p>
<p>
<select name="formFlowerNameInsert">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<p>
<select name="formPerson">
<option value="">Select Person...</option>
<option value="Jennifer">Jennifer</option>
<option value="Maria">Maria</option>
<option value="Michael">Michael</option>
</select>
</p>
<p>
<select name="formLocation">
<option value="">Select Location...</option>
<option value="Scodie Mountains">Scodie Mountains</option>
<option value="Grouse Meadow">Grouse Meadow</option>
<option value="Steve Spring">Steve Spring</option>
</select>
</p>
<p>
<input type="date" name="dateSighted">
</p>
<button name="submitInsert">Submit</button>
</body>
</html>
编辑:这两个错误都是通过以下@ Phil和@ IncredibleHat的评论中的步骤修复的。
答案 0 :(得分:-1)
这是因为您将select
字段放在form
<p>
<select name="formFlowerNameQuery">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<form action="main.php" method="post">
<input type="submit" name="submitQuery" value="Submit" />
</form>
将其更改为
<form action="main.php" method="post">
<p>
<select name="formFlowerNameQuery">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<input type="submit" name="submitQuery" value="Submit" />
</form>