Я работаю в проекте, где я должен обновить данные базы данных с помощью Kendo Grid. Поэтому я использую свойство CURD Kendo с PHP в качестве языка на стороне сервера.
Проблема, с которой я сталкиваюсь, заключается в том, что данные не обновляются, когда я нажимаю кнопку «Сохранить».
Я не нашел ни одного блога или сайта, который бы объяснял, как правильно использовать CURD с PHP, но я нашел один блог и выполнил ту же процедуру, что и в ссылке: http://www.telerik.com/blogs/get-rolling-with-kendo-ui-and-php-ndash-part-2
Ниже мой код:
<!DOCTYPE html>
<html>
<head>
<title>Set Goals</title><link rel="stylesheet" href="Kendostyles/kendo.common.min.css" />
<link rel="stylesheet" href="Kendostyles/kendo.flat.min.css" />
<link rel="stylesheet" href="Kendostyles/kendo.flat.mobile.min.css" /><script src="Kendojs/jquery.min.js"></script>
<script src="Kendojs/kendo.all.min.js"></script>
<script>$(document).ready(function () {
var crudServiceBaseUrl = "http://localhost/Test/Goals_data.php",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl,
dataType: "json"},
update: {
url: crudServiceBaseUrl,
type: "POST",
dataType: "json",
error: function(e) {
alert(e.responseText);
}
},
destroy: {
url: crudServiceBaseUrl ,
dataType: "json"},
create: {
url: crudServiceBaseUrl ,
dataType: "json"},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
pageSize: 20,
schema: {
model: {
id: "month_id",
fields: {
month_id: { editable: false, nullable: false },
month_desc: { validation: { required: true },editable: false },
revenue: { type: "number", validation: { required: true, min: 1} },
wheels_count: { type: "number", validation: { min: 1, required: true } },
hours: { type: "number", validation: { min: 1, required: true } },
invoice_count: { type: "number", validation: { min: 1, required: true } },}
/*Revtotal: function() {
return this.get("UnitPrice") * this.get("UnitsInStock");
}*/
}
},
save: function(){
this.refresh();
},
aggregate: [ { field: "month_desc", aggregate: "count" },
{ field: "revenue", aggregate: "sum" },
{ field: "wheels_count", aggregate: "sum" },
{ field: "hours", aggregate: "sum" },
{ field: "invoice_count", aggregate: "sum" },
//{field: "Revtotal", aggregate: "sum"}
]
});
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,columns: [
{ field:"month_desc", title: "Month" ,editable: false,aggregates: ["count"], footerTemplate: "Total ", width: "100px"},
{ field: "revenue", title:"Revenue", format: "{0:c}", width: "120px" ,aggregates: ["sum"],footerTemplate: "#=sum#"},
{ field: "wheels_count", title:"Wheels", width: "120px",aggregates: ["sum"],footerTemplate: "#=sum#"},
{ field: "hours", title:"Hours", width: "120px",aggregates: ["sum"],footerTemplate: "#=sum#"},
{ field: "invoice_count", title:"Invoice", width: "120px",aggregates: ["sum"],footerTemplate: "#=sum#"},
/* {
field: "Revtotal",
title: "Total",
width: "120px",
editable: false,
template: "#= Revtotal() #",
aggregates: ["sum"],footerTemplate: "#= sum #"},*/
],editable: true,
navigable: true, // enables keyboard navigation in the grid
toolbar: [ "save", "cancel" ] // adds save and cancel buttons
});
/* var item = {
revenue: $("#txtrevenue").val(),
wheels_count: $("#txtwheels_count").val(),
hours: $("#txthours").val(),
invoice_count: $("#txtinvoice_count").val(),
}
dataSource.add(item);
dataSource.sync();
if(dataSource.sync())
{
console.log("done");
}*/
});
</script></head>
<body>
<div id="grid" width=100% height=90%></div>
<a href="#" id="rt">return</a></body></html>
**PHP code:**
<?php
require 'connection.php';
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];// handle a GET
if ($verb == "GET") {
$query2= mysql_query("select substr(a12.month_desc,1,3) month_desc,a12.month_id,a11.revenue,a11.wheels_count,a11.hours,a11.invoice_count from fct_order_month a11 left outer join dim_month a12 on a11.month_id=a12.month_id where a11.franchise_id='1' and a12.month_end_date >'2013-01-01'
and a12.month_start_date <'2013-12-31' group by a11.month_id,a11.franchise_id");
$arr= array();
while ($row2=mysql_fetch_assoc($query2)) {
$arr[]=$row2;
}
echo json_encode($arr);
}
// handle a POST
if ($verb == "POST") {
// DISCLAIMER: It is better to use PHP prepared statements to communicate with the database.
// this provides better protection against SQL injection.
// [http://php.net/manual/en/pdo.prepared-statements.php][4]
// get the parameters from the post. escape them to protect against sql injection.
$rev = mysql_real_escape_string($_POST["revenue"]);
$whl = mysql_real_escape_string($_POST["wheels_count"]);
$hr = mysql_real_escape_string($_POST["hours"]);
$ic = mysql_real_escape_string($_POST["invoice_count"]);
$mon = mysql_real_escape_string($_POST["month_id"]);$rs = mysql_query("UPDATE fct_order_month SET revenue = '" . $rev ."',wheels_count = '" . $whl ."',hours = '" . $hr ."',invoice_count = '" . $ic ."' WHERE month_id = '" .$mon. "' and franchise_id='1' ");
if ($rs) {
echo json_encode($rs);
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo "Update failed for Month: ";
}}?>
где я делаю ошибку?
Может ли кто-нибудь помочь мне в этом.
Задача ещё не решена.
Других решений пока нет …