php - 如何修复 PHP 中的未定义变量错误
问题描述
我这样做是为了进行评估,我几乎完成了,但是每次我尝试使用 PHP 代码更新或编辑我的数据库时,它都会给出未定义的变量错误
我已经尝试隔离它所说的导致错误的代码并修复它,但无论我做什么它都不起作用
HTML:
<form id = "add_books_form" name = "new_book" method = "post" action = "book_processings.php">
<div id = "inputs"><label>ISBN </label><input id = "elements" type = "text" maxlength = "30" name = "ISBN" required/><br></div>
<div id = "inputs"><label>Title </label><input id = "elements" type = "text" maxlength = "100" name = "Title" required/><br></div>
<div id = "inputs"><label>Replacement Cost $ </label><input id = "elements" type = "number" value = "0.00" step = "0.10" min = "0" max = "99.99" name = "ReplacementCost" required/><br></div>
<div id = "inputs"><label>Adult Theme </label><select id = "elements" name = "adult_theme">
<option value = "Y">Yes</option>
<option value = "N">No</option>
</select><br></div>
<div id = "inputs"><label>Category </label><select id = "elements" name = 'category'>
<?php
while($all_category_rec = mysqli_fetch_assoc($all_category_qrun)){
echo"<option value = '".$all_category_rec['CategoryID']."'>".$all_category_rec['Category']."</option>";
}
?>
</select><br></div>
<div id = "inputs"><label>Author </label><select id = "elements" name = 'author'>
<?php
while($all_authors_rec = mysqli_fetch_assoc($all_authors_qrun)){
echo"<option value = '".$all_authors_rec['AuthorID']."'>".$all_authors_rec['FirstName']." ".$all_authors_rec['LastName']."</option>";
}
?>
</select><br></div>
<div id = "inputs"><label>Publisher </label><select id = "elements" name = 'publisher'>
<?php
while($all_publishers_rec = mysqli_fetch_assoc($all_publishers_qrun)){
echo"<option value = '".$all_publishers_rec['PublisherID']."'>".$all_publishers_rec['PublisherName']."</option>";
}
?>
</select><br></div>
<div id = "inputs"><label>Published Year </label><input id = "elements" type = "number" step = "1" max = "2025" min = "1400" value = "2019" name = "YearOfPublication" required/><br></div>
<input id = "button" type = "submit" name = "submit" value = "Add Book"/>
</form>
PHP:
if($_POST['submit'] == 'Add Book'){
$add_book_query = "INSERT INTO books(ISBN, Title, ReplacementCost, AdultContent, Category, AuthorID, YearOfPublication , PublisherID)
VALUES('$ISBN', '$Title', '$ReplacementCost', '$AdultContent', '$CategoryID', '$AuthorID', '$YearOfPublication', '$PublisherID')";
$add_book_qrun = mysqli_query($dbcon, $add_book_query);
if(!$add_book_qrun){
echo"<h3>Data was not entered.</h3>";
}else{
echo"<h3>Data was successfuly enetered.</h3>";
}
echo"<form id = 'proc' name = 'go_back' method = 'post' action = 'add_book.php'>";
echo"<input id = 'button' type = 'submit' name = 'submit2' value = 'Go Back'/>";
echo"</form>";
}
对于此查询,我希望它添加我输入到数据库中的详细信息,但相反,我得到所有值的未识别变量
解决方案
您需要从 $_POST 全局数组中提取这些值。要查看此数组中的内容,请尝试:
echo (print_r($_POST, true));
然后提取变量,尝试:
$ISBN = $_POST['ISBN'];
$title = $_POST['Title'];
$replacementcost = $_POST['ReplacementCost'];
etc
注意你的大写字母!
因为这是大学的作业,所以我会添加一些内容,上面写着“在生产环境中,$_POST 全局数据将在使用前进行清理,以防止可能的 sql 注入”
推荐阅读
- python - 从模块访问变量
- regression - 使用 libreOffice calc 通过一组 3D 点拟合平面,以最小化总距离
- css - 如何为具有自定义列宽的可滚动表完成固定标题?
- docker - 如何通过将自定义补丁应用到文件而不具有状态“已退出 (0)”来创建自定义 Docker 映像
- tensorflow - TensorFlow 2.0 使用 GradientTape 在 dtype=int32 上返回意外输出
- python - 当输入参数很小时,scipy.optimize 返回初始猜测
- laravel - Laravel Blade 模板中的嵌套循环
- javascript - 验证转换为数字失败
- apache - 在不更改前端代码的情况下禁用哈希 URL(如 http://localhost/#/login)
- botframework - 获取用户的所有对话