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

vaj7vani  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(436)

这是一个与复选框相关的经典问题,我试图找到答案,但找不到。请帮忙。
我尝试使用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 ";

这是我的表,我将从中检索数据。在本例中,它是“主课程”表。http://prntscr.com/jjgoqm
这是“users”表,在这里我使用implode()获取并保存复选框的checked值http://prntscr.com/jjhnu7
这是选中复选框并保存到“用户课程”列的“用户”表中后的结果。http://prntscr.com/jjhrbe 它在php-mysql上工作,但是当我把它改成mysqli时,它就不工作了,结果变成这样http://prntscr.com/jjhv82 它不见了。它对mysql有效,但对mysqli无效
拜托,怎么了?
谢谢你的帮助。

mzsu5hc0

mzsu5hc01#

好吧,多亏了尼克,我可以让代码再次工作。我将张贴那些谁想要了解更多的变化。给你。


**// 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 ";
gcmastyq

gcmastyq2#

我可以看到两个问题。第一个(最有可能导致代码无法工作的原因)是 $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_reporting(E_ALL); )您将看到导致代码无法运行的错误。

相关问题