简体   繁体   English

向MySQL提交复选框时出错

[英]Error when submitting checkbox to MySQL

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 我有以下用于将表单提交到数据库的php代码,唯一的问题是复选框...提交表单时显示

Warning: join() [function.join]: Invalid arguments passed in /srv/disk6/1662822/www/website.co.nf/connect-mysql.php on line 16 警告: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);
?>

The error is Warning: join() [function.join]: Invalid arguments passed in /srv/disk6/1662822/www/website.com/connect-mysql.php on line 16 错误是警告: join()[function.join]:在第16行的/srv/disk6/1662822/www/website.com/connect-mysql.php中传递了无效的参数

I understand its something to do with join function (obviously) but I don't understand what it is... 我了解它与join函数有关(显然),但我不知道它是什么...

HTML code for checkboxes 复选框的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 对于所有其他选择,仅更改每个复选框的值即可

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 ..? 另一则信息是,mysql数据库中的ckb字段类型为tinyint,默认值为0 ...而我猜测它不是我要查找的类型...。

Your checkboxes should be in the form of an array... 您的复选框应为数组形式...

<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>

Note : It is ckb[] instead of just ckb 注意:它是ckb[]而不是ckb

$ckb = array();
foreach($_POST['checkbox'] as $val){
    $ckb[] = (int) $val;
}
$ckb = implode(',', $ckb);

Try this one. 试试这个。 $ckb should be an array. $ ckb应该是一个数组。 For security purpose $val is converted in integer. 为了安全起见,$ val被转换为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM