如何使用explode implode从数据库中选中和取消选中复选框

时间:2018-05-18 11:53:10

标签: php mysql function mysqli checkbox

这是一个与复选框相关的经典问题,我试图找到答案,但无法解决。请帮忙。

我尝试使用php mysqli发布和编辑数据库的复选框。这就是我所做的。

首先,我创建了连接并将其保存为 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();
  }

接下来,我创建了 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;
}


**// I called the user's checked checkboxes from 'users' table on 'user_course' column **

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


**// and displayed them associated from 'main_course' table. 
//The checkboxes lists are taked from 'main_course' table 
//and the checked value are taken from 'users' table on 'user_course' column**

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

    echo " $d ";    

这是我的表格,我将从中检索我的数据课程。在这种情况下,它是&#34; main_course&#34;表。 http://prntscr.com/jjgoqm

这就是&#34;用户&#34;我使用implode(http://prntscr.com/jjhnu7

获取并保存复选框选中值的表格

这是选中复选框并保存到&#34;用户&#34;之后的结果。 &#34; user_course&#34;中的表格柱。 http://prntscr.com/jjhrbe 它适用于php mysql,但是当我把它改成mysqli时,它没有工作,它变成了这样http://prntscr.com/jjhv82 它已经消失了。它适用于mysql,但没有在mysqli

上工作

请问发生了什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我可以看到两个问题。第一个(最有可能导致代码不起作用的)是$con超出了GetCheckboxes函数的范围。您需要将其添加为参数或在该函数的开头添加global $con;语句,即

function GetCheckboxes($table, $key, $Label, $Nilai='') {
  global $con;
  $s = "select * from $table order by id_course";
  $r = mysqli_query($con, $s);
  ...

第二个(不太重要,因为它只会导致NOTICE级错误),在您对GetCheckboxes的调用中,$r[user_course]应为$r['user_course']

如果您启用PHP error reportingerror_reporting(E_ALL);),您会看到导致代码无法正常运行的错误。

答案 1 :(得分:0)

好的,感谢Nick,我可以让代码再次运行。我将为那些想要了解更多信息的人发布更改。在这里。

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

function GetCheckboxes($table, $key, $Label, $Nilai='') {
global $con;
$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;
}


**// I called the user's checked checkboxes from 'users' table on 'user_course' column **

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


**// and displayed them associated from 'main_course' table. 
//The checkboxes lists are taked from 'main_course' table 
//and the checked value are taken from 'users' table on 'user_course' column**

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

    echo " $d ";