获取未定义的变量:index.php中的pdo
我正在创建配方数据库,添加“添加”和“删除”功能后,系统会抛出一个错误: 注意:未定义的变量:第52行C:\xampp\htdocs\COMP1321\recipes\index.php中的pdo 致命错误:未捕获错误:在C:\xampp\htdocs\COMP1321\recipes\index.php:52堆栈跟踪:#0{main}中,在第52行的C:\xampp\htdocs\COMP1321\recipes\index.php中调用null上的成员函数query() Index.php获取未定义的变量:index.php中的pdo,php,mysql,pdo,Php,Mysql,Pdo,我正在创建配方数据库,添加“添加”和“删除”功能后,系统会抛出一个错误: 注意:未定义的变量:第52行C:\xampp\htdocs\COMP1321\recipes\index.php中的pdo 致命错误:未捕获错误:在C:\xampp\htdocs\COMP1321\recipes\index.php:52堆栈跟踪:#0{main}中,在第52行的C:\xampp\htdocs\COMP1321\recipes\index.php中调用null上的成员函数query() Index.php
try // selection block
{
$sql = 'SELECT * FROM recipe';
$result = $pdo->query($sql);
}
<?php
try
{
//new PDO('mysql:host=mysql.cms.gre.ac.uk; dbname=mdb_', '', '');
$pdo = new PDO('mysql:host=localhost; dbname=mdb_recipes', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
} catch (PDOException $e) {
$error = 'Unable to connect to database server';
include 'error.html.php';
exit();
}
<?php
if(isset($_GET['addrecipe'])) {
include 'form.html.php';
exit();
}
//insert block
if (isset($_POST['recipename'])) {
include 'admin/includes/db.inc.php';
try
{
//prepared statement
$sql = 'INSERT INTO recipe SET
recipename = :recipename,
recipedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':recipename', $_POST['recipename']);
$s->execute();
} catch (PDOException $e) {
$error = 'Error adding submitted recipe' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
//delete block
if(isset($_GET['deleterecipe']))
{
include '../includes/db.inc.php';
try
{
$sql = 'DELETE FROM recipe WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting recipe' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
try // selection block
{
$sql = 'SELECT * FROM recipe';
$result = $pdo->query($sql);
}
catch (PDOException $e) {
$error = 'Error fetching recipes' . $e->getMessage();
include 'error.html.php';
exit();
}
foreach ($result as $row) {
$recipes[] = array(
'id' => $row['id'],
'recipename' => $row['recipename'],
'time' => $row['time'],
'ingredients' => $row['ingredients'],
'recipetext' => $row['recipetext'],
'servings' => $row['servings'],
'nutritionfacts' => $row['nutritionfacts'],
'recipedate' => $row['recipedate'],
'image' => $row['image'],
);
}
include 'recipes.html.php';
Databaseconnection.inc.php
try // selection block
{
$sql = 'SELECT * FROM recipe';
$result = $pdo->query($sql);
}
<?php
try
{
//new PDO('mysql:host=mysql.cms.gre.ac.uk; dbname=mdb_', '', '');
$pdo = new PDO('mysql:host=localhost; dbname=mdb_recipes', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
} catch (PDOException $e) {
$error = 'Unable to connect to database server';
include 'error.html.php';
exit();
}
<?php
if(isset($_GET['addrecipe'])) {
include 'form.html.php';
exit();
}
//insert block
if (isset($_POST['recipename'])) {
include 'admin/includes/db.inc.php';
try
{
//prepared statement
$sql = 'INSERT INTO recipe SET
recipename = :recipename,
recipedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':recipename', $_POST['recipename']);
$s->execute();
} catch (PDOException $e) {
$error = 'Error adding submitted recipe' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
//delete block
if(isset($_GET['deleterecipe']))
{
include '../includes/db.inc.php';
try
{
$sql = 'DELETE FROM recipe WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting recipe' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
try // selection block
{
$sql = 'SELECT * FROM recipe';
$result = $pdo->query($sql);
}
catch (PDOException $e) {
$error = 'Error fetching recipes' . $e->getMessage();
include 'error.html.php';
exit();
}
foreach ($result as $row) {
$recipes[] = array(
'id' => $row['id'],
'recipename' => $row['recipename'],
'time' => $row['time'],
'ingredients' => $row['ingredients'],
'recipetext' => $row['recipetext'],
'servings' => $row['servings'],
'nutritionfacts' => $row['nutritionfacts'],
'recipedate' => $row['recipedate'],
'image' => $row['image'],
);
}
include 'recipes.html.php';
看起来您从未包含数据库连接文件
如果未设置$\u POST['recipename']
,并且未设置$\u GET['deleterecipe']
,那么当您到达该行时,db.inc.php
从未包含在内,因此未定义$pdo
。您需要移动包含'admin/includes/db.inc.php'代码>行位于form.html.php include的正下方
这将使其可用于整个页面,因为它是必需的,无论您是否插入或删除配方,因为您需要它用于底部的选择块
每个页面只需要包含一次文件,我通常会在DB和其他类似的全局依赖项的文件顶部使用require_once()
,而不是尝试在每个需要的位置包含它,每个文件包含3次以上,然后不必要地包含该文件,并可能生成警告或错误。一般来说,这只会使代码更加复杂。这是一个共享资源,让它共享吧。或者创建一个单例,以标准方式为您创建或检索PDO实例,这样您只有一个PDO连接,而不是每个请求可能有多个连接。您是否在try/catch块的外部(之前)声明了$PDO
变量,以便它在index.php文件中可见?这两个if(isset($\u POST['recipename'))
和if(isset($\u GET['deleterecipe'])
必须为false,因此您永远不会包含数据库文件。这是错误的。不要在文件的顶部包含一次,否则会损害你的理智。