Can't insert multiple checkbox values to Mysql table

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

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

#1 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

Can't insert multiple checkbox values to Mysql table

Posted 23 January 2018 - 02:28 AM

I have spent few days also can't solve this issue, that insert the multiple checkbox values to MySQL Table. After i clicking "submit", the result page show the values of every column without any message pop up, but the values are not inserted into the table. But, without the //Inset multiple checkbox values to MySQL Table, the textfield inputed values can insert into the MySQL Table with the"Your message successfully sent" pop up . Thanks for your knowledge....

HTML
<div class="col-md-6">
<div class="form-group">
<label class="heading"> Student Grade Covered</label><br>
<input type="checkbox" name="StudentGradeLevel[]" value="Kidnergarden"/>&nbsp;Kidnergarden<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Primary School"/>&nbsp;Primary School<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 1-3"/>&nbsp;Form 1-3<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 4-5 Science Class"/>&nbsp;Form 4-5 Science Class<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 4-5 Art Class"/>&nbsp;Form 4-5 Art Class<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 6"/>&nbsp;Form 6<br>
</div>
</div>





PHP
<?php
$servername = "xxxxxx";
$dbusername = "xxxxxxxx";
$dbpassword = "xxxxxxxx";
$dbname = "xxxxxxxxxxx";

$Name = $_POST['Name'];
$Age = $_POST['Age'];
$Contact = $_POST['Contact'];
$Email = $_POST['Email'];
$Address = $_POST['Address'];
$JobStatus = $_POST['JobStatus'];
$StudentGradeLevel = $_POST['StudentGradeLevel'];

// Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);

// Check connection
if($conn->connect_error) {
    die("Connection failed:" . $conn->connect_error);
}

print_r($_POST);

if(empty($Name)) {
echo "Name can not be blank, Please press back and correct the issue!";
die();
}
if(empty($Age)) {
echo "Name can not be blank, Please press back and correct the issue!";
die();
}
if(empty($Contact)) {
echo "Name can not be blank, Please press back and correct the issue!";
die();
}
if(empty($Email)) {
echo "Email can not be blank, Please press back and correct the issue!";
die();
}
if(empty($Address)) {
echo "Address can not be blank. Please press back and correct the issue!";
die();
}
if(empty($JobStatus)) {
echo "Job Status can not be blank. Please press back and correct the issue!";
die();
}
if(empty($StudentGradeLevel)) {
echo "Student Grade Level can not be blank. Please press back and correct the issue!";
die();
}

//Inset multiple checkbox values to MySQL Table
$query = "INSERT INTO TableName(StudentGradeLevel) VALUES ";
for ($i=0; $i<count($StudentGradeLevel); $i++)
    $query .= "('" . $StudentGradeLevel[$i] . "'),";
$query = rtrim($query,',');
mysqli_query($query) or die (mysqli_error() );

//Insert textfield inputed values to MySQL Table
$sql = "INSERT INTO TableName(Name,Age,Contact,Email,Address,JobStatus)
Values ('$Name','$Age','$Contact','$Email','$Address','$JobStatus')";

if ($conn->query($sql) === TRUE) {
  echo '<script>alert("Your message successfully sent"); location.replace(document.referrer);</script>'; 
} else {
  echo "Error: ". $sql ."<br>" . $conn->error;                                  
}


$conn->close();
?>





Is This A Good Question/Topic? 0
  • +

Replies To: Can't insert multiple checkbox values to Mysql table

#2 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 23 January 2018 - 02:34 AM

You should enable error reporting/display so you can see the error PHP throws on line 59.

Note: you are wide open to SQL injection.
Was This Post Helpful? 0
  • +
  • -

#3 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

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

Posted 23 January 2018 - 02:43 AM

How?
Was This Post Helpful? 0
  • +
  • -

#4 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 23 January 2018 - 02:46 AM

see http://php.net/error-reporting & http://php.net/manua....display-errors
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2389
  • View blog
  • Posts: 9,628
  • Joined: 03-December 12

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

