jeudi 5 août 2021

how to store checkbox values i select to mysql db

I have 4 attributs: cp1,cp2,cp3,cp4, and I want to store values of these attributes to an SQL database using PHP my SQL , for example cp1=1 ,cp2=2 ,cp3=40 , cp4 = 2 , i'd like to store 1,2,40,2 to my database. this program stores these values, but my problem is that these values aren't stored at the specified attribute, here is my program:

 <?php 

  
                while($column = mysqli_fetch_assoc($search_result))
                   
       {        
                echo '<tr><td>';

               // echo '</td>';
               // foreach ($column as $key => $value)
                //    echo '<td>'.htmlspecialchars($value).'</td>';
                // echo '</tr>';

                // testtttttttt

               

                foreach ($column as $key => $value){
                   echo '<tr><td>'.$key.'</td>';

                   echo '<tr>';
                        echo '<td><input type="checkbox" name="selected[]" value="'.$value.'"/></td>';
                    
                        echo '<td>'.htmlspecialchars($value).'</td>';
                  echo '</tr>';
                  
        
                  }


            }
             ?>
    </table>
    <input type="submit" name="sub" value="envoyer"/>

    </form>
 

    <?php  



if(isset($_POST['sub']))  
{ 
$PN = $_POST['PN']; 
$cp1=$_POST['cp1'];
$cp2=$_POST['cp2'];
$cp3=$_POST['cp3'];
$cp4=$_POST['cp4'];


$host="localhost";//host name  
$username="root"; //database username  
$word="";//database word  
$db_name="mysql";//database name  
$tbl_name="adapter0"; //table name  
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string  

$checkbox1=$_POST['selected'];   




$in_ch=mysqli_query($con,"insert into adapter1(PN, cp1 , cp2, cp3, cp4  ) values ('".$checkbox1[0]."','". $checkbox1[1]."','". $checkbox1[2] ."','".$checkbox1[3]."','". $checkbox1[4] ."')");  
if($in_ch==1)  
   {  
      echo'<script>alert("Inserted Successfully")</script>';  
   }  
else  
   {  
      echo'<script>alert("Failed To Insert")</script>';  
   }  
}  
?> 



Aucun commentaire:

Enregistrer un commentaire