Can't insert multiple checkbox values to Mysql table

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1302 Views - Last Post: 08 February 2018 - 06:49 PM Rate Topic: -----

#16 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1928
  • View blog
  • Posts: 5,925
  • Joined: 15-January 14

Re: Can't insert multiple checkbox values to Mysql table

Posted 30 January 2018 - 10:27 AM

First, your code that checks if the form was submitted only gets values from the form. Your database code is not inside that if statement. So you always try to run the database query even if the form wasn't submitted, but you just run the query with no data. If the form was submitted you should do more than only getting the form values, you should also use the form values.

Secondly, what's going on here:

foreach($StudentGradeLevel as $StudentGradeLevel)
$StudentGradeLevel = implode(",",$StudentGradeLevel);



You don't have to pay money every time you create a new variable, there's no reason to use the same variable name for 4 different values there. I'm not even sure what that's trying to do since everything uses the same name. If you want to just implode the array you don't need a loop, for example, so I'm not sure what you intended to do there, only that it's incorrect.
Was This Post Helpful? 1
  • +
  • -

#17 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2388
  • View blog
  • Posts: 9,607
  • Joined: 03-December 12

Re: Can't insert multiple checkbox values to Mysql table

Posted 31 January 2018 - 01:53 PM

I see issues with your database design.

You want a loop, not to implode the array. The values should be singular and tied to a student, not a delimited list that is bulk loaded, unless this is NoSQL, but it isn't.
Was This Post Helpful? 1
  • +
  • -

#18 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 01 February 2018 - 02:49 AM

If i don't insert the values into database with "Implode", Any suggestion and the references coding and website for me?
For my knowledge, if the 'StudentGradeLevel" values looping to insert into table, at the same time others textfield variable also iterately insert values into table, but there only have one values at a time. So, how it going to work if i not create another inserting code only for "StudentGradeLevel". Or anybody have any references for me?
Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4154
  • View blog
  • Posts: 13,146
  • Joined: 08-June 10

Re: Can't insert multiple checkbox values to Mysql table

Posted 01 February 2018 - 02:55 AM

Quote

Or anybody have any references for me?


Your main issue here is database normalisation ( https://en.wikipedia...e_normalization ), make sure to also read the first four linked articles in the 'Normal forms' section. Without understanding why there is a problem with the data you will not really solve the problem.

This post has been edited by Dormilich: 01 February 2018 - 02:55 AM

Was This Post Helpful? 3
  • +
  • -

#20 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2388
  • View blog
  • Posts: 9,607
  • Joined: 03-December 12

Re: Can't insert multiple checkbox values to Mysql table

Posted 01 February 2018 - 07:14 AM

I think your data needs broken down even further, but it may be I simply don't understand your levels either.
Form 1-3, is that one thing, or 3? If you needed to search for form 1, you can't easily do so.


The "forms" should be in a table. And there is a joining table that links the form record to the student record.
Was This Post Helpful? 1
  • +
  • -

#21 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 01 February 2018 - 04:39 PM

Form 1 - 3 is one word, the user choose this as answer and the system insert into table, while others users want to see this details later, then the system retrieve these data 'form 1 - 3' or 'form 4 - 5 science class' or etc from the database and display.
Was This Post Helpful? 0
  • +
  • -

#22 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2388
  • View blog
  • Posts: 9,607
  • Joined: 03-December 12

Re: Can't insert multiple checkbox values to Mysql table

Posted 01 February 2018 - 06:35 PM

That is what it translates too, but not how it typically works behind the scenes.
Was This Post Helpful? 1
  • +
  • -

#23 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 01:34 AM

I follow with the NF, with create ID for this table, "TutorTable1" and insert checkbox values in "TutorTable2" with creating the same increament "ID" and "StudentGradeLevel", two attributes. The error message " HTTP ERROR 500" pop up and All values inserted into "TutorTable1", but no checkbox values insert into " TutorTable2". What is the problem?


<?php
$servername = "xxxxxx";
$dbusername = "xxxxxx";
$dbpassword = "xxxxxxx";
$dbname = "xxxxxxxxxxxxx";


try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,$dbpassword);
$conn ->setAttribute(
     // any occuring errors will be thrown as PDOException
	 PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION
	 );

  if (isset($_POST['submit'])) {
 
    $Name = $_POST['Name'];
    $Age = $_POST['Age'];
    $Contact = $_POST['Contact'];
    $Email = $_POST['Email'];
    $Address = $_POST['Address'];
    $JobStatus = $_POST['JobStatus'];
    $StudentGradeLevel = $_POST['StudentGradeLevel'];
  }


