首页 > 解决方案 > 如何修复 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>";
    }

对于此查询,我希望它添加我输入到数据库中的详细信息,但相反,我得到所有值的未识别变量

标签: phphtmldatabase

解决方案


您需要从 $_POST 全局数组中提取这些值。要查看此数组中的内容,请尝试:

echo (print_r($_POST, true));

然后提取变量,尝试:

$ISBN = $_POST['ISBN'];
$title  = $_POST['Title'];
$replacementcost = $_POST['ReplacementCost'];
etc

注意你的大写字母!

因为这是大学的作业,所以我会添加一些内容,上面写着“在生产环境中,$_POST 全局数据将在使用前进行清理,以防止可能的 sql 注入”


推荐阅读