Я пытаюсь, чтобы пользователь заполнил форму для обновления своего блога, после того, как он заполнил форму и заполнил все необходимые поля, он будет перенаправлен на страницу индекса и на эту страницу.будет текст «Спасибо, ваше сообщение обновлено».
Я попытался перейти к действию формы и изменить его на выражение if else
<?php
#this is saying if the following fields are not filled out, you will stay on updateform.php and if they are you will be moved to the updatehandle.php page
if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['id'] != NULL) && ($_POST['comment'] != NULL)) {
echo "updatehandle.php?id=" . $_GET['id'];
$submit = true;
}
else
{
echo "updateform.php?id=" . $_GET['id'];
}
?>
, но это не такне работает, потому что тогда он потеряет значение id, когда я попытаюсь отправить его.Я попытался создать другой оператор $ result, который перенаправил бы пользователя на страницу индекса, но не знал, с чего начать, потому что у меня уже было это утверждение
if ($result) {
echo "<center><strong>Thank you, the Blog Post has been updated.</strong></center><br /><br />";
$title = NULL;
$title = NULL;
}
else {
echo "<strong>There was an error! </strong>" . mysqli_error($dbc);
}
}
?>
вот код для обновления.php page
<?php session_start(); ?>
<html>
<head>
</head>
<body>
<!-- styles the page-->
<style type="text/css">
body{
font-family: Arial, Gadget, sans-serif;
background-color: #FFD9D9;
}
strong{
color: red;
font-weight: bold;
}
center{
margin: auto;
}
</style>
<!-- includes the header and mysqli pages-->
<?php
include('./includes/header.html');
require('./includes/mysqli_connect.php');
?>
<!-- have access to posting special characters and will trim unnecessary spaces-->
<?php
$userid = mysqli_real_escape_string($dbc, trim($_GET['id']));
$title = mysqli_real_escape_string($dbc, trim($_POST['title']));
$post = mysqli_real_escape_string($dbc, trim($_POST['post']));
$blogid = $_GET['id'];
#This adds in the comment based on their ID, email,fname,lname and date they made the comment
if (($title != NULL) && ($post != NULL)){
$query = "UPDATE blogposts SET title='$title', post='$post' WHERE blogid=$blogid";
$result = mysqli_query($dbc, $query);
#This displays an error if there was an error in making a comment
#EDIT: The "There was an error" never will display since my form validates if criteria was filled out or not, if it's not it makes the guest fill out that area
if ($result) {
echo "<center><strong>Thank you, the Blog Post has been updated.</strong></center><br /><br />";
$title = NULL;
$title = NULL;
}
else {
echo "<strong>There was an error! </strong>" . mysqli_error($dbc);
}
}
?>
<?php
//run query if id was entered
if (isset($blogid)) {
//this is our SELECT qurt
$q = "SELECT * FROM blogposts WHERE blogid='" .$_GET['id']. "'";
//This issues our command to the database server
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
}
echo $q;
?>
<form action="<?php echo basename(__FILE__) . "?id=" . $blogid; ?>" method="post">
<?php
if(($_POST['title'] == NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')){
echo "<strong><center>Please fill in your Blog Post Title!</center></strong><br />";
}
?>
<p><label><center><b>Blog Post Title: </p></label></center></b><p align="center"><input name="title" type="text" size="38.5" value="
<?php
echo $_POST['title'];
?>
"/>
</p>
<br /><br />
<?php
if(($_POST['post'] == NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')){
echo "<strong><center>Please fill in your Blog Post Body!</center></strong><br />";
}
?>
<p><center><label><b>Blog Post Body: </p></center></label></b>
<p align="center"><textarea name="post" cols="40" rows="5">
<?php echo $_POST['post'];
?>
</textarea>
</p>
<br />
<p align="center"><input type="submit" name="submit" value="Submit" /></p>
</form>
<!--includes the footer page-->
<?php
include('./includes/footer.html');
?>
</body>
</html>
и вот код для индексной страницы
<html>
<head>
</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
font-family: Arial, Gadget, sans-serif;
background-color: #FFD9D9;
}
strong{
color: red;
font-weight: bold;
}
center{
border-left: .17em dashed;
border-top: .17em solid;
border-right: .17em dashed;
border-bottom: .17em solid;
padding-left:25px;
padding-bottom: 20px;
width: 1000px;
background-color: #E1A0A0;
border-color: black;
margin: auto;
text-align: left;
}
h3 {
text-align: center;
color: red;
}
</style>
<!-- this includes the header and mysqli pages-->
<?php
include('header.php');
include('mysqli_connect.php');
?>
<!--SORTING CODE-->
<?php
// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'd';
// Determine the sorting order:
switch ($sort) {
case 'ln':
$order_by = 'lname ASC';
break;
case 'fn':
$order_by = 'fname ASC';
break;
case 'd':
$order_by = 'date ASC';
break;
default:
$order_by = 'date ASC';
$sort = 'd';
break;
}
?>
<?php
?>
<div align="center">
<?php
#sort by links
echo
"<b>Sort By: <a href='index.php?sort=ln'>Last Name</a> |
<a href='index.php?sort=fn'>First Name</a> |
<a href='index.php?sort=rd'>Date Posted</a></b>";
?>
</div>
<br/>
<?php
#END OF SORTING CODE
#PAGINATION CODE
// Number of records to show per page:
$display = 5;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
// Count the number of records:
$q = "SELECT COUNT(id) FROM guestbook";
$r = @mysqli_query ($dbc, $q);
$row = @mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
#END OF PAGINATION CODE
#DELETE HANDLE CODE
if($_GET['id'] != NULL) {
$id = $_GET['id'];
$querydel = "DELETE FROM guestbook WHERE id='$id'";
$resultdel = mysqli_query($dbc, $querydel);
#This determines if the comment is going to be deleted or if there is an error when deleting
if ($resultdel) {
echo '<h3>Your comment has been deleted. You will not see your ID, First, Last, Email, Comment, or Date Posted.</h3>';
}
else {
echo "There was an error! " . mysqli_error($dbc);
}
}
#END OF DELETE HANDLE CODE
?>
<?php
$query = "SELECT * FROM guestbook ORDER BY $order_by LIMIT $start, $display";
$result = mysqli_query($dbc, $query);
?>
<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<strong><center><br/>ID: </strong>" . $row['id'] . "<br />" . "<strong>First Name: </strong>" . $row['fname'] . "<br />" . "<strong>Last Name: </strong>" . $row['lname'] . "<br />" . "<strong>Email: </strong>" . $row['email'] . "<br />" . "<strong>Comment: </strong>" . $row['comment'] . "<br />" . "<strong>Posted At: </strong>" . $row['date'] . "<br/><br/>" . "<a href='updateform.php?id=" . $row['id'] . "'>Update Comment</a> | <a href='index.php?id=" . $row['id'] . "'>Delete</a> <br/>" . "</center>\n";
}
?>
<div align="center"><b>
<?php
// Make the links to other pages, if necessary.
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a> ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
} // End of FOR loop.
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
?>
</div>
</b>
<!--this includes the footer page-->
<?php
include('footer.php');
?>
</body>
</html>
Я бы хотел, чтобы пользователь успешно заполнил все необходимые поля (заголовок, должность иидентификатор, прикрепленный к нему, чтобы он мог обновляться), а затем пользователь перенаправляется на страницу индекса и отображает сообщение с благодарностью.