单击提交按钮后,我想重定向到我的管理页面

时间:2014-12-04 03:41:23

标签: php

我在使用标题功能点击提交按钮后尝试重定向。它没有用,所以我在顶部尝试了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>      

2 个答案:

答案 0 :(得分:0)

为什么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>

使用此逻辑,在提交表单并将数据插入数据库后,您的页面将被重定向。

答案 1 :(得分:0)

您应该在提交时处理用户输入。所以把它们放在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>