PHP空字段不起作用

时间:2014-12-13 19:29:37

标签: php html mysql forms

任何人都可以在此脚本中找到任何错误,因为我仍然可以提交空白表单。 PHP empty()函数也不起作用。我试过双重检查,三重检查,我找不到简单的解释。我在使用XAMPP的本地环境中工作。有什么建议吗?

<?php
include('core/includes/startsite.inc.php');
if(isset($_POST['submit'])){
$con = new mysqli("localhost", "root", "salvation", "dcstocksolutions");
$name = $_POST['name'];
$comment = $_POST['comment'];
if($name == "" || $comment == ""){
    $msg = "Sorry, you missed out a field.";
}
$q = "INSERT INTO testimonials ('name', 'comment') VALUES ('$name', '$comment')";

if($con->query($q)){
    $_SESSION['msg'] = "Thank you! Your review was submitted.";
    header("Location: testimonials?r=1");
} else {
    $msg = "Sorry, there was an error submitting your review, please try again later.";
}
}
?>
<div class="testimonial-container">
<h3 align="center">Leave a review</h3><br />
    <?php
        if(isset($msg)){
            echo("<p align=\"center\">" . $msg . "</p>");
        }
    ?>
    <form action="review" method="POST" autocomplete="off">
        <table>
            <tr>
                <td>Full Name:</td>
                <td><input type="text" name="name" placeholder="Full Name" value=""></td>
            </tr>
            <tr>
                <td>Comment:</td>
                <td><textarea name="comment"></textarea></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="submit" value="Submit Review"></td>
            </tr>
        </table>
    </form>
</div>
<?php
include('core/includes/endsite.inc.php');
?>

1 个答案:

答案 0 :(得分:1)

结帐这个。你没有在这里检查提交按钮

<?php
include('core/includes/startsite.inc.php');
if(isset($_POST['submit'])){
$con = new mysqli("localhost", "root", "salvation", "dcstocksolutions");
$name = $_POST['name'];
$comment = $_POST['comment'];
if($name == "" || $comment == ""){
    $msg = "Sorry, you missed out a field.";
}else{
$q = "INSERT INTO testimonials ('name', 'comment') VALUES ('$name', '$comment')";

if($con->query($q)){
    $_SESSION['msg'] = "Thank you! Your review was submitted.";
    header("Location: testimonials?r=1");
} else {
    $msg = "Sorry, there was an error submitting your review, please try again later.";
}
}else{echo "Not submited !";}}
?>
<div class="testimonial-container">
<h3 align="center">Leave a review</h3><br />
    <?php
        if(isset($msg)){
            echo("<p align=\"center\">" . $msg . "</p>");
        }
    ?>
    <form action="review" method="POST" autocomplete="off">
        <table>
            <tr>
                <td>Full Name:</td>
                <td><input type="text" name="name" placeholder="Full Name" value=""></td>
            </tr>
            <tr>
                <td>Comment:</td>
                <td><textarea name="comment"></textarea></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="submit" value="Submit Review"></td>
            </tr>
        </table>
    </form>
</div>
<?php
include('core/includes/endsite.inc.php');
?>