I am currently making a simple bulletin board with PHP.
Therefore, when I press the edit button from the post list page, I would like to add a function that allows me to jump to the edit screen if I am logged in and return to the post list screen if I am not logged in.
The list of posts is admin.php and the edit screen is edit.php.
However, even if you log in and press the edit button on admin.php, it will always be returned from edit.php to admin.php.
I want you to stay at edit.php when you are logged in well.
Below is the code for edit.php.
<?php
var_dump($_SESSION['admin_login']);
exit();
$link=mysqli_connect("localhost", "root", "root", "keijiban");
date_default_timezone_set('Asia/Tokyo');
if(empty($_SESSION['admin_login'])||$_SESSION['admin_login']!==true){
// Redirect to Login Page
header("Location: ./admin.php");
}
if(!empty($_GET['comment_id'])&empty($_POST['comment_id']){
$comment_id=(int)htmlspecialchars($_GET['comment_id',ENT_QUOTES);
// Connect to Database
$mysqli = new mysqli("localhost", "root", "root", "keijiban");
// Verifying Connection Errors
if($mysqli->connect_errno){
$error_message[] = 'Database connection failed. Error number '.$mysqli->connect_errno.': '.$mysqli->connect_error;
} else{
// Loading Data
$sql="SELECT* FROM comment WHERE id=$comment_id";
$res=$mysqli->query($sql);
if($res){
$arrayData=$res->fetch_assoc();
} else{
// Return to list if no data is read
header("Location: ./admin.php");
}
$mysqli->close();
}
} elseif(!empty($_POST['comment_id']){
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<metacharset="utf-8">
<title>Terry Bulletin Board Management Page (Edit Posts) </title>
<style>
.btn_cancel{
display:inline-block;
margin-right —10px;
padding —10px20px;
color:#555;
font-size: 86%;
border-radius:5px;
border —1px solid#999;
}
.btn_cancel:hover{
color:#999;
border-color:#999;
text-decoration: none;
}
</style>
</head>
<body>
<h1>Terry bulletin board management page (editing posts)</h1>
<?php if(!empty($error_message): ?>
<ul class="error_message">
<?php foreach($error_message as$value): ?>
<li> ·<?phpecho$value;?></li>
<?php endforeach;?>
</ul>
<?php endif;?>
<div class="container">
<form method="post">
<div class="form-group">
<label for="form-mail">Username</label>
<input type="username" class="form-control" name="username" placeholder="tree" value="<?php if(!empty($arrayData['username'])) {echo$arrayData['username'];}?>
</div>
<div class="form-group">
<label for="exampleInputComment">Comments</label>
<input type="comment" class="form-control" name="comment" placeholder="Post freely!" value="<?php if(!empty($arrayData['comment'])) {echo$arrayData['comment'];}?>">
</div>
<a class="btn_cancel" href="admin.php">Cancel</a>
<button type="submit" class="btn btn-primary">Update</button>
<input type="hidden" name="comment_id"value="<?phpecho$arrayData['id'];?>">
</form>
</div>
</body>
</html>
I'm a beginner at php.
If it hasn't been resolved yet, just for your information.
Looking at the source, there are two conditions for redirecting to ./admin.php.
One is when the SESSION value is not included and the second is when SQL execution fails.
I think the following are possible reasons why the value of SESSION is not included.
I think the reasons for SQL execution failures are as follows:
The above may be the cause, so please check it out.
© 2024 OneMinuteCode. All rights reserved.