简体   繁体   中英

Insert multiple values in single column of database using php

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.

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."'),";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM