Error when submitting checkbox to MySQL

旧城冷巷雨未停 提交于 2020-01-04 05:21:13

问题


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

Warning: join() [function.join]: Invalid arguments passed in /srv/disk6/1662822/www/website.co.nf/connect-mysql.php on line 16

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

I understand its something to do with join function (obviously) but I don't understand what it is...

HTML code for checkboxes

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


回答1:


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




回答2:


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

Try this one. $ckb should be an array. For security purpose $val is converted in integer.



来源:https://stackoverflow.com/questions/23308928/error-when-submitting-checkbox-to-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!