Posted 23 January 2018 - 06:53 AM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6504
  • View blog
  • Posts: 26,300
  • Joined: 12-December 12

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

Posted 23 January 2018 - 07:12 AM

You should also not alternate between the procedural and object-oriented versions of the mysqli library.
Was This Post Helpful? 0
  • +
  • -

#7 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

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

Posted 30 January 2018 - 12:49 AM

All the data can insert into database, but the checkbox values of StudentGradeLevel still can not insert into database.

HTML
<div class="col-md-6">
<div class="form-group">
<label class="heading"> Student Grade Covered</label><br>
<input type="checkbox" name="StudentGradeLevel[]" value="Kidnergarden"/>&nbsp;Kidnergarden<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Primary School"/>&nbsp;Primary School<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 1-3"/>&nbsp;Form 1-3<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 4-5 Science Class"/>&nbsp;Form 4-5 Science Class<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 4-5 Art Class"/>&nbsp;Form 4-5 Art Class<br>
<input type="checkbox" name="StudentGradeLevel[]" value="Form 6"/>&nbsp;Form 6<br>
</div>
</div>
								
                                      
<div class="center-content">
<input type="submit" name="submit" value="submit" class="btn btn-lg">
</div>




PHP
<?php
$servername = "";
$dbusername = "";
$dbpassword = "";
$dbname = "";


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,StudentGradeLevel)
	   VALUES
         (:Name, :Age, :Contact, :Email, :Address, :JobStatus, :StudentGradeLevel)"	   
);
$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->bindParam(':StudentGradeLevel',$StudentGradeLevel);

//Write Checkbox values to DB
foreach($StudentGradeLevel as $StudentGradeLevel)
$StudentGradeLevel = implode(",",$StudentGradeLevel);
$setup->execute();

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



Was This Post Helpful? 0
  • +
  • -

#8 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

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

Posted 30 January 2018 - 12:56 AM

1)If i put { at line 43 on PHP, "Syntax Error, Unexpected T_CATCH" message pop up at line 90.
2) The data values that inserted in StudentGradeLevel column in my table is the word "Array" without showing the checked values that selected in the checkbox.
Was This Post Helpful? 0
  • +
  • -

#9 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 30 January 2018 - 01:22 AM

Quote

2) The data values that inserted in StudentGradeLevel column in my table is the word "Array" without showing the checked values that selected in the checkbox.

That happens when you stringify an array. The PHP notice would have notified you about that issue.
Was This Post Helpful? 1
  • +
  • -

#10 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

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

Posted 30 January 2018 - 02:08 AM

Then, How to solve this issue? I have spent 2 weeks for doing this.... and read a lot of readable material....
Was This Post Helpful? 0
  • +
  • -

#11 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 30 January 2018 - 02:19 AM

Well, the first question is how the database shall handle an array of grade level values.
Was This Post Helpful? 1
  • +
  • -

#12 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

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

Posted 30 January 2018 - 03:36 AM

For my knowledge, the query is to insert the grade level values with implode to the database, if further, I really don't know, that why I need the guidance here.
Was This Post Helpful? 0
  • +
  • -

#13 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 30 January 2018 - 03:42 AM

Quote

For my knowledge, the query is to insert the grade level values with implode to the database

Then you would use implode(). cf. http://php.net/implode

Although I have to mention that this makes for a bad database design (violating 1NF).
Was This Post Helpful? 0
  • +
  • -

#14 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • Posts: 43
  • Joined: 31-December 17

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

Posted 30 January 2018 - 03:52 AM

I have put the implode in my code, but the values that insert to my table is 'Array', how going to solve this issue?
Was This Post Helpful? 0
  • +
  • -

#15 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 30 January 2018 - 03:54 AM

Quote

foreach($StudentGradeLevel as $StudentGradeLevel)

this is bound to be a problem.
Was This Post Helpful? 0
  • +
  • -

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