使用PHP将数据从Javascript插入MySQL

时间:2016-09-08 12:48:29

标签: javascript php mysql ajax

我有一个日历和时间日历。用户可以选择可用的日期和时间。我将这些信息保存在数据库中。目前,如果用户选择例如星期一从08:00到10:00,我将每个插槽一行保存在数据库中。

Calendar

Information saved in the db

我想要做的是只保存一行(最后一行)。而不是所有这些。所以基本上,我需要保存点击点击的信息。我真的不知道怎么做到这一点。这是到目前为止的代码:

 function isSlotSelected($slot) { return $slot.is('[data-selected]'); }
 function isSlotSelecting($slot) { return $slot.is('[data-selecting]'); }

 /**
  * Get the selected time slots given a starting and a ending slot
  * @private
  * @returns {Array} An array of selected time slots
  */
 function getSelection(plugin, $a, $b) {
   var $slots, small, large, temp;
   if (!$a.hasClass('time-slot') || !$b.hasClass('time-slot') ||
       ($a.data('day') != $b.data('day'))) { return []; }
   $slots = plugin.$el.find('.time-slot[data-day="' + $a.data('day') + '"]');
   small = $slots.index($a); large = $slots.index($b);
   if (small > large) { temp = small; small = large; large = temp; }
   return $slots.slice(small, large + 1);
 }

 DayScheduleSelector.prototype.attachEvents = function () {
   var plugin = this
  , options = this.options
  , $slots;

 this.$el.on('click', '.time-slot', function () {
  var day = $(this).data('day');
  if (!plugin.isSelecting()) {  // if we are not in selecting mode
    if (isSlotSelected($(this))) { plugin.deselect($(this)); }
    else {  // then start selecting
      plugin.$selectingStart = $(this);
      $(this).attr('data-selecting', 'selecting');
      plugin.$el.find('.time-slot').attr('data-disabled', 'disabled');
      plugin.$el.find('.time-slot[data-day="' + day + '"]').removeAttr('data-disabled');
    }
  } else {  // if we are in selecting mode
    if (day == plugin.$selectingStart.data('day')) {  // if clicking on the same day column
      // then end of selection
      plugin.$el.find('.time-slot[data-day="' + day + '"]').filter('[data-selecting]')
        .attr('data-selected', 'selected').removeAttr('data-selecting');
      plugin.$el.find('.time-slot').removeAttr('data-disabled');
      plugin.$el.trigger('selected.artsy.dayScheduleSelector', [getSelection(plugin, plugin.$selectingStart, $(this))]);
      plugin.$selectingStart = null;
    }
  }
});

this.$el.on('mouseover', '.time-slot', function () {
  var $slots, day, start, end, temp, endAux;
  if (plugin.isSelecting()) {  // if we are in selecting mode
    day = plugin.$selectingStart.data('day');
    $slots = plugin.$el.find('.time-slot[data-day="' + day + '"]');
    $slots.filter('[data-selecting]').removeAttr('data-selecting');
    start = $slots.index(plugin.$selectingStart);
    end = $slots.index(this);
    if (end < 0) return;  // not hovering on the same column
    if (start > end) { temp = start; start = end; end = temp; }
    $slots.slice(start, end + 1).attr('data-selecting', 'selecting');
  }

$.ajax({
  url:   "/Member/test.php",
  dataType:"json",
  type:  "POST",
  data:  {
    day,
    start,
    end
    } 

}).success( function( weekDay, startTime, endTime) {
  console.log( weekDay );
  console.log( startTime );
  console.log( endTime );
}).error( function( error ) {
  console.log( "error:", error );
})
    });
  };

这是我在数据库中保存信息的PHP:

<?php
include 'connection.php';
session_start();
$raw_json = json_encode( $_POST );
if($raw_json != "[]"){
$sql = "INSERT INTO Users (day) VALUES ('$raw_json')";

if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
} else {
        echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

您正在mouseover .time-slotthis.$el.on('mouseover', '.time-slot', function () {元素(可能是日历中的一个矩形)向您的服务器发出请求:

mouseover

因此,如果您从上午8点开始并拖到上午10点,每次用户将鼠标悬停在.time-slot元素上时都会触发mouseup事件,从而导致执行多个查询。您可能希望使用.time-slot事件,并检查上一个mouseover: lastSlot = element hovered over mouseup: send request to server with lastSlot 悬停的内容。

在psuedo-code中是这样的:

selected.artsy.dayScheduleSelector

看到您正在使用DayScheduleSelector,选择后插件fires an event

$("#weekly-schedule").on('selected.artsy.dayScheduleSelector', function (e, selected) { /* selected is an array of time slots selected this time. */ /* pop the last element of selected and execute your request */ }

  

进行选择时触发。将事件和一组选定的时隙传递给事件处理程序。

selected

如果您只想要最后选择的条目,pop it off Sub processCopy(file) 'Abrir documento Workbooks.Open Filename:=file, UpdateLinks:=0 cantn = ThisWorkbook.Sheets.Count cantv = Sheets.Count - 5 cantn = cantn - 4 'Recorrer los libros del documento abierto For i = 1 To (Sheets.Count - 5) 'Obtener nombre del libro seleccionado nombre = Sheets(i + 2).Name 'Filtrar los libros no necesarios If nombre <> "Instructions" And nombre <> "Executive Summary" And nombre <> "Process Update" And nombre <> "Template" And nombre <> "Notes" Then If cantv >= cantn Then ThisWorkbook.Sheets(3).copy after:=ThisWorkbook.Sheets(cantn + 2) cantn = cantn + 1 ThisWorkbook.Sheets(cantn + 2).Name = nombre ElseIf nombre <> ThisWorkbook.Sheets(cantn + 2).Name Then ThisWorkbook.Sheets(cantn + 2).Name = nombre End If Workbooks.Open Filename:=file, UpdateLinks:=0 ActiveWorkbook.Worksheets(nombre).Activate ActiveWorkbook.Sheets(nombre).Range("C4:O23").Select Selection.copy ThisWorkbook.Sheets(nombre).Range("C4:O23").PasteSpecial 数组并向其发送请求。这不是一个确切的实现,但这应该给你足够的指针,使其适应你的需要。

另外,您当前的查询易受MySQL注入攻击。请阅读:How can I prevent SQL-injection in PHP?。您的代码格式错误且不清晰,这并不能真正激励人们首先帮助您;理解你的代码所做的事情(n&#t; t)需要花费更多的时间,而不是提供有用的帮助。