Created
June 27, 2022 06:16
-
-
Save msisaifu/a16fe66cfb626ddc4319a72253723725 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$servername = "localhost"; | |
$username = "root"; | |
$password = ""; | |
$dbname = "students"; | |
$table_name = 'student_info'; | |
$inserted = false; | |
$something_wrong = false; | |
$id = $_GET['id']; | |
$rows = null; | |
// Create connection | |
$conn = mysqli_connect($servername, $username, $password, $dbname); | |
// Check connection | |
if (!$conn) { | |
die("Connection failed: " . mysqli_connect_error()); | |
} | |
if ($_POST && isset($_POST['submit'])) { | |
$id = $_POST['id'] ? $_POST['id'] : null; | |
$name = $_POST['name'] ? $_POST['name'] : ''; | |
$roll = $_POST['roll'] ? $_POST['roll'] : ''; | |
$department = $_POST['department'] ? $_POST['department'] : ''; | |
$sql = "UPDATE {$table_name} SET | |
`name` = '{$name}', | |
`roll` = '{$roll}', | |
`department` = '{$department}' | |
WHERE `id` = {$id}"; | |
if (mysqli_query($conn, $sql)) { | |
header("Location: index.php"); | |
} else { | |
$something_wrong = true; | |
echo "Error: " . $sql . "<br>" . mysqli_error($conn); | |
} | |
} | |
$sql = "SELECT * FROM {$table_name} WHERE id='{$id}'"; | |
$result = $conn->query($sql); | |
if (mysqli_num_rows($result) > 0) { | |
$rows = mysqli_fetch_assoc($result); | |
} else { | |
header("Location: index.php"); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="notification is-light"> | |
<h2 class="is-flex is-justify-content-center has-text-weight-bold is-size-4">Database Management System</h2> | |
</div> | |
<div class="panel is-danger"> | |
<p class="panel-tabs"> | |
<a href="index.php">List</a> | |
<a href="add.php" class="is-active">Add</a> | |
</p> | |
<?php | |
if ($inserted) { ?>. | |
<div class="columns mt-4"> | |
<div class="column is-6 is-offset-3"> | |
<article class="message is-success"> | |
<div class="message-body"> | |
Data insertion successful. | |
</div> | |
</article> | |
</div> | |
</div> | |
<?php } ?> | |
<?php | |
if ($something_wrong) { ?>. | |
<div class="columns mt-4"> | |
<div class="column is-6 is-offset-3"> | |
<article class="message is-danger"> | |
<div class="message-body"> | |
Something wrong in insertion. | |
</div> | |
</article> | |
</div> | |
</div> | |
<?php } ?> | |
<div class="columns mt-4"> | |
<div class="column is-6 is-offset-3"> | |
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> | |
<input value="<?php echo $rows['id'] ?>" name="id" type="hidden"> | |
<div class="field"> | |
<label class="label">Name</label> | |
<div class="control"> | |
<input required class="input" value="<?php echo $rows['name'] ?>" name="name" type="text" placeholder="Student Name"> | |
</div> | |
</div> | |
<div class="field"> | |
<label class="label">Roll</label> | |
<div class="control"> | |
<input required class="input" value="<?php echo $rows['roll'] ?>" name="roll" type="text" placeholder="Roll"> | |
</div> | |
</div> | |
<div class="field"> | |
<label class="label">Department</label> | |
<div class="control"> | |
<input required class="input" value="<?php echo $rows['department'] ?>" name="department" type="text" placeholder="Department"> | |
</div> | |
</div> | |
<div class="field is-grouped"> | |
<div class="control"> | |
<input type="submit" class="button is-link" name="submit" value="Submit"> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment