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>