//insert data query
$setup = $conn->prepare("
       INSERT INTO
	     TutorRegisterForm(Name,Age,Contact,Email,Address,JobStatus)
	   VALUES
         (:Name, :Age, :Contact, :Email, :Address, :JobStatus)"	   
);
$setup->bindParam(':Name',$Name);
$setup->bindParam(':Age',$Age);
$setup->bindParam(':Contact',$Contact);
$setup->bindParam(':Email',$Email);
$setup->bindParam(':Address',$Address);
$setup->bindParam(':JobStatus',$JobStatus);

$setup->execute();


//Write Checkbox values to DB
foreach($StudentGradeLevel as $StudentGradeLevel_1) {
    $setup_2 = $conn->prepare("
                  INSERT INTO 
                        TutorStudentGrade(StudentGradeLevel)
                        VALUES
                        (:StudentGradeLevel_1)");
    $setup_2->binParam(':StudentGradeLevel_1',$StudentGradeLevel_2);
    $setup_2->execute();
                        
}



echo "New records created successfully!";
}
catch(PDOException $e)
{
echo "Error:" .$e->getMessage();
}

$conn = null;
?>




Was This Post Helpful? 0
  • +
  • -

#24 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6500
  • View blog
  • Posts: 26,286
  • Joined: 12-December 12

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 01:50 AM

You haven't defined $StudentGradeLevel_2.

You don't have to mirror the original name StudentGradeLevel, just appending _x is going to lead to confusing code, and they types of error that I have just highlighted. Use a little more imagination; you could simplify to $grade, for example. Modern code is written in discrete blocks, so we can localise variable names without risking conflict with any global names.
Was This Post Helpful? 1
  • +
  • -

#25 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 05:10 AM

I make the correction with changing the variable name to $Grade, the error still the same. Anybody can tell me, what is the problem? Thanks
www.xxx.com is currently unable to handle this request.
" HTTP ERROR 500"

<?php
$servername = "xxxxxx";
$dbusername = "xxxxxx";
$dbpassword = "xxxxxxxxxx";
$dbname = "xxxxxxxxxx";


try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,$dbpassword);
$conn ->setAttribute(
     // any occuring errors will be thrown as PDOException
	 PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION
	 );

  if (isset($_POST['submit'])) {
 
    $Name = $_POST['Name'];
    $Age = $_POST['Age'];
    $Contact = $_POST['Contact'];
    $Email = $_POST['Email'];
    $Address = $_POST['Address'];
    $JobStatus = $_POST['JobStatus'];
    $StudentGradeLevel = $_POST['StudentGradeLevel'];
  }


//insert data query
$setup = $conn->prepare("
       INSERT INTO
	     TutorTable1(Name,Age,Contact,Email,Address,JobStatus)
	   VALUES
         (:Name, :Age, :Contact, :Email, :Address, :JobStatus)"	   
);
$setup->bindParam(':Name',$Name);
$setup->bindParam(':Age',$Age);
$setup->bindParam(':Contact',$Contact);
$setup->bindParam(':Email',$Email);
$setup->bindParam(':Address',$Address);
$setup->bindParam(':JobStatus',$JobStatus);

$setup->execute();


//Write Checkbox values to DB

foreach($StudentGradeLevel as $Grade) {
    $setup_2 = $conn->prepare("
                  INSERT INTO 
                        TutorTable2(StudentGradeLevel)
                        VALUES
                        (:Grade)");
    $setup_2->binParam(':StudentGradeLevel_1',$Grade);
    $setup_2->execute();
                        
}



echo "New records created successfully!";
}
catch(PDOException $e)
{
echo "Error:" .$e->getMessage();
}

$conn = null;
?>



Was This Post Helpful? 0
  • +
  • -

#26 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2388
  • View blog
  • Posts: 9,607
  • Joined: 03-December 12

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 11:45 AM

Line 47
Was This Post Helpful? 0
  • +
  • -

#27 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 05:40 PM

<?php
$servername = "xxxxxxxxxxxx";
$dbusername = "xxxxxxxxxxxxxxxxxx";
$dbpassword = "xxxxxxxxxxxxxxxxxxxxx";
$dbname = "xxxxxxxxxxxxxxxxxxx";


