Hi, I have written this code in PHP that addeds a new engineer to my value to table engineers to my engineer column.
When i click create enginner button my page refreshes with no errors no PHP errros i get no success or warning errors. i have tried with existing name and new names.
CODE:
<?php
include('nav.php');
include('ConnectionDB.php');
$msg = '';
$result = false;
$server = htmlentities($_SERVER['PHP_SELF']);
if (isset($_POST['submit'])) {
//Create new enginner
$result = create_eng();
}
?>
<div class="jumbotron">
<div class="container">
<h1><br><img src="../images/clipboard-customer.png"/> Add New Engineer:</br></h1>
</div>
</div>
</head>
<body>
<form class="form-horizontal" role="form" id="add_eng">
<div class="form-group" action="<?php $server ?>" method = "post">
<?php display_msg($msg, $result);?>
<label for="EngineerName" class="col-sm-2 control-label">Engineer Name:</label>
<div class="col-sm-6">
<input type="EngineerName" class="form-control" id="EngineersName">
<small class="form-text text-muted">
Enter the new engineer's name above to enter them into the database.
</small>
</div>
<input name="submit" type="submit" value="Create Engineer" class="btn btn-primary default"></input>
<button type="input" class="btn btn-primary default" onclick="job_reset()">Reset Form</button>
</div>
</form>
<?php
$sql = "Select * from [Engineers] where [Engineer] not like '%\_%' Escape '\' order by [Engineer] asc";
$stmt = $conn->prepare($sql);
$stmt->execute();
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="containerenglist">
<div class="col-sm-5">
<table id="alleng" class="table table-bordered">
<tr>
<th>Active Engineer's Names:</th>
</tr>
<?php
foreach ($results as $row) {
echo "<tr><td>";
echo $row['Engineer'];
echo "</td>";
echo "</tr>";
}
?>
</table>
</div>
</div>
<script>
function job_reset() {
document.getElementById("job_reset").reset();
}
</script>
</body>
</html>
<?php
//create enginner function
function create_eng(){
//validate Enginner
$eng_name = validate_eng();
if ($eng_name){
//check if department exists
if(!eng_exist($eng_name)){
//insert enginner
return insert_eng($eng_name);
}
}
return false;
}
//message creation
function display_msg($msg,$type){
$type === true ? $ccClass = "alert-success" :
$cssClass = "alert-error";
If($msg != ''){
?>
<div class="alert <?php echo $cssClass; ?>">
<?php echo $msg; }?>
</div>
<?php
}
//check if enginner exists
function eng_exist($eng_name){
global $msg, $conn;
$sql_select = "select [enginner] from [enginners]
where name = " . $conn->quote($eng_name) . "
limit 1";
$stmt = $conn->query($sql_select);
if($stmt === false) {
$msg = 'Error querying engineer table';
return null;
}
$r = $stmt->fetch(PDO::fetch_Assoc);
If($r !== false){
$msg = "Enginner with name $eng_name exists in Database.";
return true;
}else
return false;
}
//Add to Database
function insert_eng($eng_name){
global $conn, $msg;
//SQL insert statement
$sql = "insert into [Enginners](Engineer) values (" . $conn->quote($eng_name) .")";
if($conn->exec($sql) === false) {
$msg = 'Error inserting the engineer.';
return false;
}else{
$msg = "The new engineer $eng_name has been created";
return true;
}
}
function validate_eng(){
global $msg;
$eng_name = $_POST['EngineersName'];
if($eng_name != ''){
$eng_name = filter_var($eng_name,FILTER_SANITIZE_STRING);
return $eng_name;
}else{
$msg = 'Please enter the engineers name';
return false;
}
}
?>
I hope its all copied across correctly and is readable.