I've been trying to read multiple checkboxes from my HTML form and store them in a MySQL database. The problem is, my query doesn't work most of the time. The PHP page runs fine, there's no MySQL error, but the query doesn't update my database.
Here's my PHP:
<?php
$host="localhost";//host name
$username="****"; //database username
$word="****";//database word
$db_name="***";//database name
$tbl_name="students"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
//Collecting data
$name = $_POST["name"];
$number = $_POST["number"];
$mail = $_POST["mail"];
$tenper = $_POST["10percentage"];
$twper = $_POST["12percentage"];
$collegearr = $_POST["unilist"];
$colval = implode(", ",$collegearr);
echo $name."<br>".$number."<br>".$mail."<br>".$tenper."<br>".$twper."<br>".$colval."<br>";
// Making query
$query = mysqli_query($con, "insert into students(name, phone_num, email, 10_per, 12_per, universities) values ('$name', '$number', '$mail', '$tenper', '$twper', '$colval')") or die(mysqli_error());
mysqli_close($connection); // Closing Connection with Server
?>
The echo works fine and prints all the data. Which means data from the form is coming through fine. Also if I remove the universities field and the $colval. The query works just fine. I've tried modifying the universities field and making it LONGTEXT just in case it was a character limit issue but that didn't work as well.
Here's how my checkboxes are declared. I'm using Bootstrap:
<div class="form-check">
<a href="https://www.ed.ac.uk/studying/undergraduate" target="_blank">
<input class="form-check-input" type="checkbox" name="unilist[]" value="The University of Edinburgh">
<label class="form-check-label" name="uni[]" for="defaultCheck1">The University of Edinburgh</label>
</a>
</div>
I can't seem to figure out what the bug here is. I've tried everything I read online and on SO regarding dealing with multiple checkboxes but I can't seem to get it to work.
P.S The query works sometimes when I either
- Remove the comma from the
implode()
- Select only 1-2 checkboxes
Sometimes it won't work even with these conditions
Aucun commentaire:
Enregistrer un commentaire