try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,$dbpassword);
$conn ->setAttribute(
     // any occuring errors will be thrown as PDOException
	 PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION
	 );

  if (isset($_POST['submit'])) {
 
    $Name = $_POST['Name'];
    $Age = $_POST['Age'];
    $Contact = $_POST['Contact'];
    $Email = $_POST['Email'];
    $Address = $_POST['Address'];
    $JobStatus = $_POST['JobStatus'];
    $StudentGradeLevel = $_POST['StudentGradeLevel'];
  

//insert data query
$setup = $conn->prepare("
       INSERT INTO
	     TutorRegisterForm(Name,Age,Contact,Email,Address,JobStatus)
	   VALUES
         (:Name, :Age, :Contact, :Email, :Address, :JobStatus)"	   
);
$setup->bindParam(':Name',$Name);
$setup->bindParam(':Age',$Age);
$setup->bindParam(':Contact',$Contact);
$setup->bindParam(':Email',$Email);
$setup->bindParam(':Address',$Address);
$setup->bindParam(':JobStatus',$JobStatus);
$setup->execute();


//Write Checkbox values to DB

foreach($StudentGradeLevel as $Grade) {
    $setup_2 = $conn->prepare("
                  INSERT INTO 
                        TutorStudentGrade(StudentGradeLevel)
                        VALUES
                        (:Grade)");
    $setup_2->binParam(':Grade',$Grade);
    $setup_2->execute();
                        
} }

echo "New records created successfully!";
}
catch(PDOException $e)
{
echo "Error:" .$e->getMessage();
}

$conn = null;
?>




Was This Post Helpful? 0
  • +
  • -

#28 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 05:55 PM

<?php
$servername = "xxxxxxxxxxxxxx";
$dbusername = "xxxxxxxxxxxxxxx";
$dbpassword = "xxxxxxxxxxxxxxx";
$dbname = "xxxxxxxxxxxxxxxxxxxxxxx";


try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,$dbpassword);
$conn ->setAttribute(
     // any occuring errors will be thrown as PDOException
	 PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION
	 );

  if (isset($_POST['submit'])) {
 
    $Name = $_POST['Name'];
    $Age = $_POST['Age'];
    $Contact = $_POST['Contact'];
    $Email = $_POST['Email'];
    $Address = $_POST['Address'];
    $JobStatus = $_POST['JobStatus'];
    $StudentGradeLevel = $_POST['StudentGradeLevel'];
  
//insert data query
$setup = $conn->prepare("
       INSERT INTO
	     TutorRegisterForm(Name,Age,Contact,Email,Address,JobStatus)
	   VALUES
         (:Name, :Age, :Contact, :Email, :Address, :JobStatus)"	   
);
$setup->bindParam(':Name',$Name);
$setup->bindParam(':Age',$Age);
$setup->bindParam(':Contact',$Contact);
$setup->bindParam(':Email',$Email);
$setup->bindParam(':Address',$Address);
$setup->bindParam(':JobStatus',$JobStatus);
$setup->execute();


//Write Checkbox values to DB

foreach($StudentGradeLevel as $Grade) {
    $setup_2 = $conn->prepare("
                  INSERT INTO 
                        TutorStudentGrade(StudentGradeLevel)
                        VALUES
                        (:Grade)");
    $setup_2->binParam(':Grade',$Grade);
    $setup_2->execute();
                        
} }

echo "New records created successfully!";
}
catch(PDOException $e)
{
echo "Error:" .$e->getMessage();
}
}
$conn = null;
?>


This post has been edited by tedchong: 03 February 2018 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#29 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 06:15 PM

I added closing curly brace at line 52 to match the open curly brace at line 15, and i added closing curly brace at line 60 to match open curly brace at line 80. I changed the value':StudentGradeLevel_1' to ':Grade'. The error message still popping up.
www.xxx.com is currently unable to handle this request.
" HTTP ERROR 500"

The have no any values inset into these 2 tables.
Was This Post Helpful? 0
  • +
  • -

#30 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2388
  • View blog
  • Posts: 9,607
  • Joined: 03-December 12

Re: Can't insert multiple checkbox values to Mysql table

Posted 03 February 2018 - 06:26 PM

So, naming may be off. Use print_r($_POST) to see the values that are getting submitted by the form.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3