1

In the folowing code I am doing an insert from a form into a mysql table

The form fields are populated from an MySQL database, which work correctly.

The problem is extracting data from the multiple checkboxes and inserting each value as a new row in the same table 'selection' columns userid and videoid.

The userid field is inserted correctly, but the videoid is not posting any data.

     <?php
    $con=mysqli_connect("$host", "$username",           "$password","$db_name")or die("cannot connect");//connection string  
    $user=$_POST['userid']; 
    $checkbox1=$_POST['videoid'];  
    $chk="";  

    foreach($checkbox1 as $chk1)  
       {  
          $chk .= $chk1.",";  
       }   
    $in_ch=mysqli_query($con,"INSERT INTO tbl_selection (userid, videoid) VALUES ('$user', '$chk');");  
    if($in_ch==1)  
       {  
          echo'<script>alert("Inserted Successfully")</script>';  
       }  
    else  
       {  
          echo'<script>alert("Failed To Insert")</script>';  
       }  
    }  
    ?>  
    </body>  
    </html> 

This is the html form which is populated from a mysql table:

 <?php
    connect to database
    ?>
    <div class="control-group">
    <?php
    $query = "SELECT * FROM video";
    $result2 = mysql_query($query);
    while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
    ?>
    <div class="controls"> 
        <label class="checkbox"> 
          <input type="checkbox" name="videoid" value="<?php echo $line[id]?>"><?php echo $line[title]?> 
        </label> 
      </div>
      <?php } ?> 
dkjoe
  • 31
  • 1
  • 6
  • 1
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 14 '16 at 16:45
  • You only need to connect to the database ONCE per script. That is quite a time consuming process – RiggsFolly May 14 '16 at 16:46
  • ...or mysqli_ if you must be old skool – Strawberry May 14 '16 at 16:48
  • Any idea as to why the foreach statement might not be working. – dkjoe May 14 '16 at 20:15
  • what do you get in $_POST['videoid']? what values? – carmel May 15 '16 at 05:50
  • I have tested with - echo ("

    your chosen video is $checkbox1

    "); - which returns a value, but the value is not being inserted correctly. The problem must be in the **foreach** statement.
    – dkjoe May 15 '16 at 06:08
  • Please print the output of $chk so that we can see the values. – Fallen May 15 '16 at 06:27
  • $chk returns a blank. – dkjoe May 15 '16 at 06:35
  • thats the problem :) please post the html that you have for the form – carmel May 15 '16 at 06:41
  • Just updated the post, to show the content of the html form. The checkboxes are populated from a mysql table, which is working ok as it displays all of the checkboxes on screen. – dkjoe May 15 '16 at 06:48

1 Answers1

0

1) Change

<input type="checkbox" name="videoid" value="<?php echo $line[id]?>"><?php echo $line['title']?> 

To

<input type="checkbox" name="videoid[]" value="<?php echo $line[id]?>"><?php echo $line['title']?> 

Means, for multiple checkbox. Use name as array type. Like videoid[].

2) Use for or foreach loop for inserting multiple checkbox value into a table. First, find out the checked checkbox through sizeof. Then, use accordingly.

Updated Code

<?php
$con=mysqli_connect("$host", "$username","$password","$db_name")or die("cannot connect");
$user=$_POST['userid']; 
$videoCheckBox=$_POST['videoid'];  

$checkedVideo = sizeof($videoCheckBox);

for($i=0;$i<$checkedVideo;$i++) {
    $videoId = $videoCheckBox[$i];
    $queryVideo = mysqli_query($con,"INSERT INTO `tbl_selection` (`userid`, `videoid`) VALUES ('$user', '$videoId');");  
}

if($checkedVideo == 0) {
    echo'<script>alert("Failed To Insert")</script>';  
} elseif($queryVideo) {
    echo'<script>alert("Inserted Successfully")</script>';
}

?>  
</body>  
</html> 


<div class="control-group">
    <?php
    $query = "SELECT * FROM video";
    $result2 = mysql_query($query);
    while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
    ?>
    <div class="controls"> 
        <label class="checkbox"> 
            <input type="checkbox" name="videoid[]" value="<?php echo $line['id']?>"><?php echo $line['title']?> 
        </label> 
    </div>
    <?php } ?> 

For more info, please click Inserting data into mySQL table from mutlidimensional input form

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77