使用php在数据库的单个列中插入多个值

时间:2018-06-07 09:07:02

标签: php

我想使用php在数据库中插入多个复选框值。但使用此代码时,它不会插入数据库中。请告诉我什么是错的和解决方案。现在当我检查生物和物理时,我想将它们添加到单列数据库中。

这是html部分

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

这是php部分

<?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>");
    }
}
?>

我尝试了implode()函数,但它无效。

1 个答案:

答案 0 :(得分:0)

您可以在数据库的不同列上 INSERT 值,或使用内爆并将值保存到一列

[HttpPost("PostDir")] [DisableRequestSizeLimit] public async Task<IActionResult> PostDir(string serverPath) { var zipName = Path.Combine(_config["QuickDrive:TempDir"], Guid.NewGuid() + ".zip"); using (var ms = new MemoryStream()) using (var fileStream = System.IO.File.Create(zipName)) { await Request.Body.CopyToAsync(ms); ms.Position = 0; await ms.CopyToAsync(fileStream); } return Ok(); } 应该是一个数组。

$_POST['subject']

此外,您可以使用一个查询插入多个项目,例如

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

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