douji1999 2015-08-02 15:20
浏览 31
已采纳

PHP:数据添加到数据库后页面没有重定向[重复]

This question already has an answer here:

I wrote this code that adds data from a form to a database. That part works.

I am trying to redirect my page to completed.php after the data has been added sucessfully. Now it only adds data to my database table and nothing more.

This is my code:

<html>
<head>
<title>insert data in database using PDO(php data object)</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="main">
<h1>Insert data into database using PDO</h1>
<div id="login">
<h2>Student's Form</h2>
<hr/>
    <form action="" method="post">
        <label>Student Name :</label>
        <input type="text" name="username" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
        <label>Student Email :</label>
        <input type="email" name="email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
        <input type="submit" value=" Submit " name="submit"/><br />
    </form>
</div>
</div>

<?php
if(isset($_POST["submit"])){

$hostname='localhost';
$username='adendud85_root';
$password='ayebruh';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=adendud85_aye",$username,$password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

    $sql = "INSERT INTO aye (username, email)
    VALUES ('".$_POST["username"]."','".$_POST["email"]."')";

    if ($dbh->query($sql)) {
        //echo "Success";
        header("Location: completed.php?username=".$_POST["username"]."");
    }
    else{
        echo "error";
    }

    $dbh = null;

}


catch(PDOException $e)
    {
        echo "error";

    }

}


?>
</body>
</html>

Anyone knows what to do or what I did wrong?

</div>
  • 写回答

1条回答 默认 最新

  • dsgd5756 2015-08-02 15:34
    关注

    Tr this, all your echo statements should be after header.

        <?php
    if(isset($_POST["submit"])){
    
    $hostname='localhost';
    $username='adendud85_root';
    $password='ayebruh';
    
    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=adendud85_aye",$username,$password);
    
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
    
        $sql = "INSERT INTO aye (username, email)
        VALUES ('".$_POST["username"]."','".$_POST["email"]."')";
    
        if ($dbh->query($sql)) 
        {
            header("Location: completed.php?username=".$_POST["username"]);
            //echo "Success";
        }
        else{
            echo "error";
        }
    
        $dbh = null;
    
    }
    
    
    catch(PDOException $e)
        {
            echo "error";
    
        }
    
    }
    
    
    ?>
    
    <html>
    <head>
    <title>insert data in database using PDO(php data object)</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
    
    <div id="main">
    <h1>Insert data into database using PDO</h1>
    <div id="login">
    <h2>Student's Form</h2>
    <hr/>
        <form action="" method="post">
            <label>Student Name :</label>
            <input type="text" name="username" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
            <label>Student Email :</label>
            <input type="email" name="email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
            <input type="submit" value=" Submit " name="submit"/><br />
        </form>
    </div>
    </div>
    
    
    </body>
    </html>
    

    Also, use prepared statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