我可以将复选框值保存在我的 SQL 数据库中,但是当我加载数据时,所有内容都会被检查。有谁知道这是为什么吗?
问题区域:
<td><input type="checkbox" <?php if(isset($item['staged'])) echo "checked=\"checked\""?> data-type="checkbox" name="data[InvoiceDetail][<?php echo $key;?>][staged]" id="staged_<?php echo $key+1?>" class="form-control autocomplete_txt" autocomplete="off"></td>
完整 HTML 代码:
<?php if(isset($invoice['InvoiceDetail'])&&!empty($invoice['InvoiceDetail'])){?>
<?php foreach ( $invoice['InvoiceDetail'] as $key=>$item){?>
<tr>
<td> <input class="case" type="checkbox"/> </td>
<td><input value="<?php echo isset($item['product_id']) ? $item['product_id']: ''; ?>" type="text" data-type="productCode" name="data[InvoiceDetail][<?php echo $key;?>][product_id]" id="itemNo_<?php echo $key+1?>" class="form-control autocomplete_txt" autocomplete="off"></td>
<td><input value="<?php echo isset($item['productName']) ? $item['productName']: ''; ?>" type="text" data-type="productName" name="data[InvoiceDetail][<?php echo $key;?>][productName]" id="itemName_<?php echo $key+1?>" class="form-control autocomplete_txt" autocomplete="off"></td>
<td><input value="<?php echo isset($item['price']) ? $item['price']: ''; ?>" type="number" name="data[InvoiceDetail][<?php echo $key;?>][price]" id="price_<?php echo $key+1?>" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
<td>
<input value="<?php echo isset($item['quantity']) ? $item['quantity']: ''; ?>" type="number" name="data[InvoiceDetail][<?php echo $key;?>][quantity]" id="quantity_<?php echo $key+1?>" class="form-control changesNo quanyityChange" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
<input value="<?php echo isset($item['quantityInStock']) ? $item['quantityInStock']: ''; ?>" type="hidden" id="stock_<?php echo $key+1?>" autocomplete="off"/>
<input value="0" type="hidden" id="stockMaintainer_<?php echo $key+1?>" name="data[InvoiceDetail][<?php echo $key;?>][stockMaintainer]" autocomplete="off"/>
<input value="<?php echo isset($item['quantity']) ? $item['quantity']: ''; ?>" type="hidden" id="previousQuantity_<?php echo $key+1?>" autocomplete="off"/>
<input value="<?php echo isset($item['id']) ? $item['id']: ''; ?>" type="hidden" id="invoiceDetailId_<?php echo $key+1?>" autocomplete="off"/>
</td>
<td><input value="<?php echo $item['price']*$item['quantity']; ?>" type="number" id="total_<?php echo $key+1?>" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
<td><input type="checkbox" <?php if(isset($item['staged'])) echo "checked=\"checked\""?> data-type="checkbox" name="data[InvoiceDetail][<?php echo $key;?>][staged]" id="staged_<?php echo $key+1?>" class="form-control autocomplete_txt" autocomplete="off"></td>
</tr>
<?php } ?>
<?php }else{?>
<tr>
<td><input class="case" type="checkbox"/></td>
<td><input type="text" data-type="productCode" name="data[InvoiceDetail][0][product_id]" id="itemNo_1" class="form-control autocomplete_txt" autocomplete="off"></td>
<td><input type="text" data-type="productName" name="data[InvoiceDetail][0][productName]" id="itemName_1" class="form-control autocomplete_txt" autocomplete="off"></td>
<td><input type="number" name="data[InvoiceDetail][0][price]" id="price_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
<td>
<input type="number" name="data[InvoiceDetail][0][quantity]" id="quantity_1" class="form-control changesNo quanyityChange" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
<input type="hidden" id="stock_1" autocomplete="off"/>
<input type="hidden" name="data[InvoiceDetail][0][stockMaintainer]" id="stockMaintainer_1" autocomplete="off"/>
<input type="hidden" id="previousQuantity_1" autocomplete="off"/>
<input type="hidden" id="invoiceDetailId_1" autocomplete="off"/>
</td>
<td><input type="number" id="total_1" class="form-control totalLinePrice addNewRow" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
<td><input value="1" type="checkbox" name="data[InvoiceDetail][0][staged]" id="staged_1" class="form-control autocomplete_txt" autocomplete="off"></td>
</tr>
<?php } ?>
对应的SQL代码:
private function saveInvoiceDetail(array $invoice_details, $invoice_id = ''){ //$this->pre($invoice_details);
$deleteQuery = "DELETE FROM invoice_details WHERE invoice_id = $invoice_id";
mysqli_query($this->_con, $deleteQuery);
foreach ($invoice_details as $invoice_detail){
$product_id = mysqli_real_escape_string( $this->_con, trim( $invoice_detail['product_id'] ) );
$productName = mysqli_real_escape_string( $this->_con, trim( $invoice_detail['productName'] ) );
$quantity = mysqli_real_escape_string( $this->_con, trim( $invoice_detail['quantity'] ) );
$price = mysqli_real_escape_string( $this->_con, trim( $invoice_detail['price'] ) );
$staged = mysqli_real_escape_string( $this->_con, trim( $invoice_detail['staged'] ) );
$stockMaintainer = mysqli_real_escape_string( $this->_con, trim( $invoice_detail['stockMaintainer'] ) );
if( $stockMaintainer != 0){
$updateQuery = "UPDATE products SET quantityInStock = ( quantityInStock - ($stockMaintainer) ) WHERE productCode = '$product_id'";
mysqli_query($this->_con, $updateQuery);
}
$query = "INSERT INTO invoice_details (`id`, `invoice_id`, `product_id`, productName, `quantity`, `price`, `staged`)
VALUES (NULL, '$invoice_id', '$product_id', '$productName', '$quantity', '$price', '$staged')";
//$this->pre($query);
mysqli_query($this->_con, $query);
}
}
private function restructure_array(array $arr)
{
$result = array();
foreach ($arr as $key => $value) {
for ($i = 0; $i < count($value); $i++) {
$result[$i][$key] = $value[$i];
}
}
return $result;
}
public function getInvoice( $id = ''){
if( !empty( $id ) ){
$query = "SELECT * FROM invoices where uuid ='$id'";
$result = mysqli_query($this->_con, $query);
$data = mysqli_fetch_assoc($result);
$invoiceDetail = array();
$clientDetail = array();
if(isset( $data['client_id'] ) && !empty( $data['client_id'] ))$clientDetail = $this->getClientDetail($data['client_id'] );
if( isset( $data['id'] ) && !empty( $data['id'] )){
$invoice_id = $data['id'];
$query1 = "SELECT InvoiceDetail.id, Product.productName, Product.quantityInStock, InvoiceDetail.product_id, InvoiceDetail.productName, InvoiceDetail.quantity,
InvoiceDetail.staged, InvoiceDetail.price FROM invoice_details InvoiceDetail LEFT JOIN products
Product ON InvoiceDetail.product_id = Product.productCode WHERE InvoiceDetail.invoice_id = $invoice_id ORDER by InvoiceDetail.id ASC";
$invoiceDetailResult = mysqli_query($this->_con, $query1);
while($row = mysqli_fetch_assoc($invoiceDetailResult)){
array_push($invoiceDetail, $row);
}
}else{
throw new Exception( FIELDS_MISSING );
}
if(empty($invoiceDetail)){
$result = array( 'Invoice'=> $data, 'Client' => $clientDetail);
}else{
$result = array( 'Invoice'=> $data, 'Client' => $clientDetail, 'InvoiceDetail'=> $invoiceDetail);
}
return $result;
}
}
最佳答案
如果$item['staged']
将是0
或1
然后isset()
单独使用可能是错误的函数。
我假设这是处理来自数据库的数据,所以它不像处理 $_POST/$_GET
中的复选框。数组,因为即使在屏幕上未选中它,它也将始终存在,因为如果在屏幕上未选中它,您应该将 0 写入数据库。
尝试
<td>
<input type="checkbox" <?php if(isset($item['staged']) && $item['staged'] == 1) echo "checked=\"checked\""?>
data-type="checkbox"
name="data[InvoiceDetail][<?php echo $key;?>][staged]"
id="staged_<?php echo $key+1?>"
class="form-control autocomplete_txt"
autocomplete="off">
</td>
事实上,您可能会丢失 isset()
完全并且只是代码
<td>
<input type="checkbox" <?php if($item['staged'] == 1) echo "checked=\"checked\""?>
data-type="checkbox"
name="data[InvoiceDetail][<?php echo $key;?>][staged]"
id="staged_<?php echo $key+1?>"
class="form-control autocomplete_txt"
autocomplete="off">
</td>
回复评论
就像我上面所说的,在处理来自 $_POST/$_GET
的数据时数组中未选中的复选框将不存在于 $_POST/$_GET
中数组,因此您必须使用 isset()
测试它的存在
例如:
$staged = isset( $invoice_detail['staged']) ? 1 : 0;
关于javascript - 在 js php/html 数组中保存/检索复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32192217/