我是javascript的初学者,我在所有这些方面都有点迷失。
我希望在我的sql数据库中推送我手中的所有数据。我按照双手提供的例子,但它不起作用。
以下是代码,我在其中创建了handontable:
`
$(document).ready(function () {
var container = document.getElementById('Grille_competences');
var headerRenderer = function (instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.fontWeight = 'bold';
td.style.textAlign = 'center';
td.style.backgroundColor = '#B0C4DE';
};
//Initialisation des données de la grille.
var data = [["Compétences","Description","Code",""],
["", "", "",""],
["","", "",""],
["","", "",""],
["","", "",""],
["","", "",""]];
//Création de la grille
hot = new Handsontable(container, {
data: data,
height: 800,
width: 1183,
colWidths: [35,200,25,80],
manualRowResize: true,
colHeaders: true,
rowHeaders: true,
mergeCells: true,
stretchH: 'all',
columnSorting: true,
contextMenu: true,
contextMenuCopyPaste: {
swfPath: './zeroclipboard/dist/ZeroClipboard.swf'
},
//Fonctionnalités lors d'un clic droit dans la grille.
contextMenu: {
items: {
"row_above": {
name: 'Insérer ligne au dessus',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
"row_below": {
name: 'Insérer ligne en dessous',
disabled: function() {
return hot.getSelected()[0] === 0;
}
},
"hsep1": "---------",
"col_left": {
name: 'Insérer colonne à gauche',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
"col_right": {
name: 'Insérer colonne à droite',
disabled: function() {
return hot.getSelected()[0] === 0;
}
},
"hsep2": "---------",
"remove_row": {
name: 'Supprimer la ligne',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
"remove_col": {
name: 'Supprimer la colonne',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
"hsep3": "---------",
"copy": {
name:'Copier',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
"paste": {
name: 'Coller',
disabled: function(){
return hot.getSelected()[0] === 0;
}
},
"hsep4": "---------",
"undo": {
name:'Précédent',
disabled: function(){
return hot.getSelected()[0] === 0;
}
},
"redo": {
name: 'Suivant',
disabled: function(){
return hot.getSelected()[0] === 0;
}
},
"hsep5": "---------",
"make_read_only": {
name: 'Lecture seule',
disabled: function() {
return hot.getSelected()[0] === 0;
}
},
"alignment": {
name: 'Alignement du texte',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
"mergeCells": {
name: 'Fusionner les cellules',
disabled: function () {
return hot.getSelected()[0] === 0;
}
},
},
},
//Entetes de la grille en lecture seule.
cells: function(row, col, prop) {
var cellProperties = {};
if(row===0){
cellProperties.renderer = headerRenderer;
}
if(row === 0 && col <3){
cellProperties.readOnly = true;
}
return cellProperties;
}
});
//Lors d'un clic sur le bouton valider, transmission des données de la grille.
});
$('#save').click(function(){
$.ajax({
url: "testGetData.php",
dataType: 'json',
data: {data: hot.getData() },
type: 'GET',
success: function () {
$console.text('Saved !');
}
});
});
`
这是testGetData.php文件的代码:
<?php
session_start();
require_once('./lib/demo/php/functions.php');
$db = getConnection();
createBDD($db);
if($db)
{
$db = new PDO('mysql:host=localhost; dbname=bdd'.$_SESSION['login'],'root', 'passwd');
createTableBDD($db);
foreach ($_GET['data'] as $value){
$query = $db->prepare('INSERT INTO Competences(libelle) VALUES('.$value[0].')');
$query->execute();
}
}
?>
如果我理解,那么handontable会向testGetData.php发送$ _REQUEST。所以,我可以访问$ _GET超全局。考虑到此变量是许多数组的数组,foreach中的$值与其中一个数组重合。但我不明白为什么没有推动。 我修改了我的sql数据库的getConnection()函数。
答案 0 :(得分:0)
我不知道你要对createTableBDD($db);
做些什么。您是否每次调用此页面时都尝试创建新表?你应该只创建一次表(可能在phpmyadmin中),如
CREATE TABLE cm (id int auto_increment primary key, a int NULL, b int NULL, c int NULL )
然后在.php文件中执行以下操作:
// connect to the database:
$p=new PDO("mysql:host=localhost; dbname=$dbname",$uname,$DBpassword);
// in order to demonstrate the back-end without the handson front-end
// I just generate some sample data (6 rows, 3 columns) into array $data:
for ($i=100;$i<700;$data[]=$b,$i+=100) for ($b=array(),$j=1;$j<4;$j++) $b[]=$i+$j;
// this would otherwise come from: $data=$_GET['data'];
// prepare the INSERT statement ONLY ONCE:
$ins=$p->prepare("INSERT INTO cm(a,b,c) VALUES(?,?,?)");
// and run it for each line of the $data array:
foreach ($data as $a) $ins->execute($a);
您可能会发现本教程对开始使用PDO很有帮助:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers#Running_Simple_INSERT.2C_UPDATE.2C_or_DELETE_statements
请注意,此解决方案仅适用于正好 3列的$data
个数组。您在前端的handson表可能会提供任何列数。因此,在INSERT
数据之前,您需要确保列数正确。