不接受表单中的数据,将传递的变量作为 NULL


Data from a form is not accepted, takes a passed variable as a NULL?

我一直在研究这个表单问题,它不会将数据插入数据库并显示错误:

完整性约束冲突:1048 列"appID"不能为空

我已经在我的$_GET['id']上运行了var_dump,它将其显示为正常传递的变量,我不知道为什么 MySQL 认为它是空的。

形式:

<?php
if ($_SESSION["loggedIn"]) { ?>
<!--//include("form.php");
echo "<a href='form.php?id=".$_GET['id']."'>Add a review</a>";-->
<form method="POST"
 action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="rating">
 <option value=""></option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 <option value="5">5</option>
</select>
<span class="error"><?php echo $rateErr;?></span>
<br />
<textarea rows="4" cols="50" name="comment"  value="<?php echo htmlspecialchars($comment);?>">Enter text here...</textarea>
<span class="error"><?php echo $comErr;?></span>
<input type="submit" name="submit" value="Submit">
</form>
<?php }
else {
echo "You need to login to review this app";
}
?>

以及验证和插入数据的功能

<?php
    // define variables and initialize with empty values
    $rateErr = $comErr = "";
    $rating = $comment = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST["rating"] == "") {
            $rateErr = "Rate the app";
        }
        else {
            $rating= $_POST["rating"];
        }
        if (empty($_POST["comment"])) {
            $comErr = "Missing";
        }
        else {
            $comment = $_POST["comment"];
        }
        if ($rateErr == "" && $comErr == "") {
            try {
                $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
                $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                $sql = "INSERT INTO reviews (rating, content, appID, user) VALUES (:rating, :comment, :id, :username)";
                $stmt = $con->prepare( $sql );
                $stmt->bindValue( ":rating", $rating);
                $stmt->bindValue( ":comment", $comment);
                $stmt->bindValue( ":id", $_GET['id']);
                $stmt->bindValue( ":username", $_SESSION['username']);
                $stmt->execute();
                return "Submitted successfully";
            }
            catch( PDOException $e ) {
                echo $e->getMessage();
            }
        }
    }
?>

如果你使用$_POST请求,你需要检查变量,更改:

if ($rateErr == "" && $comErr == "") {

if (empty($rateErr) && empty($comErr) && isset($_POST['id']) && !empty($_POST['id'])) {

并替换下一行,因为您没有$_GET请求:

$stmt->bindValue(":id", $_GET['id']);

$stmt->bindValue(":id", $_POST['id']);

您正在检查此内容;请注意POST请求方法:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

但是访问这个; 注意$_GET请求方法:

$stmt->bindValue(":id", $_GET['id']);

什么时候应该使用$_POST

$stmt->bindValue(":id", $_POST['id']);

也就是说,你有这个:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

但问题是,这样做elseGET对吗?而且您在该支票中嵌套了$_GET['id'] POST对吗?那为什么不这样做:

if (in_array($_SERVER["REQUEST_METHOD"], array('POST', 'GET')) {

这样,您的支票就可以处理POSTGET方法。也许需要更大的重构才能更好地捕获错误?这是您应该尝试的轻微重构。我从if ($_SERVER["REQUEST_METHOD"] == "POST") {中删除了数据库插入逻辑,并设置了一个if ($_SERVER["REQUEST_METHOD"] == "GET") {,该也设置了一个$appID

<?php
    // define variables and initialize with empty values
    $rateErr = $comErr = "";
    $rating = $comment = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST["rating"] == "") {
            $rateErr = "Rate the app";
        }
        else {
            $rating= $_POST["rating"];
        }
        if (empty($_POST["comment"])) {
            $comErr = "Missing";
        }
        else {
            $comment = $_POST["comment"];
        }
    }
    $appID = $appIDErr = "";
    if ($_SERVER["REQUEST_METHOD"] == "GET") {
        if (empty($_GET["id"])) {
            $appIDErr = "Missing";
        }
        else {
            $appID = $_GET["id"];
        }
    }
    if ($rateErr == "" && $comErr == "" && $appIDErr == "") {
        try {
            $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $sql = "INSERT INTO reviews (rating, content, appID, user) VALUES (:rating, :comment, :id, :username)";
            $stmt = $con->prepare( $sql );
            $stmt->bindValue( ":rating", $rating);
            $stmt->bindValue( ":comment", $comment);
            $stmt->bindValue( ":id", $appID);
            $stmt->bindValue( ":username", $_SESSION['username']);
            $stmt->execute();
            return "Submitted successfully";
        }
        catch( PDOException $e ) {
            echo $e->getMessage();
        }
    }

?>