0

Here is my html code

    <form action="view.php" method="post" id="form">
    <p>checkbox: <input type="checkbox" name="check[]" value="INCOMETAX" />INCOMETAX</p>

    <p>checkbox: <input type="checkbox" name="check[]" value="GST" />GST</p>

    <p>checkbox: <input type="checkbox" name="check[]" value="VAT" />VAT</p>

My php code is here

if ( isset($_POST['check']) ) {
        $checkbox1=$_POST['check'];  

        $chk = "";  
        foreach($checkbox1 as $chk1)  
        {  
         $chk .= $chk1 .",";

                   $sql = "INSERT INTO value (service) VALUES ('$chk')";

        }

The code is work proper but I want to store value in new row with same id ,rightnow the multiple values from checkbox is stored like INCOMETAX,GST,VAT

how I store INCOMEtax in first row,GST in second row and VAT in third row with same id 1

1
  • Is the id column of your value table not a primary key and thus unique? Commented Jul 7, 2018 at 7:58

2 Answers 2

0

You can create a foreach loop in php that will include Insert statement for every checkbox that is selected.

This question was already asked actually. Here is a link Inserting checkbox values into database

Also I encourage you to use parameterized queries.

Sign up to request clarification or add additional context in comments.

Comments

0
if ( isset($_POST['check']) ) { 

    $checkbox1=$_POST['check'];

    $sql = "";  
    foreach($checkbox1 as $chk1) {      
        mysql_query("INSERT INTO value (service) VALUES ('".$chk1."')");
    }
}

this will insert to database everytime instead of generating query

2 Comments

ERROR :--------INSERT INTO value (service) VALUES ('INCOMETAX');INSERT INTO value (service) VALUES ('GST');Error: INSERT INTO value (service) VALUES ('INCOMETAX');INSERT INTO value (service) VALUES ('GST'); You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO value (service) VALUES ('GST')' at line 1
@PayalPawar just now i created a table according to your field in the name of value, and I copied your error message and removed those unwanted letters ERROR :-------- and executed in mysql.. Everything works for me . Try after replacing this line mysql_query("INSERT INTO value (service) VALUES ('".$chk1."')"); That error might have occured because of your table name, because value is a function in mysql . so when you are using as table or field you should use ` symbol. Let me know it works or not.

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.