如何修复PHP中的未定义变量错误

时间:2019-05-02 22:21:08

标签: php html database

我正在为此进行评估,但我快完成了,但是每次我尝试使用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>";
    }

对于该查询,我希望它可以将我输入的详细信息添加到数据库中,但是,我得到的所有值的变量都未标识

1 个答案:

答案 0 :(得分:-1)

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

echo (print_r($_POST, true));

然后提取变量,请尝试:

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

当心您的大写字母!

由于这是大学的一项工作,所以我要添加一些内容:“在生产环境中,$ _ POST全局数据将在使用前进行清理,以防止可能的sql注入”