0

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


            ?>

1 Answer 1

1

Try this

select sum(total) as TOTAL AMOUNT from table
where Checkbox='checked' 
Sign up to request clarification or add additional context in comments.

1 Comment

hi sir, thanks for the reply, i've attached a pic of the code, i wonder if it's possible doing it that way.

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.