通过 html table 更新 sql 基础
Update sql base through html table
我有这个 table:
<table id="skuTable" role="grid">
<thead>
<th class="skuRow">Orden</th>
<th class="skuRow">Maquina</th>
<th>Fecha Fab.</th>
<th class="skuRow">Norden</th>
<th>Color</th>
<th>Cliente</th>
<th>Metros</th>
<th>Tiempo</th>
<th>Fecha Ent.</th>
<th>Operario</th>
<th class="skuRow">Editar</th>
</thead>
<tbody>
SQL查询:
<?php
$sql = "SELECT DISTINCT Orden, FFab, N_Orden=Ordenes.OF_N_Orden, Color, Client, Metros, Tiempo, FEnt,Operario
FROM ((
Ordenes INNER JOIN VCLIENTE ON VCLIENTE.Clie_codigo = Ordenes.OF_Cod_Cli
) INNER JOIN ARTITTEC ON ARTITTEC.Tec_codigo = Ordenes.OF_Cod_Art
)
INNER JOIN VTiempos ON VTiempos.Of_n_orden= Ordenes.OF_N_Orden
AND Vtiempos.OF_LIN =Ordenes.OF_Lin
ORDER BY Orden asc, Fecha asc";
$stmt = sqlsrv_query($conn, $sql);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
?>
我连续打印结果,通过函数
确定我要带去POST的3个
<tbody role="rowgroup"><tr data-uid="0bc4355b-a2b7-4cf6-9701-192e77ce6d1d" role="row">
<?php foreach ($maquinas as $maquina): ?>
<?php
$cadena2 = $maquina['OF_Color1'];
$cadena = $maquina['Orden'];
$resultado = str_replace("999", " ", $cadena);
$resultado2 = str_replace("BLACK C", "BLACK ", $cadena2);
?>
<td role="gridcell" class="sku_group" id="sku_group-<?php echo intval ($maquina['Orden'])?>"><span style="color: #ff0000"><strong><?php echo $resultado ?></strong></span></td>
<td role="gridcell"><?php echo $maquina['Fecha'];?></td>
<td role="gridcell" class="sku_maquina" id="sku_maquina-<?php echo intval ($maquina['Maquina'])?>"><?php echo $maquina['Maquina'];?></td>
<td role="gridcell"><?php echo utf8_encode ($maquina['Clie_nombre']);?></td>
<td role="gridcell" class="group_id" align="center" id="group_id-<?php echo intval ($maquina['N_Orden'])?>" ><?php echo $maquina['N_Orden']; ?></form></td>
<td role="gridcell" ><?php echo $maquina['Linea']; ?></td>
<td role="gridcell" > <a target="_blank" href="https://clientes.ealbeniz.com/clientes/images/<?php echo $maquina['OF_Cod_Art'];?>.pdf"><?php echo $maquina['OF_Cod_Art']; ?></a></td>
<td role="gridcell" ><?php echo utf8_encode( $maquina['OF_Descripcion']); ?></td>
<td role="gridcell"><?php echo $maquina['Cant'];?></td>
<td role="gridcell"><?php echo $maquina['Metros']; ?></td>
<td role="gridcell" ><?php echo $maquina['OF_Tipo_Papel'];?></td>
<td role="gridcell"><?php echo $maquina['Tec_Tip_stamp']; ?></td>
<td role="gridcell" ><?php echo $maquina['Tec_Ava_stamp'];?></td>
<td role="gridcell" <?php echo ($maquina['repetido']) ? 'style="color: red; font-weight: bold;"' : ''; ?>><?php echo $maquina['Mag']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Cod_Troq']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Num_Tintas'];?></td>
<td role="gridcell"><?php echo $resultado2 ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color2'];?></td>
<td role="gridcell"><?php echo $maquina['OF_Color3']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color4']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color5']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color6']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color7'];?></td>
<td role="gridcell"><?php echo $maquina['OF_Color8']; ?></td>
<td role="gridcell"><?php echo $maquina['tIMER']; ?></td>
<td role="gridcell"><?php echo $maquina['ESTADO'];?></td>
<td role="gridcell"><input type="button" class="edit" name="edit" value="Edit"></td>
</tr>
<?php endforeach; ?>
</b>
</font>
<?php
?>
</tbody>
</table><div class="k-pager-wrap k-grid-pager k-widget k-floatwrap" data-role="pager"><span class="k-pager-info k-label"></span></div></div>
在php的末尾,我添加了以下功能,用POST必须发送的3个数据到update41.php来更新数据:
// ----- Edit Row -----
$(document).on("click", "#skuTable .edit", function () {
var $this = $(this);
var tds = $this.closest('tr').find('td').filter(function () {
return $(this).find('.edit').length === 0;
});
if ($this.val() === 'Edit') {
$this.val('Save');
if($this.id !== '.sku_group') {
tds.prop('contenteditable', true);
}
} else {
var isValid = true;
var errors = '';
$('#myDialogBox').empty();
var elements = tds;
if (tds.find('input').length > 0) {
elements = tds.find('input');
}
var dict = {};
elements.each(function (index, element) {
var type = $(this).attr('class');
var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
console.log(type);
// ----- Switch statement that provides validation for each table cell -----
switch (type) {
case "group_id":
dict["Group_ID"] = value;
break;
case "sku_group":
dict["SKU Group"] = value;
break;
case "sku_maquina":
dict["SKU Maquina"] = value;
break;
}
})
if (isValid) {
console.log(dict);
$this.val('Edit');
tds.prop('contenteditable', false);
var request = $.ajax({
type: "POST",
url: "update41.php",
data: dict
});
request.done(function (response, textStatus, jqXHR){
if(JSON.parse(response) == true){
console.log("row updated");
} else {
console.log("row failed to updated");
console.log(response);
console.log(textStatus);
console.log(jqXHR);
}
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.log(textStatus);
console.log(jqXHR);
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
});
} else {
alert(errors);
}
}
});
</script>
update41.php:
<?php
$Orden = $_POST['SKU Group'];
$NOrden = $_POST['Group_ID'];
$Maquina =$_POST['SKU Maquina'];
$host="xxxxx";
$dbName="xxxxx";
$dbUser="xxxxx";
$dbPass="xxxxx";
$pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
$sql = "UPDATE Ordenes SET OF_OrdenOFs = '$Orden' WHERE OF_N_Orden = '$NOrden'";
$stmt = $pdo->prepare($sql);
//$stmt->bindValue('[:SKU Group]', $SKU);
//$stmt->bindValue(':Group_ID', $Group_ID)
$result = $stmt->execute();
echo json_encode($result);
if(!$result) {
echo json_encode(sqlsrv_errors());
}
$sql2 = "UPDATE VTiempos SET Maquina = '$Maquina' WHERE Of_n_orden = '$NOrden'";
$stmt2 = $pdo->prepare($sql2);
//$stmt->bindValue('[:SKU Group]', $SKU);
//$stmt->bindValue(':Group_ID', $Group_ID)
$result2 = $stmt2->execute();
echo json_encode($result2);
if(!$result2) {
echo json_encode(sqlsrv_errors());
}
?>
按下保存时控制台中出现以下数据:
sku_group
41prueba.php:459 undefined
41prueba.php:459 sku_maquina
41prueba.php:459 undefined
41prueba.php:459 group_id
2141prueba.php:459 undefined
41prueba.php:474 {SKU Group: "", SKU Maquina: "41", Group_ID: "129236"}
VM25655:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (41prueba.php:484)
at c (jquery-1.10.2.min.js:4)
at Object.fireWith [as resolveWith] (jquery-1.10.2.min.js:4)
at k (jquery-1.10.2.min.js:6)
at XMLHttpRequest.r (jquery-1.10.2.min.js:6)
(anonymous) @ 41prueba.php:484
c @ jquery-1.10.2.min.js:4
fireWith @ jquery-1.10.2.min.js:4
k @ jquery-1.10.2.min.js:6
r @ jquery-1.10.2.min.js:6
XMLHttpRequest.send (async)
send @ jquery-1.10.2.min.js:6
ajax @ jquery-1.10.2.min.js:6
(anonymous) @ 41prueba.php:477
dispatch @ jquery-1.10.2.min.js:5
v.handle @ jquery-1.10.2.min.js:5
这里可以看到它从我按下保存以放置控制台示例的行中获取字段:
41prueba.php:474 {SKU Group: "", SKU Maquina: "41", Group_ID: "129236"}
我不明白为什么它不将数据传递给 update41.php
非常感谢,请原谅我的英语
看起来你的 elements
变量是输入的集合,但你的 HTML 可能没有任何输入,除了一个按钮。
此外,如果您想通过 HTML 传递任意数据,标准方法是使用 data-
属性,例如 data-type
。通过类似 class 的方式传递它是非常有限的(并且对于阅读代码的任何人来说都不清楚)。
错误是在这部分函数中:
case "group_id":
dict["Group_ID"] = value;
break;
case "sku_group":
dict["SKU Group"] = value;
break;
case "sku_maquina":
dict["SKU Maquina"] = value;
break;
SKU Maquina 和 SKU Group 不必分开(无空白 space)。
正确的是(加_):
case "group_id":
dict["Group_ID"] = value;
break;
case "sku_group":
dict["SKU_Group"] = value;
break;
case "sku_maquina":
dict["SKU_Maquina"] = value;
break;
所以在 update41.php 中你必须把它写成相同的:
$NOrden = $_POST['Group_ID'];
$Orden = $_POST['SKU_Group'];
$Maquina =$_POST['SKU_Maquina'];
我有这个 table:
<table id="skuTable" role="grid">
<thead>
<th class="skuRow">Orden</th>
<th class="skuRow">Maquina</th>
<th>Fecha Fab.</th>
<th class="skuRow">Norden</th>
<th>Color</th>
<th>Cliente</th>
<th>Metros</th>
<th>Tiempo</th>
<th>Fecha Ent.</th>
<th>Operario</th>
<th class="skuRow">Editar</th>
</thead>
<tbody>
SQL查询:
<?php
$sql = "SELECT DISTINCT Orden, FFab, N_Orden=Ordenes.OF_N_Orden, Color, Client, Metros, Tiempo, FEnt,Operario
FROM ((
Ordenes INNER JOIN VCLIENTE ON VCLIENTE.Clie_codigo = Ordenes.OF_Cod_Cli
) INNER JOIN ARTITTEC ON ARTITTEC.Tec_codigo = Ordenes.OF_Cod_Art
)
INNER JOIN VTiempos ON VTiempos.Of_n_orden= Ordenes.OF_N_Orden
AND Vtiempos.OF_LIN =Ordenes.OF_Lin
ORDER BY Orden asc, Fecha asc";
$stmt = sqlsrv_query($conn, $sql);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
?>
我连续打印结果,通过函数
确定我要带去POST的3个<tbody role="rowgroup"><tr data-uid="0bc4355b-a2b7-4cf6-9701-192e77ce6d1d" role="row">
<?php foreach ($maquinas as $maquina): ?>
<?php
$cadena2 = $maquina['OF_Color1'];
$cadena = $maquina['Orden'];
$resultado = str_replace("999", " ", $cadena);
$resultado2 = str_replace("BLACK C", "BLACK ", $cadena2);
?>
<td role="gridcell" class="sku_group" id="sku_group-<?php echo intval ($maquina['Orden'])?>"><span style="color: #ff0000"><strong><?php echo $resultado ?></strong></span></td>
<td role="gridcell"><?php echo $maquina['Fecha'];?></td>
<td role="gridcell" class="sku_maquina" id="sku_maquina-<?php echo intval ($maquina['Maquina'])?>"><?php echo $maquina['Maquina'];?></td>
<td role="gridcell"><?php echo utf8_encode ($maquina['Clie_nombre']);?></td>
<td role="gridcell" class="group_id" align="center" id="group_id-<?php echo intval ($maquina['N_Orden'])?>" ><?php echo $maquina['N_Orden']; ?></form></td>
<td role="gridcell" ><?php echo $maquina['Linea']; ?></td>
<td role="gridcell" > <a target="_blank" href="https://clientes.ealbeniz.com/clientes/images/<?php echo $maquina['OF_Cod_Art'];?>.pdf"><?php echo $maquina['OF_Cod_Art']; ?></a></td>
<td role="gridcell" ><?php echo utf8_encode( $maquina['OF_Descripcion']); ?></td>
<td role="gridcell"><?php echo $maquina['Cant'];?></td>
<td role="gridcell"><?php echo $maquina['Metros']; ?></td>
<td role="gridcell" ><?php echo $maquina['OF_Tipo_Papel'];?></td>
<td role="gridcell"><?php echo $maquina['Tec_Tip_stamp']; ?></td>
<td role="gridcell" ><?php echo $maquina['Tec_Ava_stamp'];?></td>
<td role="gridcell" <?php echo ($maquina['repetido']) ? 'style="color: red; font-weight: bold;"' : ''; ?>><?php echo $maquina['Mag']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Cod_Troq']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Num_Tintas'];?></td>
<td role="gridcell"><?php echo $resultado2 ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color2'];?></td>
<td role="gridcell"><?php echo $maquina['OF_Color3']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color4']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color5']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color6']; ?></td>
<td role="gridcell"><?php echo $maquina['OF_Color7'];?></td>
<td role="gridcell"><?php echo $maquina['OF_Color8']; ?></td>
<td role="gridcell"><?php echo $maquina['tIMER']; ?></td>
<td role="gridcell"><?php echo $maquina['ESTADO'];?></td>
<td role="gridcell"><input type="button" class="edit" name="edit" value="Edit"></td>
</tr>
<?php endforeach; ?>
</b>
</font>
<?php
?>
</tbody>
</table><div class="k-pager-wrap k-grid-pager k-widget k-floatwrap" data-role="pager"><span class="k-pager-info k-label"></span></div></div>
在php的末尾,我添加了以下功能,用POST必须发送的3个数据到update41.php来更新数据:
// ----- Edit Row -----
$(document).on("click", "#skuTable .edit", function () {
var $this = $(this);
var tds = $this.closest('tr').find('td').filter(function () {
return $(this).find('.edit').length === 0;
});
if ($this.val() === 'Edit') {
$this.val('Save');
if($this.id !== '.sku_group') {
tds.prop('contenteditable', true);
}
} else {
var isValid = true;
var errors = '';
$('#myDialogBox').empty();
var elements = tds;
if (tds.find('input').length > 0) {
elements = tds.find('input');
}
var dict = {};
elements.each(function (index, element) {
var type = $(this).attr('class');
var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
console.log(type);
// ----- Switch statement that provides validation for each table cell -----
switch (type) {
case "group_id":
dict["Group_ID"] = value;
break;
case "sku_group":
dict["SKU Group"] = value;
break;
case "sku_maquina":
dict["SKU Maquina"] = value;
break;
}
})
if (isValid) {
console.log(dict);
$this.val('Edit');
tds.prop('contenteditable', false);
var request = $.ajax({
type: "POST",
url: "update41.php",
data: dict
});
request.done(function (response, textStatus, jqXHR){
if(JSON.parse(response) == true){
console.log("row updated");
} else {
console.log("row failed to updated");
console.log(response);
console.log(textStatus);
console.log(jqXHR);
}
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.log(textStatus);
console.log(jqXHR);
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
});
} else {
alert(errors);
}
}
});
</script>
update41.php:
<?php
$Orden = $_POST['SKU Group'];
$NOrden = $_POST['Group_ID'];
$Maquina =$_POST['SKU Maquina'];
$host="xxxxx";
$dbName="xxxxx";
$dbUser="xxxxx";
$dbPass="xxxxx";
$pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
$sql = "UPDATE Ordenes SET OF_OrdenOFs = '$Orden' WHERE OF_N_Orden = '$NOrden'";
$stmt = $pdo->prepare($sql);
//$stmt->bindValue('[:SKU Group]', $SKU);
//$stmt->bindValue(':Group_ID', $Group_ID)
$result = $stmt->execute();
echo json_encode($result);
if(!$result) {
echo json_encode(sqlsrv_errors());
}
$sql2 = "UPDATE VTiempos SET Maquina = '$Maquina' WHERE Of_n_orden = '$NOrden'";
$stmt2 = $pdo->prepare($sql2);
//$stmt->bindValue('[:SKU Group]', $SKU);
//$stmt->bindValue(':Group_ID', $Group_ID)
$result2 = $stmt2->execute();
echo json_encode($result2);
if(!$result2) {
echo json_encode(sqlsrv_errors());
}
?>
按下保存时控制台中出现以下数据:
sku_group
41prueba.php:459 undefined
41prueba.php:459 sku_maquina
41prueba.php:459 undefined
41prueba.php:459 group_id
2141prueba.php:459 undefined
41prueba.php:474 {SKU Group: "", SKU Maquina: "41", Group_ID: "129236"}
VM25655:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (41prueba.php:484)
at c (jquery-1.10.2.min.js:4)
at Object.fireWith [as resolveWith] (jquery-1.10.2.min.js:4)
at k (jquery-1.10.2.min.js:6)
at XMLHttpRequest.r (jquery-1.10.2.min.js:6)
(anonymous) @ 41prueba.php:484
c @ jquery-1.10.2.min.js:4
fireWith @ jquery-1.10.2.min.js:4
k @ jquery-1.10.2.min.js:6
r @ jquery-1.10.2.min.js:6
XMLHttpRequest.send (async)
send @ jquery-1.10.2.min.js:6
ajax @ jquery-1.10.2.min.js:6
(anonymous) @ 41prueba.php:477
dispatch @ jquery-1.10.2.min.js:5
v.handle @ jquery-1.10.2.min.js:5
这里可以看到它从我按下保存以放置控制台示例的行中获取字段:
41prueba.php:474 {SKU Group: "", SKU Maquina: "41", Group_ID: "129236"}
我不明白为什么它不将数据传递给 update41.php
非常感谢,请原谅我的英语
看起来你的 elements
变量是输入的集合,但你的 HTML 可能没有任何输入,除了一个按钮。
此外,如果您想通过 HTML 传递任意数据,标准方法是使用 data-
属性,例如 data-type
。通过类似 class 的方式传递它是非常有限的(并且对于阅读代码的任何人来说都不清楚)。
错误是在这部分函数中:
case "group_id":
dict["Group_ID"] = value;
break;
case "sku_group":
dict["SKU Group"] = value;
break;
case "sku_maquina":
dict["SKU Maquina"] = value;
break;
SKU Maquina 和 SKU Group 不必分开(无空白 space)。
正确的是(加_):
case "group_id":
dict["Group_ID"] = value;
break;
case "sku_group":
dict["SKU_Group"] = value;
break;
case "sku_maquina":
dict["SKU_Maquina"] = value;
break;
所以在 update41.php 中你必须把它写成相同的:
$NOrden = $_POST['Group_ID'];
$Orden = $_POST['SKU_Group'];
$Maquina =$_POST['SKU_Maquina'];