I have a series of checkboxes that interact with the DB in 3 possible ways; INSERT
, DELETE
, no action. I will have a total of 8-10 checkboxes, however each checkboxes requires 32 lines of code (inc 3 if statements) to work. Rather than have 320 lines of code for 10 checkboxes I believe a function would be more efficient. My knowledge of PHP is still newbie, and as such I'm unsure how I would go about containing the below code into a working function. I have looked all over Google but haven't found anything to help get mine working. Any help would be appreciated!
PHP Code:
//If the Java checkbox is checked it will check to see if java is already a skill assigned to the user. If so it will ignore, if not it will add.
if (isset($_POST['java'])){
if (empty($_POST['javaexp'])){
//header('Location: UpdateAccount.php');
//die();
}else{
$javaexp = $_POST['javaexp'];}
$sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = 1")
or die(mysqli_error($con));
$row = mysqli_fetch_assoc($sql);
if ($row ['total'] == "0"){
$sql = $con->query("INSERT INTO userskills ( UserID, SkillID, Experience) VALUES ($User, 1, $javaexp)");
//If the Java checkbox is not checked it will check to see if java is already a skill assigned to the user. If they are it will delete it. If not it will ignore.
}} else{
$sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = 1")
or die(mysqli_error($con));
$row = mysqli_fetch_assoc($sql);
if ($row ['total'] == "1"){
$sql = $con->query("DELETE FROM userskills
WHERE UserID = $User AND SkillID = 1");
}}
$con = sql connection on another php file.
HTML Code:
<p>
<label>
<input type="checkbox" name="java" value="checkbox" id="CheckboxGroup1_0" class="skillselect">
Java</label> <input type="number" name="javaexp" class="expnumber" placeholder="Enter Experience in years.">
<br>
<br>
<label>
<input type="checkbox" name="iOS" value="checkbox" id="CheckboxGroup1_1" class="skillselect">
Checkbox</label> <input type="number" name="" class="expnumber" placeholder="Enter Experience in years.">
<br>
<br>
<label>
<input type="checkbox" name="PHP" value="checkbox" id="CheckboxGroup1_2" class="skillselect">
Checkbox</label> <input type="number" name="" class="expnumber" placeholder="Enter Experience in years.">
<br>
<br>
</p>
<div class="FormElement">
<input name="Update" type="submit" class="button" id="Update" value="Submit Changes">
</div>
Aucun commentaire:
Enregistrer un commentaire