即使连接正常,我的php表单也不会提交到数据库中

时间:2018-03-28 19:29:08

标签: php mysql database forms

<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values

$isbn = $ISBN = $title = $Title = $pubdate = $PubDate = $pubid = $PubID = $cost = $Cost = $retail = $Retail = $category = $Category = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    if (isset($_POST["ISBN"])) {
        $isbn = trim($_POST["ISBN"]);
        echo "Yes, ISBN is set";    
    }else{  
        echo "N0, ISBN is not set";
    }

    if (isset($_POST["Title"])) {
        $title = trim($_POST["Title"]);
        echo "Title set";
    } else 
        echo "Title not set";

    if (isset($_POST["PubDate"])) {
        $pubdate = trim ($_POST ["PubDate"]);
        echo "PubD set";
    } else 
        echo "PubD not set";

    if (isset($_POST["PubID"])) {
        $pubid = trim ($_POST ["PubID"]);
        echo "PubID set";
    } else 
        echo "PubID not set";

    if (isset($_POST["Cost"])) {
        $cost = trim ($_POST["Cost"]);
        echo "Cost set";
    } else 
        echo "Cost not set";

    if (isset($_POST["Retail"])) {
        $retail = trim ($_POST["Retail"]);
        echo "retail set";
    } else 
        echo "retail not set";

    if (isset($_POST["Category"])) {
        $category = trim($_POST ["Category"]);
        echo "Category set";
    } else 
        echo "Category not set";

    $isbn_err="no isbn";
    // Check input errors before inserting in database
    if(empty($isbn_err) ) {

        // Prepare an insert statement

        $sql = "INSERT INTO books VALUES (?,?,?,?,?,?,?)";

        if($stmt = $mysqli->prepare($sql)) {

            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("issssss ", $isbn, 
                                        $title,
                                        $pubdate,
                                        $pubid,
                                        $cost,
                                        $retail,
                                        $category);

            // Set parameters
            $isbn= $ISBN;
            $title = $Title ;
            $pubdate = $PubDate;
            $pubid = $PubID ;
            $cost   = $Cost ;
            $retail = $Retail;
            $category = $Category;

            // Attempt to execute the prepared statement
            if($stmt->execute()) {
                echo "starting query";
                // Records created successfully. Redirect to landing page

                header("location: index.php");
                exit();
            } else {
                echo $stmt->error; //"Something went wrong. Please try again later.";
            }
        }
        // Close statement  
        $stmt->close(); 
    }

    // Close connection
    $mysqli->close(); 
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create Student Record</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        .wrapper{
            width: 500px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header">
                        <h2>Create Student Record</h2>
                    </div>
                    <p>Fill this form correctly to create a new student in ABIIT's database.</p>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"method="post">
                        <div class="form-group ">
                            <label>ISBN</label>
                            <input type="text" name="ISBN" class="form-control" value="">
                            <span class="help-block"></span>
                        </div>
                       <div class="form-group ">
                            <label>TITLE</label>
                            <input type="text" name="Title" class="form-control" value="">
                            <span class="help-block"></span>
                        </div>
                        <div class="form-group ">
                            <label>PUB Date</label>
                            <input type="text" name="PubDate" class="form-control" value="">
                            <span class="help-block"></span>
                        </div>
                        <div class="form-group ">
                            <label>PUBID</label>
                            <input type="text" name="PubID" class="form-control" value="">
                            <span class="help-block"></span>
                        </div>
                        <div class="form-group ">
                            <label>Cost</label>
                            <input type="text" name="Cost" class="form-control" value="">
                            <span class="help-block"></span>
                        </div>

                <div class="form-group ">
                            <label>retail</label>
                            <input type="text" name="Retail" class="form-control" value="">
                            <span class="help-block"></span>

                       </div>

                        <div class="form-group ">
                            <label>category</label>
                            <input type="text" name="Category" class="form-control" value="">
                            <span class="help-block"></span>

                       </div>

                        <input type="submit" class="btn btn-primary" value="Submit">
                        <a href="index.php" class="btn btn-default">Cancel</a>
                    </form>
                </div>
            </div>        
            </div>         
        </div>
    </div>
</body>
</html>

代码应该将输入表单的7个值插入数据库

确保我仔细检查了连接绑定变量和可能发生的其他问题

验证是否设置了变量

默认情况下,所有变量都使用空值初始化

尝试提交表单,但不会将页面提交或重定向到任何地方

目前不知道问题可能在哪里

此时尝试了所有内容,不确定它是否可能是服务器方法,表单操作或其他内容

1 个答案:

答案 0 :(得分:1)

由于这两行代码,数据库更新代码永远不会运行。

首先设置this.url = 'new-url'; this.success = newSuccessCallback; ,然后测试它是否为$isbn_err="no isbn";,当然,因为你提供的是一个永远不会为空的值,所以你永远不会输入代码块你的数据库访问。

if(empty($isbn_err) )