0

I can save the checkboxes value just fine in my sql database, but when I go to load the data, EVERYTHING is checked. Does anyone know why that is?

Problem Area :

 <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>

Full HTML Code :

<?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 } ?>

Corresponding SQL Code :

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;
        }
    }
3
  • so what's in $item['staged']? an empty string is still "set" and would cause you to output a checked attribute. Commented Aug 24, 2015 at 21:45
  • @MarcB $item['staged'] should be either a 1 or a 0, 1 if it is checked. The value "1" is saved just fine into my sql database, but the program does not read "1" and output a check to the checkbox, it just checks everything :P Commented Aug 24, 2015 at 21:47
  • what about 0? that's still a "set" value. isset is basically "exists and the value is not null". you should probably test for != 1 or something instead. Commented Aug 24, 2015 at 21:58

1 Answer 1

2

If $item['staged'] will be 0 or 1 then isset() on its own is probably the wrong function to use on its own.

I assume this is processing data from the database, so its not like when you process a checkbox from the $_POST/$_GET arrays, as it will always be there even if it was unchecked on the screen because you should then have written 0 to the database if it was unchecked on the screen.

Try

<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>

In fact you can probably loose the isset() completely and just code

<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>

Reply to comment

Like I said above, when processing that data from the $_POST/$_GET arrays a checkbox that is not checked will not exist in the $_POST/$_GET arrays so then you have to test for its existance using isset()

For example :

$staged = isset( $invoice_detail['staged']) ? 1 : 0;
Sign up to request clarification or add additional context in comments.

8 Comments

This works PERFECT when it comes to reading the checkbox from the sql database, but for some odd reason, BREAKS the saving :P
Is that any more help
So plain english(sorry, learning php), the above isset sql code means : if invoice detail(staged) is set, check to see if it is 0 or one and proceed?
Thats ok, If you mean this line isset( $invoice_detail['staged']) ? 1 : 0 it means if $invoice_detail['staged'] is set i.e. exists then I should store 1 into the database, else I shoudl store 0 into the database
Actually as you are generating the data value 0 or 1 you dont need the mysqli_real_escape_string() stuff at all. See amendment
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.