PHP没有插入数据库

时间:2018-04-09 21:48:28

标签: php mysqli

我正在尝试运行我的PHP脚本问题。用户应该使用所需的信息填写应用程序,并且一旦提交,就直接进入数据库,并显示在我做的外部应用程序管理器上。嗯..在提交应用程序时,没有任何东西被添加到数据库中。我确保我的SQL查询是正确的结构作为我的数据库的架构,但仍然没有。使用PHPMyAdmin将查询运行到数据库本身,它工作得很好.. 这是我的完整PHP,它将信息添加到数据库中。

<?php

                $sql = mysqli_query($mysqli, "SELECT username FROM tmod WHERE username = '".$_SESSION['user']['username']."'");
                if(isset($_POST['submit']))
                {
                    if (empty($_POST['real']) || empty($_POST['age']) || empty($_POST['why']) || empty($_POST['dif']) || empty($_POST['agree']))
                    {
                        echo '<div class="animated shake">
                                <div class="message error">Please fill in all fields.</div>
                            </div>'; 
                    }
                    else
                    {
                    if(mysqli_num_rows($sql) < 1){
                        $q =  mysqli_query($mysqli, "INSERT INTO `tmod` (AppID,username,realname,age,position,why,different,additional,agree) VALUES (NULL, '".$_SESSION['user']['username']."', '".filter($_POST["real"])."', '".filter($_POST["position"])."',  '".filter($_POST["age"])."', '".filter($_POST["why"])."', '".filter($_POST["dif"])."', '".filter($_POST["additional"])."', '".filter($_POST["agree"])."')");

                        if( $q !== false ) { 
                        echo '<div class="animated shake">
                                <div class="message success"><b>Thank you!</b> Your application has been submitted and is awaiting to be reviewed by staff!</div>
                            </div>'; 
                        } else {
                            echo '<div class="animated shake">
                                <div class="message error">Your application did not go through due to an error.</div>
                            </div>';
                        }
                    }else{
                        echo '<div class="animated shake">
                                <div class="message error">You have already applied, please wait for a reply.</div>
                            </div>';
                        } 
                    }   
                }
            ?>

            <div class="box">
                <div class="contentHeader headerGreen">
                    <div class="inside">
                        Staff Application
                    </div>
                </div>
                <div class="inside">
                    <form method="post">
                        <div class="form-section">
                            <strong>Username:</strong><br />
                            <input disabled="disabled" type="text" value="{username}">
                        </div>

                        <div class="form-section">
                            <strong>Real Name:</strong><br />
                            <input type="text" name="real" maxlength="28">
                        </div>
                        <div class="form-section">
                            <strong>Position:</strong><br />
                            <input type="text" name="position" maxlength="28">
                        </div>

                        <div class="form-section">
                            <strong>Age:</strong><br />             
                            <select name="age">
                                <?php for ($i = 13; $i <= 60; $i++) : ?>
                                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                <?php endfor; ?>
                            </select>
                        </div>

                        <div class="form-section">

                            <textarea name="why" rows="4" cols="29" placeholder="Why Should We Hire You?"></textarea>
                        </div>

                        <div class="form-section">

                            <textarea name="dif" rows="4" cols="40"placeholder="How are you different from the rest?"></textarea>
                        </div>

                        <div class="form-section">
                            <strong>
                            <textarea name="additional" rows="4" cols="40" placeholder="Additional Information - Leave your Skype here" ></textarea>
                        </div>

                        <div class="form-section" style="float:bottom">
                            <input type="checkbox" value="1" name="agree" >
                            <label>I agree to be active on the Habbsane Hotel.</label>
                        </div>

                        <div class="form-section">
                            <input type="submit" class="submit" name="submit" value="Apply">
                        </div>
                    </form>
                </div>
            </div>

我导入了我的数据库连接信息。

<?php       require_once ('link/to/my/includes/connection.php'); ?>

connection.php的内容:

<?php
$mysqli = new mysqli("DB HOST", "root", "db pass", "DB NAME"); ?>

以下是数据库中我的tmod表的设置

`AppID` int(11) NOT NULL,
`different` varchar(6000) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
`realname` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`why` varchar(6000) DEFAULT NULL,
`additional` varchar(6000) DEFAULT NULL,
`agree` varchar(1) DEFAULT NULL,
`denied` varchar(255) DEFAULT 'Not Read Yet',
`reply` varchar(1000) DEFAULT NULL,
`save` varchar(255) DEFAULT NULL

1 个答案:

答案 0 :(得分:-2)

您可以添加

  echo("Error description: " . mysqli_error($mysqli));

输出mysql错误。不要离开生产。

您的数据库设置为AppID int(11) NOT NULL

但是你的代码显示:

"INSERT INTO `tmod` (AppID, username,`realname`,age,position,why,different,additional,agree) 
VALUES (NULL,...

您正在将NULL插入AppID。确保AppID在数据库端设置为自动递增。

此外,您要为列position插入一个值,但却缺少表格中的position列。 (根据OP评论的完整性添加)。

请记住@Grant对SQL Injection所说的内容并使用PDO