我可以在我的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;
}
}
答案 0 :(得分:2)
如果$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
数组中不存在未选中的复选框,因此您必须使用{{1}来测试其存在性}}
例如:
isset()