So, i’m doing a little CRUD as a project to train PHP so I can move on an learn symfony. To give context, it’s a school managing system, with teachers, classes, students, etc. In this page i’m trying to delete the class, which works, however I want it to set the class to NULL in all students which were part of that class. For example:
jake - 11 years - classA
*classA deleted*
jake - 11 years - NULL
There’s also another query in the code which does the same thing, but to a specefic relation table between classes and students, which only has the student id and the class name.
Here’s the code below
<?php
if(isset($_POST["id_turma"]) && !empty($_POST["id_turma"])){
$turma=trim($_GET["turma"]);
echo $turma;
// Include config file
require_once "config.php";
// Prepare a delete statement
$sql = "DELETE FROM turmas WHERE id_turma = ?";
$sql2 = "UPDATE alunos SET turma = 'NULL' WHERE turma = ".$turma;
mysqli_query($link,$sql2);
$sql3 = "DELETE FROM turmaalunos WHERE turma =".$turma;
mysqli_query($link,$sql3);
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 = trim($_POST["id_turma"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records deleted successfully. Redirect to landing page
header("location: turmas.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
if(empty(trim($_GET["id_turma"]))){
// 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>Delete Record</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
<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 mb-3">Delete Record</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method="post">
<div class="alert alert-danger">
<input type="hidden" name="id_turma" value="<?php echo trim($_GET["id_turma"]); ?>"/>
<p>Tem mesmo a certeza que deseja eliminar a informação desta turma?</p>
<p>
<input type="submit" value="Yes" class="btn btn-danger">
<a href="index.php" class="btn btn-secondary">No</a>
</p>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>