向MySQL提交复选框时出错 [英] Error when submitting checkbox to MySQL
问题描述
我有以下php代码提交表单到数据库唯一的问题是与复选框...提交表单上显示
I have the following php code for submitting the form to the database the only problem is with the checkboxes ... on submitting the form this shows up
警告:join()[function.join]:在第16行的/srv/disk6/1662822/www/website.co.nf/connect-mysql.php中传递的参数无效:
<?php
$host="host.com" ;
$username="1662822_db1" ;
$password="awesomepassword" ;
$db_name="1662822_db1" ;
$tbl_name="courses" ;
$dbcon = mysqli_connect("$host","$username","$password","$db_name") ;
if (!$dbcon) {
die('error connecting to database'); }
echo 'Courses successfully registerd , ' ;
// escape variables for security
$studentid = mysqli_real_escape_string($dbcon, $_POST['studentid']); echo $studentid;
**$ckb = join (', ', $_POST['ckb']);**
**$sql="INSERT INTO courses (studentid, ckb)
VALUES ('$studentid', '$ckb')";**
if (!mysqli_query($dbcon,$sql)) {
die('Error: ' . mysqli_error($dbcon));
}
echo " Thank you for using IME Virtual Registeration ";
mysqli_close($dbcon);
?>
错误是
警告: join .join]:在第16行的/srv/disk6/1662822/www/website.com/connect-mysql.php中传递的参数无效
The error is Warning: join() [function.join]: Invalid arguments passed in /srv/disk6/1662822/www/website.com/connect-mysql.php on line 16
我理解它的事情(显然),但我不明白是什么...
I understand its something to do with join function (obviously) but I don't understand what it is...
复选框的HTML代码
<input type="checkbox" name="ckb" value="strenthofmaterials";>
<label for="StrengthofMaterials"> Strength Of Materials </label>
<input type="checkbox" name="ckb" value="dynamics";>
<label for="StrengthofMaterials"> dynamics </label>
对所有其他选项进行更改,只更改每个复选框的值
it goes on for all other choices with only changing the value of each checkbox
另一条信息,mysql数据库中的ckb字段类型在tinyint中,默认值为0 ...我猜它的不是我正在寻找的类型..? / p>
another piece of information , the ckb field in mysql database type in tinyint with a default value of 0 ...and I'm guessing its not the type I'm looking for ..?
推荐答案
$ckb = array();
foreach($_POST['checkbox'] as $val){
$ckb[] = (int) $val;
}
$ckb = implode(',', $ckb);
尝试这个。 $ ckb应该是一个数组。
为了安全起见,$ val被转换为整数。
Try this one. $ckb should be an array. For security purpose $val is converted in integer.
这篇关于向MySQL提交复选框时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!