我正在尝试使用Ajax来编辑来自MySQL的表。我遇到的问题是这个代码在MySQL数据库中返回null。我注意到它正确地找到了ID,但是,当我提交时,其余的值都没有转移。
任何帮助将不胜感激。
我有一个包含6列“ID(int),LICENSE_NUMBER(int),LICENSE_TYPE(varchar),TRADE_NAME(varchar),CR_NUM(int)”的表 这是我的Ajax:
<script>
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
function editData(str) {
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("info").innerHTML=xmlhttp.responseText;
getData();
}
}
var idx = str;
var LICENSE_NUMBERx = document.getElementById("LICENSE_NUMBER-"+str).value; // license_number
var LICENSE_TYPEx = document.getElementById("LICENSE_TYPE-"+str).value; // LICENSE_TYPE
var TRADE_NAMEx = document.getElementById("TRADE_NAME-"+str).value; // LICENSE_TYPE
var CR_NUMx = document.getElementById("CR_NUM-"+str).value; // CR_NUM
//var dsx = encodeURIComponent(document.getElementById("ds-"+str).value);
//var dtx = document.getElementById("dt-"+str).value;
var params = "LICENSE_NUMBER="+LICENSE_NUMBERx+"&LICENSE_TYPE="+LICENSE_TYPEx+"&TRADE_NAME="+TRADE_NAMEx+"&CR_NUM="+CR_NUMx+"&id="+idx;
xmlhttp.open("POST","projecteditdata.php");
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(params);
}
function deleteData(str) {
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("info").innerHTML=xmlhttp.responseText;
getData();
}
}
var idx = str;
var params = "&id="+idx;
xmlhttp.open("POST","projectdeletedata.php");
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(params);
}
function getData(){
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("mytab").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","projectgetdata.php",true);
xmlhttp.send();
}
</script>
这是SQL:
$sql = "SELECT * FROM lcb_table WHERE ID IS NOT NULL ORDER BY LICENSE_NUMBER";
/*$result = $conn->query($sql)*/
$result = $conn->query($sql);
echo "
<table class='table table-bordered table-striped' id='tblData'>
<thead>
<tr>
<th colspan='2'> Modifiy Record in Table </th>
<th>License Number</th>
<th>License Type</th>
<th>Trade Name</th>
<th>Central Registration Number CR</th>
</tr>
</thead>";
// Loop through database
while($row = $result->fetch_assoc()) {
$id = $row['ID'];
$license_number = $row['LICENSE_NUMBER'];
$license_type = $row['LICENSE_TYPE'];
$trade_name = $row['TRADE_NAME'];
$cr_num = $row['CR_NUM'];
echo "
<tbody>
<tr>
<td><button type='button' class='btn btn-warning btn-md active fa fa-pencil' data-toggle='modal' data-target='#update-" . $id . "'></button></td>
<td><button type='button' class='btn btn-danger btn-md active fa fa-trash-o fa' data-toggle='modal' data-target='#delete-" . $id . "'></button></td>
<td>".$license_number."</td>
<td>".$license_type."</td>
<td>".$trade_name."</td>
<td>".$cr_num."</td>
</tr>
</tbody>";
这是从projectgetdata.php
调用的表单 <div class="modal-body">
<form>
<fieldset>
<div class='col-sm-2'>
<div class="form-group">
<label style="font-size: 12px" for="LICENSE_NUMBER">License Number</label>
<input type="text" class="form-control" id="LICENSE_NUMBER-<?php echo $id ?>" value="<?php echo $license_number ?>">
</div>
</div>
<div class='col-sm-2'>
<div class="form-group">
<label style="font-size: 12px" for="LICENSE_TYPE">License Type</label>
<input type="text" class="form-control" id="LICENSE_TYPE-<?php echo $id ?>" value="<?php echo $license_number ?>">
</div>
</div>
<div class='col-sm-8'>
<div class="form-group">
<label style="font-size: 12px" for="TRADE_NAME">Trade Name</label>
<input name="TRADE_NAME" type="text" class="form-control" id="TRADE_NAME-<?php echo $id ?>" value="<?php echo $trade_name ?>">
</div>
</div>
<div class='col-sm-4'>
<div class="form-group">
<label style="font-size: 12px" for="CR_NUM">Central Registration Number (CR)</label>
<input name="CR_NUM" type="text" class="form-control" id="CR_NUM-<?php echo $id ?>" value="<?php echo $cr_num ?>">
</div>
</div>
</fieldset>
</form>
这是projectedit.php方面
<?php
include("../includes/config.php");
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: " . $conn->connect_error;
}
$id = $_POST['ID'];
$stmt = $conn->prepare("UPDATE lcb_table SET LICENSE_NUMBER=?,LICENSE_TYPE=?,TRADE_NAME=?,CR_NUM=? where ID=?");
$stmt->bind_param('sssss', $license_number,$license_type,$trade_name,$cr_num,$id);
$license_number = $POST['LICENSE_NUMBER'];
$license_type = $POST['LICENSE_TYPE'];
$trade_name = $POST['TRADE_NAME'];
$cr_num = $POST['CR_NUM'];
if($stmt->execute()){
echo "Success";
} else{
echo "Error, No Data";
}
$conn->close();
?>