将日期插入Mysql数据库时出错

时间:2020-03-31 05:12:01

标签: mysql

我有一个包含几列的表。列之一的类型为DATE。当我尝试向表中插入数据时,出现以下错误:

{"code":"ER_TRUNCATED_WRONG_VALUE","errno":1292,"sqlMessage":"Incorrect date value: '11-04-2020' for column 'date' at row 1","sqlState":"22007","index":0,"sql":"INSERT INTO `players` (achternaam, telefoonnummer, adres, email, typetaart, aantalpersonen, smaak, vulling, opmerking, prijs, reeds_voldaan, nog_te_voldoen, date, image, user_name) VALUES ('Ashoti', '33', 'test', 'w_emanual@hotmail.com', 'Verjaardagstaart', '20', 'Vanillia smaak','Chocolade vulling', 'undefined', '100', '20', '80', '11-04-2020', '11.jpeg', '11')"}

我有一个jQuery datepicker。这是我的添加页面

<%- include ('partials/header.ejs'); -%>
<head>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <!--<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">-->
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $(function(){
            $("#date").datepicker({
                formatDate: "yy-mm-dd",
                dateFormat: "dd-mm-yy",

                // dateFormat: "mm-dd-yy",
                // dateFormat: "yy-mm-dd",
                changeMonth: true,
                changeYear: true,
                minDate: 0,
                gotoCurrent: true,
                });
        });
    </script>
</head>
<div class="container">
    <% if (message != '') { %>
        <p class="text-center text-danger"><%= message %></p>
    <% } %>
    <form class="add-player-form" action="" method="post" enctype="multipart/form-data">
        <div class="form-row">
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam" required>
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="telefoonnummer" id="telefoonnummer" placeholder="Telefoonnummer" required>
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="username" id="username" placeholder="Username" required>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-6">
                <input type="number" class="form-control" name="number" id="number" placeholder="Number" required>
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="adres" id="adres" placeholder="Adres" required>
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="email" id="email" placeholder="Email">
            </div>
            <h1>Reservering informatie</h1>
            <div class="form-group col-md-6">
                <select id="typetaart" name="typetaart" class="form-control" required>
                    <option selected disabled>Selecteer typetaart</option>
                    <option>Verjaardagstaart</option>
                    <option>Verlovingstaart</option>
                    <option>Bruidstaart</option>
                    <option>Eerste heilige communie taart</option>
                    <option>Geslacht bekentenistaart</option>
                    <option>Moederdag taart</option>
                    <option>Vaderdags taart</option>
                    <option>Cupcakes</option>
                    <option>Overige</option>
                </select>
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="aantalpersonen" id="aantalpersonen" placeholder="Aantalpersonen" required>
            </div>
            <div class="form-group col-md-6">
                <select id="smaak" name="smaak" class="form-control" required>
                    <option selected disabled>Smaak</option>
                    <option>Vanillia smaak</option>
                    <option>Chocolade smaak</option>
                    <option>Citroen smaak</option>
                    <option>Mokka smaak</option>
                    <option>Anders, zie opmerkingen</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <select id="vulling" name="vulling" class="form-control" required>
                    <option selected disabled>Smaak</option>
                    <option>Vanillia vulling</option>
                    <option>Chocolade vulling</option>
                    <option>Citroen vulling</option>
                    <option>Anders, zie opmerkingen</option>
                </select>
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="opmerkingen" id="opmerkingen" placeholder="Opmerkingen" >
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="prijs" id="prijs" placeholder="Prijs" required >
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="reedsVoldaan" id="reedsVoldaan" placeholder="Reeds voldaan" >
            </div>
            <div class="form-group col-md-4">
                <input type="text" class="form-control" name="nogTeVoldoen" id="nogTeVoldoen" placeholder="Nog te voldoen" >
            </div>
            <div class="form-group col-md-6">
                <input class="form-control" type="text"  id="date" name="date" placeholder="Date"> 
            </div>
            <div class="col-md-12">
                <label for="player-img"><b>Player Image</b></label><br>
                <input type="file" name="image" id="player-img" class="" required>
            </div>
        </div>
        <button type="submit" class="btn btn-primary float-right">Add Player</button>
    </form>
</div>
</div>
</body>
</html>

我的INSERT查询看起来像

"INSERT INTO `players` (achternaam, telefoonnummer, adres, email, typetaart, aantalpersonen, smaak, vulling, opmerking, prijs, reeds_voldaan, nog_te_voldoen, date, image, user_name) VALUES ('" + achternaam + "', '" + telefoonnummer + "', '" + adres + "', '" + email + "', '" + typetaart + "', '" + aantalpersonen + "', '" + smaak + "','" + vulling + "', '" + opmerking + "', '" + prijs + "', '" + reedsVoldaan + "', '" + nogTeVoldoen + "', '" + date + "', '" + image_name + "', '" + username + "')";

我试图更改formatDate,但是没有用。有人可以帮我这个问题,并告诉我插入查询应如何将日期插入Mysql数据库。互联网上的一些解决方案说,我必须添加一个函数STR_TO_DAT才能将日期转换为yyyy-mm-dd以便插入到Mysql数据库中。但是我仍然不知道如何以及在何处添加该功能。如果您能帮助我提供解决方案,那将非常好。 亲切的问候, 威廉·阿肖蒂

1 个答案:

答案 0 :(得分:0)

STR_TO_DATE函数放入您的INSERT查询中,如下所示:

...
    STR_TO_DATE ('" + date + "', '%d-%m-%Y'),
...

有关STR_TO_DATE

的更多信息
相关问题