我已经下载了Editable Grid,我发现它是一个非常有用的工具。目前我在向数据库添加新行时遇到问题。到目前为止,数据正从数据库中提取并显示在网格上,它还允许我更新和删除数据。当我尝试添加数据时,每次单击“添加”并弹出错误?
DatabaseGrid.prototype.addRow = function(brandId)
{
var self = this;
$.ajax({
url: 'add.php',
type: 'POST',
dataType: "html",
data: {
tablename : self.editableGrid.name,
brandName: $("#brandName").val(),
brandCategory: $("#brandCategory").val(),
brandKeyword: $("#brandKeyword").val()
//addedBy: $("#addedBy").val(),
//dateTime: $("#dateTime").val()
},
success: function (response)
{
if (response == "ok" ) {
// hide form
showAddForm();
$("#brandName").val('');
$("#brandCategory").val('');
$("#brandKeyword").val('');
//$("#addedBy").val('');
//$("#dateTime").val('');
alert("Row added : reload model");
self.fetchGrid();
}
else
alert("error");
console.log(message);
},
error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + errortext); },
async: true
});
};
PHP
<?php
require_once('config.php');
// Database connection
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$mysqli->real_connect($config['db_host'],$config['db_user'],$config['db_password'],$config['db_name']);
// Get all parameter provided by the javascript
$brandName = $mysqli->real_escape_string(strip_tags($_POST['brandName']));
$brandCategory = $mysqli->real_escape_string(strip_tags($_POST['brandCategory']));
$brandKeyword = $mysqli->real_escape_string(strip_tags($_POST['brandKeyword']));
//$addedBy = $mysqli->real_escape_string("kate");
//$dateTime = $mysqli->real_escape_string("CURRENT_TIMESTAMP");
$tablename = $mysqli->real_escape_string(strip_tags($_POST['tablename']));
$return=false;
if ( $stmt = $mysqli->prepare("INSERT INTO ".$tablename." (brandName, brandCategory, brandKeyword) VALUES ( ?, ?, ?)")) {
$stmt->bind_param("ss", $brandName, $brandCategory, $brandKeyword);
$return = $stmt->execute();
$stmt->close();
}
$mysqli->close();
echo $return ? "ok" : "error";
HTML
var datagrid = new DatabaseGrid();
window.onload = function() {
// key typed in the filter field
$("#filter").keyup(function() {
datagrid.editableGrid.filter( $(this).val());
// To filter on some columns, you can set an array of column index
//datagrid.editableGrid.filter( $(this).val(), [0,3,5]);
});
$("#showaddformbutton").click( function() {
showAddForm();
});
$("#cancelbutton").click( function() {
showAddForm();
});
$("#addbutton").click(function() {
datagrid.addRow();
});
};
</script>
<!-- simple form, used to add a new row -->
<div id="addform">
<div class="row">
<input type="text" id="brandName" name="brandName" placeholder="Brand Name" />
</div>
<div class="row">
<select id="brandCategory" name="brandCategory">
<option value="">Select...</option>
<option value="Arts and Entertainment">Arts and Entertainment</option>
<option value="Automotive">Automotive</option>
<option value="Business">Business</option>
<option value="Careers">Careers</option>
<option value="Education">Education</option>
<option value="Family and Parenting">Family and Parenting</option>
<option value="Health and Fitness">Health and Fitness</option>
<option value="Food and Drink">Food and Drink</option>
<option value="Hobbies and Interests">Hobbies and Interests</option>
<option value="Home and Garden">Home and Garden</option>
<option value="Law, Government and Politics">Law, Government and Politics</option>
<option value="News">News</option>
<option value="Personal Finance">Personal Finance</option>
<option value="Pets">Pets</option>
<option value="Society">Society</option>
<option value="Science">Science</option>
<option value="Shopping">Shopping</option>
<option value="Sports">Sports</option>
<option value="Style and Fashion">Style and Fashion</option>
<option value="Technology and Computing">Technology and Computing</option>
<option value="Travel">Travel</option>
<option value="Real Estate">Real Estate</option>
<option value="Religion and Spirituality">Religion and Spirituality</option>
</select>
</div>
<div class="row">
<textarea name="brandKeyword" id="brandKeyword" placeholder="Add Keywords"></textarea>
</div>
<div class="row tright">
<a id="addbutton" class="button green" ><i class="fa fa-save"></i> Add</a>
<a id="cancelbutton" class="button delete">Cancel</a>
</div>
</div>