Patti
Patti

Reputation: 1

php sql Get query when checkbox is checked

I just wanted to know how to get the value of a column in SQL, when the checkbox is checked, then it would add up the values since they are numbers.

data

Table name = adding

    SEARCH = Dan

    -----------------------
    name   |   date    | Total | Checkbox
    -------------------------------------
    Dan    | dd/mm/yyy | 200   |  checked
    Dan    | dd/mm/yyy | 400   |  checked
    Dan    | dd/mm/yyy | 300   |  checked
    Dan    | dd/mm/yyy | 250   |  unchecked
    Dan    | dd/mm/yyy | 260   |  unchecked

TOTAL AMOUNT = 900

UPDATE: Here's the code i've done so far, and it's still not working.

 <?php  

            include "connection.php";

                //$searchName= $_POST['submit'];


                if(isset($_POST['submit'])){
                    $searchName = $_POST['submit'];

                    $searchName = preg_replace("#[^0-9a-z]#i","",$searchName);

                    $query = "SELECT * FROM adding where Code LIKE '$searchName%' ORDER BY Date";
                    $result = mysql_query($query);

                    if(mysql_num_rows($result)>0){
                        echo "<form action=Payment.php method=get>";                            
                        echo "<table border='1'>
                        <tr>
                        <th>Name</th>
                        <th>Date</th>   
                        <th>Total</th>
                        <th>Checkbox</th>
                        </tr>";
                        while($row = mysql_fetch_array($result)){
                            //$idno = $row['id'];

                            echo "<tr>";
                            echo "<td>" . $row['Code'] . " </td>";
                            echo "<td>" . $row['Date'] . "</td>";
                            echo "<td>" . $row['Total'] . "</td>";
                            echo "<td><input type = \"checkbox\" name=\"pay[]\" value='{$row['id']}' ></td>";
                            echo "</form>"; 
                        }
                        echo "<table>";
                        echo "<input type=\"submit\" name =\"button2\" value=\"Submit\">";
                        echo "</form>";

                        if(isset($_GET['pay'])){

                            $checked=$_GET("pay");
                            $c= count($checked);
                            $totalpayment = $row['Total'];

                            for($i=0;$i<$c;$i++){
                                if($checked[$i]==$row['id']){
                                    $totalpayment = $totalpayment+$row['Total']; 
                                }
                            }
                            echo "total is: " .$totalpayment. "<br>";

                        } 


                    }
                    else{
                        echo "RECORD NOT FOUND!";
                    }




                }

                mysql_free_result($result);


            ?>

Upvotes: 0

Views: 525

Answers (1)

Madhivanan
Madhivanan

Reputation: 13700

Try this

select sum(total) as TOTAL AMOUNT from table
where Checkbox='checked' 

Upvotes: 1

Related Questions