未定义指数 $_POST


Undefined Index $_POST

<?php 
    /*
        Our "config.inc.php" file connects to database every time we include or require
        it within a php script.  Since we want this script to add a new user to our db,
        we will be talking with our database, and therefore,
        let's require the connection to happen:
    */
    require("config.inc.php");
    if(!empty($_POST))
    {
        echo "Posted data not empty";
    } else {
        $query = "INSERT INTO userrequests ( userName, contactNumber, userAddress, storeList, requestBody ) VALUES ( :name, :contact, :address, :store, :request ) ";
        $query_params = array(
            ':name' => $_POST['userName'],
            ':contact' => $_POST['contactNumber'],
            ':address' => $_POST['userAddress'],
            ':store' => $_POST['storeList'],
            ':request' => $_POST['requestBody']
        );
        try {
            $stmt   = $db->prepare($query);
            $result = $stmt->execute($query_params);
        }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());
            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error2. Please Try Again!";
            die(json_encode($response));
        }
        $response["success"] = 1;
        $response["message"] = "Request successfully sent! One of our Rockets will contact you in a while!";
        echo json_encode($response);
?>
    <h1>Register</h1> 
    <form action="register.php" method="POST"> 
        Name:<br /> 
        <input type="text" name="userName" value="" /> 
        <br /><br /> 
        Contact Number:<br /> 
        <input type="text" name="contactNumber" value="" /> 
        <br /><br /> 
        Address:<br /> 
        <input type="text" name="userAddress" value="" /> 
        <br /><br />
        Store:<br /> 
        <input type="text" name="storeList" value="" /> 
        <br /><br /> 
        Request:<br /> 
        <input type="text" name="requestBody" value="" /> 
        <br /><br /> 
        <input type="submit" value="Call A Rocket" /> 
    </form>
<?php
    }
?>

注意:未定义的索引:C:''xampp''htdocs''callarocket''register.php 第 20 行中的用户名

注意:未定义的索引:C:''xampp''htdocs''callarocket''register.php 第 21 行中的联系人号码

注意:未定义的索引:c:''xampp''htdocs''callarocket''register.php 中的 userAddress 在第 22 行

注意:未定义的索引:C:''xampp''htdocs''callarocket''register.php 第 23 行中的 storeList

注意:未定义的索引:请求正文在 C:''xampp''htdocs''callarocket''register.php 第 24 行 {"成功":0,"消息":"数据库错误 2.请重试!

我可以知道这个代码有什么问题吗?

因此,您正在检查$_POST是否为空,如果是,则使用不存在的变量来创建查询。你应该把你的sql代码放在它说$_POST不为空的部分。

但是,您应该检查所有密钥(如果存在(。这样,您确定不会发生任何错误。

<?php 
    require("config.inc.php");
    if(!empty($_POST))
    {
        echo "Posted data not empty";
        $query = "INSERT INTO userrequests ( userName, contactNumber, userAddress, storeList, requestBody ) VALUES ( :name, :contact, :address, :store, :request ) ";

        $query_params = array(
            ':name' => $_POST['userName'],
            ':contact' => $_POST['contactNumber'],
            ':address' => $_POST['userAddress'],
            ':store' => $_POST['storeList'],
            ':request' => $_POST['requestBody']
        );

        try {
            $stmt   = $db->prepare($query);
            $result = $stmt->execute($query_params);
        }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());
            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error2. Please Try Again!";
            die(json_encode($response));
        }
        $response["success"] = 1;
        $response["message"] = "Request successfully sent! One of our Rockets will contact you in a while!";
        echo json_encode($response);
    }else{
?>
    <h1>Register</h1> 
    <form action="register.php" method="POST"> 
        Name:<br /> 
        <input type="text" name="userName" value="" /> 
        <br /><br /> 
        Contact Number:<br /> 
        <input type="text" name="contactNumber" value="" /> 
        <br /><br /> 
        Address:<br /> 
        <input type="text" name="userAddress" value="" /> 
        <br /><br />
        Store:<br /> 
        <input type="text" name="storeList" value="" /> 
        <br /><br /> 
        Request:<br /> 
        <input type="text" name="requestBody" value="" /> 
        <br /><br /> 
        <input type="submit" value="Call A Rocket" /> 
    </form>
    <?php
    }
    ?>