未定义变量 PHP + MySQL
Undefined Variable PHP + MySQL
我知道 Whosebug 不赞成重复问题,但请耐心等待,因为我扫描了许多类似的问题,但没有找到对我有帮助的具体解决方案。 (他们大多提到避免数据库插入的事情)
我遇到这些错误消息:
here db connection success
Notice: Undefined variable: firstname in C:\xampp\htdocs\practice_connection_app\submit.php on line 10
Notice: Undefined variable: lastname in C:\xampp\htdocs\practice_connection_app\submit.php on line 10
Notice: Undefined variable: conn in C:\xampp\htdocs\practice_connection_app\submit.php on line 11
Fatal error: Call to a member function exec() on null in C:\xampp\htdocs\practice_connection_app\submit.php on line 11
第一个结果只是表明我已经连接到我使用 phpMyadmin 创建的数据库。
这是我的相关代码(我的 html 提交页面调用 php 操作):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>student info</title>
</head>
<body>
<br>
Enter your first name and last name in the corresponding boxes.
<br>
<form action="submit.php" method="POST">
First: <input type="text" name="firstname"/>
<br>
Last: <input type="text" name="lastname"/>
<br>
<input type="submit">
</form>
</body>
</html>
数据库连接(我认为)
<?php
echo 'here';
$dsn = 'mysql:host=localhost;dbname=practice_students';
$username = 'test_usr';
$password = 'pa55word';
try {
$db = new PDO($dsn, $username, $password);
echo 'db connection success';
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
和我的 php 提交页面
<?php
echo 'here ';
$dsn = 'mysql:host=localhost;dbname=practice_students';
try {
$db = new PDO($dsn);
echo 'db connection success';
$sql = "INSERT INTO people (firstname, lastname)
VALUES ('$firstname', '$lastname')";
$conn->exec($sql);
echo "Now we know your name! Hi," . " " . $firstname . " " . $lastname;
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
我知道我可能需要做一些 'cleaning up' 以避免数据库插入,但现在我只想知道如何确保我的提交进入数据库并可以返回。
提前致谢!
不确定您阅读了哪本手册以得出该代码....
你需要访问你的POST变量(使用$_POST['firstname']
)当然是在清理它们之后......
编辑:
要访问 POSTed 变量,您可以执行以下操作:
$firstname = $_POST['firstname'];
但您确实需要进行一些消毒,您可以使用 php 的 filter_var
:
$firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
不过,您可以做得更好,并且对过滤器/消毒剂允许的内容非常严格...请在获得代码后调查这部分 "working" :)
我知道 Whosebug 不赞成重复问题,但请耐心等待,因为我扫描了许多类似的问题,但没有找到对我有帮助的具体解决方案。 (他们大多提到避免数据库插入的事情)
我遇到这些错误消息:
here db connection success
Notice: Undefined variable: firstname in C:\xampp\htdocs\practice_connection_app\submit.php on line 10
Notice: Undefined variable: lastname in C:\xampp\htdocs\practice_connection_app\submit.php on line 10
Notice: Undefined variable: conn in C:\xampp\htdocs\practice_connection_app\submit.php on line 11
Fatal error: Call to a member function exec() on null in C:\xampp\htdocs\practice_connection_app\submit.php on line 11
第一个结果只是表明我已经连接到我使用 phpMyadmin 创建的数据库。
这是我的相关代码(我的 html 提交页面调用 php 操作):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>student info</title>
</head>
<body>
<br>
Enter your first name and last name in the corresponding boxes.
<br>
<form action="submit.php" method="POST">
First: <input type="text" name="firstname"/>
<br>
Last: <input type="text" name="lastname"/>
<br>
<input type="submit">
</form>
</body>
</html>
数据库连接(我认为)
<?php
echo 'here';
$dsn = 'mysql:host=localhost;dbname=practice_students';
$username = 'test_usr';
$password = 'pa55word';
try {
$db = new PDO($dsn, $username, $password);
echo 'db connection success';
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
和我的 php 提交页面
<?php
echo 'here ';
$dsn = 'mysql:host=localhost;dbname=practice_students';
try {
$db = new PDO($dsn);
echo 'db connection success';
$sql = "INSERT INTO people (firstname, lastname)
VALUES ('$firstname', '$lastname')";
$conn->exec($sql);
echo "Now we know your name! Hi," . " " . $firstname . " " . $lastname;
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
我知道我可能需要做一些 'cleaning up' 以避免数据库插入,但现在我只想知道如何确保我的提交进入数据库并可以返回。
提前致谢!
不确定您阅读了哪本手册以得出该代码....
你需要访问你的POST变量(使用$_POST['firstname']
)当然是在清理它们之后......
编辑:
要访问 POSTed 变量,您可以执行以下操作:
$firstname = $_POST['firstname'];
但您确实需要进行一些消毒,您可以使用 php 的 filter_var
:
$firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
不过,您可以做得更好,并且对过滤器/消毒剂允许的内容非常严格...请在获得代码后调查这部分 "working" :)