I have two input fields where a user can enter a name of a person and then choose their role, faculty or instructor. For the roles I am using tinyint to determine whether it is true or false (0 or 1). I am having difficulties saving the values to mysql db. When submitting the query nothing is being stored. EXAMPLE
<?php
if(isset($_POST['submit'])){
$con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$person1_role1 = empty($_POST['role1'][1]) ? 0 : 1;
$person2_role2 = empty($_POST['role2'][1]) ? 0 : 1;
$person_name1 = $_POST['person_name1'];
$person_name2 = $_POST['person_name2'];
$option = $_POST['role'];
if($option == 'faculty'){
mysqli_query($con,"INSERT INTO person (contact_role, person_name) VALUES ($person1_role1, $person_name1)");
} elseif ($option == 'instructor') {
mysqli_query($con,"INSERT INTO person (instructor_role, person_name) VALUES ($person2_role2, $person_name2)");
}
print_r($_POST);
}
?>
<form action="test2.php" method="POST">
<b>Select the role for the person</b>
</br>
</br>
Name:<input type="text" name="person_name1">
<input type="checkbox" name="role1[]" value="faculty">faculty
<input type="checkbox" name="role1[]" value="instructor">instructor<br><br>
Name:<input type="text" name="person_name2">
<input type="checkbox" name="role2[]" value="faculty">faculty
<input type="checkbox" name="role2[]" value="instructor">instructor<br><br>
<input value="SAVE" name="submit" type="submit">
</form>
Table
CREATE TABLE IF NOT EXISTS `person` (
`person_id` int(11) NOT NULL auto_increment,
`faculty_role` tinyint(1) NOT NULL,
`instructor_role` tinyint(1) NOT NULL,
`person_name` varchar(50) NOT NULL,
PRIMARY KEY (`person_id`),
);
<select>or<input type="radio" />. And you're using$_POST['role']but as you can see in yourprint_r($_POST)that key doesn't exists.facultyorinstructor?