230 lines
9.3 KiB
PHP
230 lines
9.3 KiB
PHP
<?php
|
|
// Include config file
|
|
require_once "config.php";
|
|
|
|
// Define variables and initialize with empty values
|
|
$vokabel = $stammform = $art = $flexion = $uebersetzung = $lektion =$kommentar = "";
|
|
$vokabel_err = $stammform_err = $art_err = $flexion_err = $uebersetzung_err = $lektion_err =$kommentar_err = "";
|
|
|
|
|
|
// Processing form data when form is submitted
|
|
if(isset($_POST["id"]) && !empty($_POST["id"])){
|
|
// Get hidden input value
|
|
$id = $_POST["id"];
|
|
|
|
// Validate vokabel
|
|
$input_vokabel = trim($_POST["vokabel"]);
|
|
if(empty($input_vokabel)){
|
|
$vokabel_err = "Vokabel eingeben.";
|
|
} else{
|
|
$vokabel = $input_vokabel;
|
|
}
|
|
|
|
// Validate stammform
|
|
$input_stammform = trim($_POST["stammform"]);
|
|
if(empty($input_stammform)){
|
|
$stammform_err = "Bitte Stammform eintragen.";
|
|
} else{
|
|
$stammform = $input_stammform;
|
|
}
|
|
|
|
// Validate art
|
|
$input_art = trim($_POST["art"]);
|
|
if(empty($input_art)){
|
|
$art_err = "Bitte Wortart eingeben.";
|
|
} else{
|
|
$art = $input_art;
|
|
}
|
|
|
|
// Validate flexion
|
|
$input_flexion = trim($_POST["flexion"]);
|
|
if(empty($input_flexion)){
|
|
$flexion_err = "Bitte Flexion eingeben.";
|
|
} else{
|
|
$flexion = $input_flexion;
|
|
}
|
|
|
|
// Validate uebersetzung
|
|
$input_uebersetzung = trim($_POST["uebersetzung"]);
|
|
if(empty($input_uebersetzung)){
|
|
$uebersetzung_err = "Bitte Übersetzung eingeben.";
|
|
} else{
|
|
$uebersetzung = $input_uebersetzung;
|
|
}
|
|
|
|
// Validate lektion
|
|
$input_lektion = trim($_POST["lektion"]);
|
|
if(empty($input_lektion)){
|
|
$lektion_err = "Bitte Lektion eingeben.";
|
|
} else{
|
|
$lektion = $input_lektion;
|
|
}
|
|
|
|
// Validate kommentar
|
|
$input_kommentar = trim($_POST["kommentar"]);
|
|
if(empty($input_kommentar)){
|
|
$kommentar_err = "Bitte Tipp eingeben.";
|
|
} else{
|
|
$kommentar = $input_kommentar;
|
|
}
|
|
|
|
|
|
// Check input errors before inserting in database
|
|
if(empty($vokabel_err) && empty($stammform_err) && empty($art_err)
|
|
&& empty($flexion_err) && empty($uebersetzung_err) && empty($lektion_err) && empty($kommentar_err) ){
|
|
// Prepare an update statement
|
|
|
|
$sql = "UPDATE vokabeln SET vokabel=?, stammform=?, art=?, flexion=?, uebersetzung=?, lektion=?, kommentar=? WHERE id=?";
|
|
|
|
if($stmt = mysqli_prepare($link, $sql)){
|
|
// Bind variables to the prepared statement as parameters
|
|
mysqli_stmt_bind_param($stmt, "sssssssi", $param_name, $param_stammform, $param_art, $param_flexion, $param_uebersetzung, $param_lektion, $param_kommentar, $param_id);
|
|
|
|
// Set parameters
|
|
$param_vokabel = $vokabel;
|
|
$param_stammform = $stammform;
|
|
$param_art = $art;
|
|
$param_flexion = $flexion;
|
|
$param_uebersetzung = $uebersetzung;
|
|
$param_lektion = $lektion;
|
|
$param_kommentar = $kommentar;
|
|
|
|
// Attempt to execute the prepared statement
|
|
if(mysqli_stmt_execute($stmt)){
|
|
// Records updated successfully. Redirect to landing page
|
|
header("location: index.php");
|
|
exit();
|
|
} else{
|
|
echo "Oops! Something went wrong. Please try again later.";
|
|
}
|
|
}
|
|
|
|
// Close statement
|
|
mysqli_stmt_close($stmt);
|
|
}
|
|
|
|
// Close connection
|
|
mysqli_close($link);
|
|
} else{
|
|
// Check existence of id parameter before processing further
|
|
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
|
|
// Get URL parameter
|
|
$id = trim($_GET["id"]);
|
|
|
|
// Prepare a select statement
|
|
$sql = "SELECT * FROM vokabeln WHERE id = ?";
|
|
if($stmt = mysqli_prepare($link, $sql)){
|
|
// Bind variables to the prepared statement as parameters
|
|
mysqli_stmt_bind_param($stmt, "i", $param_id);
|
|
|
|
// Set parameters
|
|
$param_id = $id;
|
|
|
|
// Attempt to execute the prepared statement
|
|
if(mysqli_stmt_execute($stmt)){
|
|
$result = mysqli_stmt_get_result($stmt);
|
|
|
|
if(mysqli_num_rows($result) == 1){
|
|
/* Fetch result row as an associative array. Since the result set
|
|
contains only one row, we don't need to use while loop */
|
|
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
|
|
// Retrieve individual field value
|
|
$vokabel = $row["vokabel"];
|
|
$stammform = $row["stammform"];
|
|
$art = $row["art"];
|
|
$flexion = $row["flexion"];
|
|
$uebersetzung = $row["uebersetzung"];
|
|
$lektion = $row["lektion"];
|
|
$kommentar = $row["kommentar"];
|
|
} else{
|
|
// URL doesn't contain valid id. Redirect to error page
|
|
header("location: error.php");
|
|
exit();
|
|
}
|
|
|
|
} else{
|
|
echo "Oops! Something went wrong. Please try again later.";
|
|
}
|
|
}
|
|
|
|
// Close statement
|
|
mysqli_stmt_close($stmt);
|
|
|
|
// Close connection
|
|
mysqli_close($link);
|
|
} else{
|
|
// URL doesn't contain id parameter. Redirect to error page
|
|
header("location: error.php");
|
|
exit();
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Update Record</title>
|
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
|
<style>
|
|
.wrapper{
|
|
width: 600px;
|
|
margin: 0 auto;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="wrapper">
|
|
<div class="container-fluid">
|
|
<div class="row">
|
|
<div class="col-md-12">
|
|
<h2 class="mt-5">Update Record</h2>
|
|
<p>Please edit the input values and submit to update the employee record.</p>
|
|
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
|
|
<div class="form-group">
|
|
<label>Vokabel</label>
|
|
<input type="text" name="vokabel" class="form-control <?php echo (!empty($vokabel_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $vokabel; ?>">
|
|
<span class="invalid-feedback"><?php echo $vokabel_err;?></span>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Stammform</label>
|
|
<textarea name="stammform" class="form-control <?php echo (!empty($stammform_err)) ? 'is-invalid' : ''; ?>"><?php echo $stammform; ?></textarea>
|
|
<span class="invalid-feedback"><?php echo $stammform_err;?></span>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Wortart</label>
|
|
<input type="text" name="art" class="form-control <?php echo (!empty($art_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $art; ?>">
|
|
<span class="invalid-feedback"><?php echo $art_err;?></span>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>lexion</label>
|
|
<input type="text" name="flexion" class="form-control <?php echo (!empty($flexion_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $flexion; ?>">
|
|
<span class="invalid-feedback"><?php echo $flexion_err;?></span>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Übersetzung</label>
|
|
<input type="text" name="uebersetzung" class="form-control <?php echo (!empty($uebersetzung_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $uebersetzung; ?>">
|
|
<span class="invalid-feedback"><?php echo $uebersetzung_err;?></span>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Lektion</label>
|
|
<input type="text" name="lektion" class="form-control <?php echo (!empty($lektion_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $lektion; ?>">
|
|
<span class="invalid-feedback"><?php echo $lektion_err;?></span>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Tipp</label>
|
|
<input type="text" name="kommentar" class="form-control <?php echo (!empty($kommentar_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $kommentar; ?>">
|
|
<span class="invalid-feedback"><?php echo $kommentar_err;?></span>
|
|
</div>
|
|
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
|
|
<input type="submit" class="btn btn-primary" value="Submit">
|
|
<a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|