Azizi
Azizi

Reputation: 151

How to store data into multiple MySQL tables according to the checkbox value?

I have an html form, with multiple checkboxes (subjects) When a user (student) selects the subjects ,the StudentID is stored in a MySQL table along with the selections made in separate columns but in the same table.

My question is: How can I store the student ID in a new table if the checkbox value "equals" to something, would strpos do it ?

for example:

if (strpos($cc,'252000') !== false) {
    mysqli_query($dbcon,"INSERT INTO newtable (studentid,ckb) 
VALUES ('$studentid','$cc')");
}

Full Code:

<?php
      $host = 'localhost';
      $port = 8889;
      $username="root" ;
      $password="root" ;
      $db_name="db1" ;
      $tbl_name="courses" ;
      $tbl_name="studentinfo";
      $tbl_name="newtable";

        $dbcon = mysqli_connect("$host","$username","$password","$db_name") ;
                mysqli_set_charset($dbcon, "utf8");


        if (!$dbcon) {
        die('error connecting to database'); }


    $studentid = mysqli_real_escape_string($dbcon, $_GET['studentid']); //echo $studentid;

$name = $_GET['ckb'];
if(isset($_GET['ckb']))
{
foreach ($name as $courses){
$cc=$cc. $courses.',';
}
}

if (strpos($cc,'252000') !== false) {

    mysqli_query($dbcon,"INSERT INTO newtable (studentid,ckb) 
VALUES ('$studentid','$cc')");

    echo "$cc, trtue";
}

HTML

<form action="cdb.php" method="get">

<input name="studentid" type="text" id="studentid" maxlength="11"value="Student ID" />

<input type="checkbox" name="ckb[]" value="251000-1"/>
<input type="checkbox" name="ckb[]" value="251000-2"/>

Upvotes: 1

Views: 479

Answers (3)

Azizi
Azizi

Reputation: 151

It turns out , that using this code does in fact sort the data according to the checkbox in new and different tables

if (strpos($cc,'251000') !== false) {
$sql3="INSERT INTO newtable (studentid, ckb)
    VALUES ('$studentid', '$cc')";
    echo 'true';
}

However It seems I must check for the sql3 statement

if (!mysqli_query($dbcon,$sql3)) 
       {
          die('Error: ' . mysqli_error($dbcon));
       }

Another mistake I had was using reserved words such as table in one of the sql statements. that fixed and the code above added solved the problem.

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94652

Ok if you absolutely must ignore all good database design practices try this.

Instead of creating a comma delimited list and putting it into the newtable use the serialize() function to place the contents of $_GET['ckb'] into this new row. At least this way you can use unserialize() to get back an array which makes manipulating the data easier even if it does not make searching the database any easier.

You could replace serialise/unserialize with json_encode() and json_decode()

references:

serialize: http://php.net/manual/en/function.serialize.php

unserialize: http://php.net/manual/en/function.unserialize.php

<?php
    $host = 'localhost';
    // I assume you moved apache to port 8889. 
    // so its irrelevant to mysql connection, 
    // good job you are not actually using this variable anywhere
    $port = 8889;
    $username="root" ;
    $password="root" ;
    $db_name="db1" ;

    // fix so you have 3 variables and are not overwriting the same one
    $tbl_name1="courses" ;
    $tbl_name2="studentinfo";
    $tbl_name3="newtable";

    // remove unnecessary double quotes
    $dbcon = mysqli_connect($host,$username,$password,$db_name) ;

    // add some error checking that reports the actual error
    if ( ! $dbcon ) {
        echo 'Connect Error (' . mysqli_connect_errno() . ') '
                               . mysqli_connect_error();
        exit;
    }

    mysqli_set_charset($dbcon, "utf8");

     

    if(isset($_GET['ckb'])) {
        $studentid = mysqli_real_escape_string($dbcon, $_GET['studentid']);
        $cc = serialize($_GET['ckb']);
        $result = mysqli_query($dbcon,"INSERT INTO newtable 
                                       (studentid,ckb) 
                                VALUES ('$studentid','$cc')");
        if ( ! $result ) {
            echo mysqli_error($dbcon);
            exit;
        }
    }
?>

Upvotes: 1

Nana Partykar
Nana Partykar

Reputation: 10548

Below, total size of 'ckb' checkbox is calculated. Then. due to for loop, it will run till the total size. 'studentid' coming from the textbox. It will insert into the table till for loop condition is true.

extract($_POST);

$CKBsize=sizeof($ckb);
for($i=0;$i<$CKBsize;$i++)
{
   $CourseName=$ckb[$i];
   mysql_query("INSERT INTO newtable SET studentid='$studentid', ckb='$CourseName'");
}

Upvotes: 0

Related Questions