vendredi 18 mai 2018

How to Checked and Unchecked checkboxes from database using explode implode on php mysqli

This is a classic problem related to checkboxes, I've tried to find the answer but couldn't. Please help.

I tried to post and edit checkboxes to and from database using php mysqli. Here is what I did.

First, I created connection and save it as koneksi.php

$con = mysqli_connect("localhost","username","password","database_table");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

Next I created profile.php

**// I created a function to call different tables**

function GetCheckboxes($table, $key, $Label, $Nilai='') {
$s = "select * from $table order by id_course";
$r = mysqli_query($con, $s);
 $_arrNilai = explode(',', $Nilai);
  $str = '';
  while ($w = mysqli_fetch_array($r)) {
    $_ck = (array_search($w[$key], $_arrNilai) === false)? '' : 'checked';
    $str .= "<div>
                <input type='checkbox' name='".$key."[]' value='$w[$key]' 
$_ck>
                $w[$Label]

 </div> ";
  }
  return $str;
}

$sql  = mysqli_query($con,"SELECT * FROM users WHERE user_email='$_SESSION[emailuser]'");
    $r    = mysqli_fetch_array($sql,MYSQLI_BOTH); 

<label><h4>Course you are applying for:</h4></label>";
$d = GetCheckboxes('main_course', 'course_seo', 'course_name', $r[user_course]);

    echo " $d   

This is my table that I will retrieve my data course from. In this case, it's the "main_course" table. http://prntscr.com/jjgoqm

And this is the "users" table where I take and save the checkboxes checked value using implode() http://prntscr.com/jjhnu7

And This is the result after checkboxes are selected and saved into the "users" table in the "user_course" column. http://prntscr.com/jjhrbe It works on php mysql, but when I changed it into mysqli , it doesn't work and it turned like this http://prntscr.com/jjhv82 It's gone. It worked on mysql but didn't work on mysqli

Please what's going on?

Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire