2017-03-21 51 views
1

我有不同的複選框,如下面的代碼所述,並且此代碼在我的結尾處工作正常,因爲它連接了所有選定的值並以DB形式插入爲「PHP,.NET,JavaScript 「等等,但我想要的是如果我選擇了3個複選框,它應該添加三個單獨的條目1. PHP 2. JavaScript,3. Java。任何想法或概念將不勝感激。在SQL中插入多個複選框值作爲單獨的條目

<html> 
    <body> 
     <form action="" method="post" enctype="multipart/form-data"> 
     <div style="width:200px;border-radius:6px;margin:0px auto"> 
    <table border="1"> 
     <tr> 
      <td colspan="2">Select Technolgy:</td> 
     </tr> 
     <tr> 
      <td>PHP</td> 
      <td><input type="checkbox" name="techno[]" value="PHP"></td> 
     </tr> 
     <tr> 
      <td>.Net</td> 
      <td><input type="checkbox" name="techno[]" value=".Net"></td> 
     </tr> 
     <tr> 
      <td>Java</td> 
      <td><input type="checkbox" name="techno[]" value="Java"></td> 
     </tr> 
     <tr> 
      <td>Javascript</td> 
      <td><input type="checkbox" name="techno[]" value="javascript"></td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td> 
     </tr> 
    </table> 
    </div> 
    </form> 
    <?php 
    if(isset($_POST['sub'])) 
    { 
    $host="localhost";//host name 
    $username="root"; //database username 
    $word="";//database word 
    $db_name="dbtask";//database name 
    $tbl_name="new"; //table name 
    $con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string 
    $checkbox1=$_POST['techno']; 
    $chk=""; 
    foreach($checkbox1 as $chk1) 
     { 
      $chk .= $chk1.","; 
     } 
    $in_ch=mysqli_query($con,"insert into new(time) values ('$chk')"); 
    if($in_ch==1) 
     { 
      echo'<script>alert("Inserted Successfully")</script>'; 
     } 
    else 
     { 
      echo'<script>alert("Failed To Insert")</script>'; 
     } 
    } 
    ?> 
    </body> 
    </html> 

回答

0

你想保存爲一個逗號分隔的列表嗎?

implode(",", $_POST["techno[]"]); 
+0

此代碼使用逗號分隔值,但我想在sql中添加單個條目,如果我選擇3個複選框中的4個,它應該在sql中輸入3個單獨的行。 – A1Nasir

1
foreach($checkbox1 as $key => $value) 
     { 
    $in_ch=mysqli_query($con,"insert into new(time) values ('$value')"); 

} 

希望它能幫助!

1

請嘗試用這種

$checkbox1= $_POST["techno"]; 
foreach($checkbox1 as $key => $value){ 
    $in_ch=mysqli_query($con,"insert into new(time) values ('$value')"); 
} 
0

感謝您的迅速反應和想法。上述代碼工作正常,我得到了我想要的結果。

<html> 
    <body> 
     <form action="" method="post" enctype="multipart/form-data"> 
     <div style="width:200px;border-radius:6px;margin:0px auto"> 
    <table border="1"> 
     <tr> 
      <td colspan="2">Select Technolgy:</td> 
     </tr> 
     <tr> 
      <td>PHP</td> 
      <td><input type="checkbox" name="techno[]" value="PHP"></td> 
     </tr> 
     <tr> 
      <td>.Net</td> 
      <td><input type="checkbox" name="techno[]" value=".Net"></td> 
     </tr> 
     <tr> 
      <td>Java</td> 
      <td><input type="checkbox" name="techno[]" value="Java"></td> 
     </tr> 
     <tr> 
      <td>Javascript</td> 
      <td><input type="checkbox" name="techno[]" value="javascript"></td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td> 
     </tr> 
    </table> 
    </div> 
    </form> 
    <?php 
    if(isset($_POST['sub'])) 
    { 
    $host="localhost";//host name 
    $username="root"; //database username 
    $word="";//database word 
    $db_name="dbtask";//database name 
    $tbl_name="new"; //table name 
    $con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string 
    $checkbox1=$_POST['techno']; 
    $chk=""; 

    foreach($checkbox1 as $key => $value) 
      { 
     $in_ch=mysqli_query($con,"insert into new(time) values ('$value')"); 
    echo'<script>alert("Inserted")</script>'; 
    } 


    } 
    ?> 
    </body> 
    </html> 
相關問題