【问题标题】:How to Checked and Unchecked checkboxes from database using explode implode如何使用爆炸内爆从数据库中选中和未选中的复选框
【发布时间】:2018-05-20 00:04:22
【问题描述】:

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

我尝试使用 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 ";    

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

这是“用户”表,我使用 implode() http://prntscr.com/jjhnu7 获取并保存复选框选中值

这是选中复选框并保存到“user_course”列中的“users”表后的结果。 http://prntscr.com/jjhrbe 它适用于 php mysql,但是当我将其更改为 mysqli 时,它不起作用,它变成了这样http://prntscr.com/jjhv82 没了。它在 mysql 上有效,但在 mysqli 上无效

请问这是怎么回事?

感谢您的帮助。

【问题讨论】:

  • &lt;label&gt;&lt;h4&gt;Course you are applying for:&lt;/h4&gt;&lt;/label&gt;"; 在里面做什么?
  • 没什么,这是我的 CSS 风格的一部分。只是复选框的标题
  • 抱歉,如果让您感到困惑,应该是 之后关闭它并开始新的 php 代码

标签: php mysql function mysqli checkbox


【解决方案1】:

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

【讨论】:

  • 哇,尼克,你是怎么知道所有这些东西的?你刚刚拯救了我的一天,尼克。非常感谢。愿上帝永远与你同在。
【解决方案2】:

好的,感谢 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 "; 

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多