简体   繁体   English

从 PHP 中的“提交”按钮插入 mysql 值不起作用

[英]Inserting mysql values from 'submit' button in PHP doesn't work

I'm new beginner in the php, html, css and working on a projekt(for my self).我是 php、html、css 的新手,正在开发一个项目(为我自己)。 Everytime i press the Save button, nothing happening.每次我按“保存”按钮时,什么也没有发生。 I checked my code many times or rewrite the code in the different ways, but still nothing happening.我多次检查我的代码或以不同的方式重写代码,但仍然没有发生任何事情。

my code:我的代码:

<?php
session_start();
if (!isset($_SESSION['userSession']) == "true") {
    header("Location: index.php");
    exit;
}

include_once("dbconnect.php");
if(isset($_POST['submit'])) {
    $category = $_POST['category'];
    $task_type = $_POST['task_type'];
    $done_by = $_POST['done_by'];
    $order_by = $_POST['order_by'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $description = $_POST['description'];

    //insert data to database
    if ($count == 0) {

        $query = "INSERT INTO task
                            (category,task_type,done_by,order_by,
                             start_date,end_date,description) 
                    VALUES('$category','$task_type','$done_by','$order_by',
                            '$start_date','$end_date','$description')";

        if ($DBcon->query($query)) {
            $msg = "<div class='alert alert-success'>
                        <span></span> successfully saved !
                    </div>";
        } else {
            $msg = "<div class='alert alert-danger'>
                        <span></span> error while saving !
                    </div>";
        }

    }
    $DBcon->close();
}


?>

<!DOCTYPE html>
<html>
<header>
    <title>Home</title>
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
    <link rel="stylesheet" href="css/style.css" type="text/css"/>
    <!--get.time-->
    <script src="javascript/get.date.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="javascript/dropdown.funktion.js" type="text/javascript"></script>
    <!--date.diff-->
    <script data-require="angular.js@1.5.0" data-semver="1.5.0"
            src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script data-require="moment.js@2.10.2" data-semver="2.10.2"
            src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"></script>
    <script src="javascript/date.diff.js" type="text/javascript"></script>
</header>
<body ng-app="dateDiff">
<!--hoyer baground-->
<div class="fullscreen-bg">
    <video loop muted autoplay poster="img/videoframe.jpg" class="fullscreen-bg__video">
        <source src="https://hoyermotors.com/wp-content/uploads/2017/04/1_Intro.mp4" type="video/mp4">
    </video>
</div>
<!--Form-->
<div class="home-page" ng-controller="index as vm"
    <form class="form-signin" method="post" id="register-form">
        <!--hoyer logo -->
        <div class="hoyer_logo">
            <image src="https://hoyermotors.com/logo-yellow.png">

                <a href="logout.php" class="btn btn-default" style="float:right;">Logout</a>

        </div>
        <hr/>
        <!-- Start Category and task type code-->
        Category: <select id="category" name="category" class="form-control" required>
            <option value="0">Select Category</option>
            <?php
            include('dbconnect.php');
            $sql = mysqli_query($DBcon, "SELECT * FROM category");
            while ($row = mysqli_fetch_array($sql)) {
                echo '<option value="' . $row['category_id'] . '">' . $row['category_name'] . '</option>';
            } ?>
        </select>
        Task Type:<select id="task_type" name="task_type" class="form-control" required>
            <option>Select Task Type</option>
        </select>


        <!-- end Category and task type code-->
        Done By: <input type="text" class="form-control" name="done_by"
                        value="<?php echo ucwords($_SESSION['userSession']); ?>" required/>
        Order By: <input type="text" class="form-control" name="order_by" value="" required/>


        Start:
        <div class="input-group ng-isolate-scope ng-valid ng-dirty ng-valid-parse">
            <input type="datetime-local" name="start_date" id="start"
                   class="form-control ng-valid ng-isolate-scope ng-dirty ng-touched" ng-model="vm.startDate" value="">
            <div class="input-group-btn">
                <button type="button" id="1" class="btn btn-default" onClick="show(this.id)" ng-disabled="disabled">
                    now
                </button>
            </div>
        </div>
        End:
        <div class="input-group ng-isolate-scope ng-valid ng-dirty ng-valid-parse">
            <input type="datetime-local" name="end_date" id="end"
                   class="form-control ng-valid ng-isolate-scope ng-dirty ng-touched" ng-model="vm.endDate" value="">
            <div class="input-group-btn">
                <button type="button" id="2" class="btn btn-default" onClick="show(this.id)" ng-disabled="disabled">
                    now
                </button>

            </div>
        </div>
        Duration:
        <div class="input-group ng-isolate-scope ng-valid ng-valid-end-time-valid">
            <input id="duration" type="data" class="form-control ng-pristine ng-valid ng-valid-date ng-touched"
                   value="Days: {{vm.result.days}} Hours: {{vm.result.hours}} Minutes: {{vm.result.minutes}}" disabled>
            <div class="input-group-btn">
                <button type="button" id="2" class="btn btn-default" ng-click="vm.getDiff()"> =</button>

            </div>
        </div>
        Description: <textarea name="description" class="form-control ng-pristine ng-valid ng-touched"
                               ng-model="log.note"
                               mh-max-length="2000" ng-trim="false"></textarea>
        <div class="form-group">
            <button type="submit" class="btn btn-default" name="submit">
                <span></span> Save
            </button>
        </div>
        <?php
        if (isset($msg)) {
            echo $msg;
        }
        ?>
    </form>

</div>

</div>

</body>
</html>

The table桌子

CREATE TABLE IF NOT EXISTS task ( 
    task_id int(50) NOT NULL AUTO_INCREMENT, 
    category varchar(50) NOT NULL, 
    task_type varchar(50) NOT NULL, 
    done_by varchar(4) NOT NULL, 
    order_by varchar(4) NOT NULL, 
    start_date DATE DEFAULT NULL, 
    end_date DATE DEFAULT NULL, 
    description varchar(2000) NOT NULL, 
    PRIMARY KEY (task_id) 
);

You might want to consider these;您可能需要考虑这些;
1, That your database connection in dbconnect.php isn't properly setup 1、你在dbconnect.php中的数据库连接没有正确设置
2, That some values you are inserting in the database through the form may have some invalid characters such as ' or ` (which is why prepared statements are very important) 2、你通过表单在数据库中插入的某些值可能有一些无效字符,例如'` (这就是为什么准备好的语句非常重要)
3, Also check what the value of $count is because if it is anything other than 0, your insert code will not be executed. 3,还要检查$count的值是什么,因为如果它不是0,你的插入代码将不会被执行。

Also try to add action="" to your form tag... not so important but it won't hurt your code either还可以尝试将action=""添加到您的表单标签中……不是那么重要,但它也不会损害您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM