[英]I want to redirect to my admin page after I click the submit button
我使用標題功能單擊提交按鈕后嘗試重定向。 它沒有用,所以我在頂部嘗試了ob_start,然后在底部嘗試了ob_clean。 但是當我這樣做時,它會自動將我推送到我要重定向到的頁面。 繼承人的代碼的任何幫助將不勝感激。
<?php
ob_start();
// Create connection
require_once "../includes/db_conn.php";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
# Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo 'connected';
}
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$image = $_POST['image'];
}
$sql = "INSERT INTO article (`title`, `content`, `image`) VALUES ('$title', '$content','$image');";
$result = $conn->query($sql);
$conn->close();
echo <<<END_OF_FORM
<form method='post' action='article_new.php'>
Title:
<input type='text' value='$title' name='title'><br>
Content:
<input type='text' value='$content' name='content'><br>
Image:
<input type='text' value='$image' name='image'><br>
<input type='submit' value='new article' name='submit'>
</form>
END_OF_FORM;
ob_clean();
header('location: admin.php')
?>
<a href="admin.php">Admin</a>
為什么sql和重定向超出if(isset($_POST['submit']))
條件? 嘗試這樣的事情:
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$image = $_POST['image'];
$sql = "INSERT INTO article (`title`, `content`, `image`) VALUES ('$title','$content','$image');";
$result = $conn->query($sql);
$conn->close();
header('location: admin.php');
}
echo <<<END_OF_FORM
<form method='post' action='article_new.php'>
Title:
<input type='text' value='$title' name='title'><br>
Content:
<input type='text' value='$content' name='content'><br>
Image:
<input type='text' value='$image' name='image'><br>
<input type='submit' value='new article' name='submit'>
</form>
END_OF_FORM;
ob_clean();
?>
<a href="admin.php">Admin</a>
按照這種邏輯,提交表單並將數據插入數據庫后,頁面將被重定向。
提交用戶輸入時,您應該對其進行處理。 因此,將它們放在isset($_POST['submit'])
塊中。 如果要在插入完成后進行重定向,請在其中放入您的進程,然后在最后進行重定向。 而且,您應該使用准備好的語句,因為這樣容易產生SQL注入。
在進行任何標題重定向之前,您不應該顯示輸出。
<?php
require_once "../includes/db_conn.php";
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$image = $_POST['image'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
# Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO article (`title`, `content`, `image`) VALUES (?, ?, ?)";
$insert = $conn->prepare($sql);
$insert->bind_param('sss', $title, $content, $image);
$insert->execute();
header('Location: admin.php');
exit;
}
?>
<form method='post' action='article_new.php'>
<label>Title: </label>
<input type='text' name='title'><br>
<label>Content: </label>
<input type='text' name='content'><br>
<label>Image: </label>
<input type='text' name='image'><br>
<input type='submit' value='new article' name='submit'>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.