简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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