I would like an error to be outputted on the addStudent.php page if the combination of firstname and last name are already in the Student table. Currently, it simply doesn't add a non-unique entry into the table, but doesn't inform the user that the entry already exists.
Table structure:
<?php
include 'connect.php';
// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS student (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fname VARCHAR(30),
lname VARCHAR(30),
mclass VARCHAR(30),
aclass VARCHAR(30),
UNIQUE (`fname`, `lname`)
)";
if ($conn->query($sql) === TRUE) {
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
addStudent.php
<fieldset>
<legend><h2> Details </h2></legend>
<form class="pure-form">
<label>First Name </label><input type="text" id="firstname" name = "firstname" autofocus=""><br><br>
<label>Last Name </label><input type="text" id="lastname" name = "lastname"><br><br>
</form>
</fieldset>
<br>
<fieldset>
<legend><h2>Classes</h2></legend>
<form class="pure-form">
<label>Morning Class </label>
<select id = "morningclass" name="morningclass">
<option value=""> </option>
<option value="G1F">G1-F</option>
<option value="G1S">G1-S</option>
<option value="G2J">G2-J</option>
<option value="G2A">G2-A</option>
<option value="G3">G3</option>
<option value="G4">G4</option>
<option value="G5">G5</option>
</select>
<br> <br>
<label>Afternoon Class </label>
<select id = "afternoonclass" name = "afternoonclass">
<option value=""> </option>
<option value="7P">7P</option>
<option value="7H">7H</option>
<option value="8P">8P</option>
<option value="8H">8H</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select>
<br> <br>
</form>
</fieldset>
<br>
<div class="buttonAlign">
<input type="button" value="Cancel" onclick="cancel();" class="button-error pure-button">
<input type="button" value="Add" id="button" class="button-secondary pure-button">
</div>
<br>
</body>
</div >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
var fname=$("#firstname").val();
var lname=$("#lastname").val();
var mclass=$("#morningclass").val();
var aclass=$("#afternoonclass").val();
if (fname !== "" && lname !== "" && mclass !== "" && aclass !== "") {
$.ajax({
type:"post",
url:"add.php",
data:"firstname="+fname+"&lastname="+lname+"&morningclass="+mclass+"&afternoonclass="+aclass,
success:function(){
alert("Entry added");
window.location.href = "viewStudent.php";
}
});
document.getElementById("firstname").value = "";
document.getElementById("lastname").value = "";
document.getElementById("morningclass").value = "";
document.getElementById("afternoonclass").value = "";
} else {
alert("You must fill out all the empty information!");
}
});
});
function cancel() {
window.location.href = "viewStudent.php";
}
</script>
add.php
<?php
include 'connect.php';
$fname=preg_replace('/[^a-z]/', "", strtolower($_POST["firstname"]));
$lname=preg_replace('/[^a-z]/', "", strtolower($_POST["lastname"]));
$mclass=$_POST["morningclass"];
$aclass=$_POST["afternoonclass"];
$sql=("INSERT INTO student(fname,lname,mclass,aclass) values('$fname','$lname','$mclass','$aclass')");
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>