值插入数据库

时间:2018-02-22 14:30:42

标签: javascript php html

所以我有这个功能,创造了月份的textareas。因此,如果它是三月,那么就是四月份的31 textareas,然后是30 textareas等等。当用户单击一个textarea然后单击提交按钮时,应将textarea的值提交给db。因此,如果用户标记为2018-02-04,则应将该日期插入到数据库中。但是,我现在的问题是,提交的唯一值是每个月的最后一个日期。不知道为什么,也不知道如何解决它。认为它可能是ID的东西。

功能:

var showDate = new Date();
var months = ["Januari", "Februari", "March", "April", "May", "June",
              "July", "Augusti", "September", "October", "November", "December"];
var weeks = ["Sunday","Monday","Tuseday","Wednesday","Thursday","Friday","Saturday"];


function drawTable(forDate) {
    var daysInMonth = new Date(forDate.getFullYear(),forDate.getMonth()+1,0).getDate();
    var cellsToDraw = daysInMonth;
    var newdate = forDate.getFullYear() +"-"+ ("0"+ (forDate.getMonth() + 1)).slice(-2);
    var table = document.getElementById("table");
    table.innerHTML = "";
    for (var r = 0; r < (daysInMonth / 7); r++) {
        var newRow = document.createElement("tr");
        table.appendChild(newRow);
        for (var c = 0; c < 31 && cellsToDraw > 0; c++) {
            var day1 = ("0" + (c + 1)).slice(-2);

            var textarea = document.createElement("textarea");
            textarea.setAttribute("placeholder", day1 );
            //textarea.setAttribute("id", some_value); does not work
            newRow.appendChild(textarea);
            textarea.setAttribute("name", "day");
            textarea.setAttribute("day",  newdate + "-" + day1 )
            textarea.innerHTML = newdate + "-" + day1;
            cellsToDraw--;
        }
    }
}

window.onload = function() {
    document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
    drawTable(showDate );
};

function next() {
    if (showDate.getMonth() == 11) {
        showDate.setMonth( 0 );
        showDate.setFullYear( showDate.getFullYear()+1 );
    } else {
        showDate.setMonth( showDate.getMonth()+1 );
    }
    document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
    drawTable( showDate );
}

HTML:

<form class="" action="index.php" method="post">
      <table id="table" cellspacing="0" cellpadding="0"   border-collapse="collapse";>
         <br>
         <input id="btn"  type="submit" name="" value="Send">
       </form>

PHP:

<?php
include ("connection.php");
$day = mysqli_real_escape_string($conn, $_REQUEST['day']);

$stmt = "INSERT INTO table (day) VALUES('$day')";
if(empty($day)){
  $_SESSION['error'] = "Please fill in required fields";

  header('Location: index.php', true, 303);
  exit();
} else {
  if (mysqli_query($conn, $stmt)) {
    header('Location: index.php', true, 303);
        exit;
  }else {
    $error= "Error: " .mysqli_error($conn);
    echo "$error";

  }
}

?>

所有帮助都是适当的! :)

1 个答案:

答案 0 :(得分:0)

首先,我建议您始终检查输出...这就是您的javascript创建的内容。

$("#other").select2({
    tags: true,
    data: data,
    escapeMarkup: function(markup) {
        return markup;
    },
    createTag: function(params) {
        var obj = {
            id: 'new_' + params.term,
            text: '<div style="font-size: 1.2em; color:blue">' + params.term + '</div><div><b>Select2</b> supports custom themes using the theme option so you can style Select2 to match the rest of your application.</div>',
            title: params.term,
            isNew : true
        };
        return obj;
    }
}).on('select2:select', function (e) {
    if(e.params.data.isNew) {
        data.push({id: e.params.data.id, text: '<div style="font-size: 1.2em; color:blue">' + e.params.data.text + '</div><div><b>Select2</b> supports custom themes using the theme option so you can style Select2 to match the rest of your application.</div>', title: e.params.data.text});
    }
});

正如您所看到的,您将始终获得最后一个textarea的“日期”值,因为您的表单没有“day”属性的唯一值

您应该为每个textarea添加一个带有提交按钮的表单,或者完全重写您的代码..

快速解决方案:

HTML

<form class="" action="index.php" method="post">
    <br><input id="btn" type="submit" name="" value="Send">
    <table id="table" cellspacing="0" cellpadding="0" border-collapse="collapse" ;="">
        <tr>
            <textarea placeholder="01" name="day" day="2018-02-01">2018-02-01</textarea>
            <textarea placeholder="02" name="day" day="2018-02-02">2018-02-02</textarea>
            <textarea placeholder="03" name="day" day="2018-02-03">2018-02-03</textarea>
            <textarea placeholder="04" name="day" day="2018-02-04">2018-02-04</textarea>
            <textarea placeholder="05" name="day" day="2018-02-05">2018-02-05</textarea>
            <textarea placeholder="06" name="day" day="2018-02-06">2018-02-06</textarea>
            <textarea placeholder="07" name="day" day="2018-02-07">2018-02-07</textarea>
            <textarea placeholder="08" name="day" day="2018-02-08">2018-02-08</textarea>
            <textarea placeholder="09" name="day" day="2018-02-09">2018-02-09</textarea>
            <textarea placeholder="10" name="day" day="2018-02-10">2018-02-10</textarea>
            <textarea placeholder="11" name="day" day="2018-02-11">2018-02-11</textarea>
            <textarea placeholder="12" name="day" day="2018-02-12">2018-02-12</textarea>
            <textarea placeholder="13" name="day" day="2018-02-13">2018-02-13</textarea>
            <textarea placeholder="14" name="day" day="2018-02-14">2018-02-14</textarea>

            <textarea placeholder="15" name="day" day="2018-02-15">2018-02-15</textarea>
            <textarea placeholder="16" name="day" day="2018-02-16">2018-02-16</textarea>
            <textarea placeholder="17" name="day" day="2018-02-17">2018-02-17</textarea>
            <textarea placeholder="18" name="day" day="2018-02-18">2018-02-18</textarea>

            <textarea placeholder="19" name="day" day="2018-02-19">2018-02-19</textarea>
            <textarea placeholder="20" name="day" day="2018-02-20">2018-02-20</textarea>
            <textarea placeholder="21" name="day" day="2018-02-21">2018-02-21</textarea>
            <textarea placeholder="22" name="day" day="2018-02-22">2018-02-22</textarea>

            <textarea placeholder="23" name="day" day="2018-02-23">2018-02-23</textarea>
            <textarea placeholder="24" name="day" day="2018-02-24">2018-02-24</textarea>
            <textarea placeholder="25" name="day" day="2018-02-25">2018-02-25</textarea>
            <textarea placeholder="26" name="day" day="2018-02-26">2018-02-26</textarea>

            <textarea placeholder="27" name="day" day="2018-02-27">2018-02-27</textarea>
            <textarea placeholder="28" name="day" day="2018-02-28">2018-02-28</textarea>
        </tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
    </table>
</form>

js,从textarea中删除“名称”。在drawTable中添加事件监听器“onchange”

<form class="" action="index.php" method="post">
    <input id="day" type="hidden" name="day" value="">
    <table id="table" cellspacing="0" cellpadding="0"   border-collapse="collapse";>
    <br>
    <input id="btn"  type="submit" name="" value="Send">
</form>

PHP文件应该是好的