1

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"/>
6
  • I think you are forgettng that with a checkbox, it only gets sent to the script, if it is checked. So in your $_GET['ckb'] you will only get the values of those checkboxes that are checked to process Commented Aug 28, 2015 at 13:25
  • As an aside: Storing data as a comma seperated list is a bad idea. It means that every time you want to look at this data, you have to write code to unpack it again and doing that leads to mistakes. And more importantly it makes the data much more difficult to process i.e. how would you write a query that would return you all the students that are doing course 251000-1? Commented Aug 28, 2015 at 13:27
  • @RiggsFolly, that is what I'm trying to achieve and the idea that crossed my mind is to separate the student IDs according to their checkbox selections, say if the checkbox contains 251000, insert the student ID into the CourseA table, and so on as for 251000-1 and 251000-2, I was thinking if I sort the Data ASCending or DESCending I can browse through more comfortably but all I want now is to store the student IDs in different tables according to what courses they selected, I hope this was clear. Commented Aug 28, 2015 at 13:32
  • Maybe change your password. Commented Aug 28, 2015 at 13:32
  • @Strawberry, how would that solve anything? Commented Aug 28, 2015 at 13:34

3 Answers 3

1

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;
        }
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This was certainly helpful but it turn out I had this piece of code missing and it solved the whole problem using strpos , please check my own answer , Thank you. @RiggsFolly
0

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'");
}

4 Comments

It would have done the same in a foreach loop as well. Please read The foreach manual page
And dont use extract() except for specific situations which all would be coded in a function scope and not the global scope.
Hey @RiggsFolly. Today you are reviewing each and every answer of mine. Ha Ha . Its nice. I'm still in the learning phase. Thank you for giving advice evrytime.
I am not specifically picking on you, it just happens this way somedays
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.