how to post check box values to different fields of mysql db.Ple look at the code.In my code i have 18 options to select.i have a table of 18 fields.i just want to put these values in respective columns.Here is the code.
<?php
if(!empty($_POST['subjects'])) {
foreach($_POST['subjects'] as $check) {
echo $check."<br>";
SQL query ???
}
}
?>
<form action="check.php" method="post">
<input type="checkbox" name="subjects[]" value="PakistanStudies" />
<input type="checkbox" name="subjects[]" value="Islamiat" />
<input type="checkbox" name="subjects[]" value="Urdu(SyllabusA)" />
<input type="checkbox" name="subjects[]" value="Urdu(SyllabusB)" />
<input type="checkbox" name="subjects[]" value="Mathematics" />
<input type="checkbox" name="subjects[]" value="AdMathematics" />
<input type="checkbox" name="subjects[]" value="Sociology" />
<input type="checkbox" name="subjects[]" value="ReligiousStudies" />
<input type="checkbox" name="subjects[]" value="EnglishLanguage" />
<input type="checkbox" name="subjects[]" value="EnglishLiterature" />
<input type="checkbox" name="subjects[]" value="Chemistry" />
<input type="checkbox" name="subjects[]" value="Physics" />
<input type="checkbox" name="subjects[]" value="PrinciplesofAccounting" />
<input type="checkbox" name="subjects[]" value="Economics" />
<input type="checkbox" name="subjects[]" value="BusinessStudies" />
<input type="checkbox" name="subjects[]" value="Biology" />
<input type="checkbox" name="subjects[]" value="Statistics" />
<input type="checkbox" name="subjects[]" value="World History" />
<input type="submit" />
</form>
To connect to an SQL database via PHP, you can use the MySQLi Extension. First, make sure you enable the extension in the PHP.ini file and restart your web server so that the changes take affect.
You should find this line in the PHP.ini:
and simply change it to:
Please refer to the documentation on how to use the MySQLi extension. But a quick mock example of connecting to a server and inserting data is as followed:
Make sure you assign values to the servername, username, password, database and sqlCommand variables.
I am unsure if I have already answered your question but just to be sure; your SQL statement will look something like:
INSERT INTO table_name VALUES (val1, val2, val3…)
and note that you can specify the columns explicitly by placing them before the VALUES keyword in parenthesis like so:
INSERT INTO table_name (col1, col2, col3…) VALUES (val1, val2, val3…)
Finally; to make sure everything goes smoothly for you. Your HTML form should be on a page with the action set to another (or perhaps the same) page. When the Submit event is fired, the page will redirect (or refresh). So make sure that your action is pointing to the page with all your PHP for this to work.
If you are wondering what the POST data looks for check boxes. Try the following:
print_r($_POST[“subjects”]);
That should give you an idea of the structure of the array.
Please let me know if you have any further problems, I’d be happy to answer them.
EDIT – To help answer after comments
Note: I’m assuming that the columns are Boolean types for these check boxes in the following situation.
To insert or alter a field in the database where the value of the check box is the column name; try consructing the following SQL statement (altering it where needed):