【问题标题】:my php form wont submit into database even though connection is working即使连接正常,我的 php 表单也不会提交到数据库
【发布时间】:2018-09-07 14:54:51
【问题描述】:
<?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个值插入到数据库中

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

验证是否设置了变量

所有变量默认初始化为空值

试图提交表单,但它没有在任何地方提交或重定向页面

目前不知道问题出在哪里

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

【问题讨论】:

  • 代码太多。您需要自己更好地解决此问题。我们不是调试器。您需要隔离问题并从那里进行调试。如果您遇到问题,请通过Minimal, Complete, and Verifiable example 提供关于什么不工作的明确解释。我建议阅读 How to Ask 一个好问题和 the perfect question。另外,请务必使用 tour 并阅读 this
  • ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 添加到脚本的顶部。这将强制任何mysqli_ 错误生成一个您可以在浏览器上看到的异常,并且其他错误也将在您的浏览器上可见。
  • 感谢您的反馈,我不知道如何启用此功能或它的作用,因为我对发现问题的语言还是新手

标签: php mysql database forms


【解决方案1】:

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

首先你设置$isbn_err="no isbn";,然后你测试它是否是if(empty($isbn_err) ),当然你给的是一个永远不会为空的值,所以你永远不会进入你的数据库访问的代码块.

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

    // your db code was here

【讨论】:

  • 我将错误作为条件放入 isset 语句中,谢谢
猜你喜欢
  • 1970-01-01
  • 2014-08-06
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 2021-08-21
  • 2021-01-08
  • 1970-01-01
相关资源
最近更新 更多