2

I want to insert multiple checkbox values in database using php. but using this code it is not inserting in database. please tell me whats wrong and solution too. for ex now when i check bio and physics then i want to add them in database in single column.

This is html part

    <select value="class" class="wp-form-control">
        <option value="Select your class" >Select your class</option>
        <option value="10th" onclick="myFunction()">10th</option>
        <option value="12th" onclick="myFunction1()">12th</option>
    </select>

    <div id="mydiv" style="display:none">   
       <input type="checkbox" name="subject[]" value="1">bio<br>
       <input type="checkbox" name="subject[]" value="2">phy<br>
       <input type="submit" value="Calculate" name="submit" class="wpcf7-submit">
     </div>

    <div id="mydiv1" style="display:none">
        <input type="checkbox" name="subject[]" value="3">maths<br>
        <input type="checkbox" name="subject[]" value="4">sciecne<br>
         <input type="submit" value="Calculate" name="submit" class="wpcf7-submit">

    </div>

This is php part

    <?php 
    $host = 'localhost:3306';  
    $user = 'root';  
    $pass = '';  
    $dbname = 'wpf';  

    $conn = mysqli_connect($host, $user, $pass,$dbname);  
    if(!$conn){  
      die('Could not connect: '.mysqli_connect_error());  
    }  

    if(isset($_POST['submit'])) {

        if(isset($_POST['subject'])) {
            $classes=$_POST['subject'];

            $c=count($classes);
            $price=0;
            for($i=0;$i<$c;$i++) {
                if($classes[$i]==1) {
                    $price+=200;
                }
                if($classes[$i]==2){
                    $price+=300;
                }

                if($classes[$i]==3){
                    $price+=400;
                }
                if($classes[$i]==4) {
                    $price+=500;
                }
            }
            $chk="";
            foreach($classes as $sub) {
                $sub.= $sub.",";
            } 

            if($classes) {

                $insert="INSERT INTO feedetails 
                                (subjects,price) 
                         VALUES ('$sub',$price')";  

                if(mysqli_query($conn,$insert)) {
                    echo ("<SCRIPT LANGUAGE='Javascript'>
                        window.alert('Your fee has been updated.Please proceed to pay.');
                        window.location.href='payment.php';
                    </SCRIPT>");
                }
            }   
        }else{
            echo ("<SCRIPT LANGUAGE='Javascript'>
            window.alert('Please select atleast one subject to proceed');
            </SCRIPT>");
        }
    }
    ?>

I tried implode() function but it isn't working.

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
Ashwini Nemade
  • 153
  • 1
  • 13
  • A ` – RiggsFolly Jun 07 '18 at 09:14
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Jun 07 '18 at 09:16
  • okay I changed it. same error still! i want to insert checkbox values in database – Ashwini Nemade Jun 07 '18 at 09:16
  • 1
    Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Jun 07 '18 at 09:17
  • `VALUES ('$sub',$price')";` Missing a single quote before `$price` – RiggsFolly Jun 07 '18 at 09:17
  • The `language` attribute on a ` – RiggsFolly Jun 07 '18 at 09:21
  • Storing data in a column as a comma seperated list is a **horrible** way to persist this information. Later when you want to process this information you will have many more problems. Checkout a basic database design tutorial and find out how to do this sensibly – RiggsFolly Jun 07 '18 at 09:24

1 Answers1

0

you can INSERT values at different columns at database, or use implode and save values into one column

$_POST['subject'] should be an array.

$subject = implode(',', (array)$_POST['subject']);

$query="INSERT INTO feedetails (subjects,price) VALUES ('".$subject."','".$price."')";

Also you can insert multiple items with one query like this

    $query = "INSERT INTO feedetails (subjects,price) VALUES ";
    for ($z=0; $z < count($subject); $z++) {
        $query .= "('" . $subject[$z] . "','".$price."'),";
    }
munsifali
  • 1,732
  • 2
  • 24
  • 43