【发布时间】: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 上无效
请问这是怎么回事?
感谢您的帮助。
【问题讨论】:
-
<label><h4>Course you are applying for:</h4></label>";在里面做什么? -
没什么,这是我的 CSS 风格的一部分。只是复选框的标题
-
抱歉,如果让您感到困惑,应该是 之后关闭它并开始新的 php 代码
标签: php mysql function mysqli checkbox