I have a simple form to insert a user to my database, but I want the form to redirect to my homepage once the query is completed. I have tried several ways an either I get an error, the query is not run or it does not redirect. Here is the page for the form as my last trial
<?php
require("db.php");
?>
<?php include 'query.php';?>
<html>
<head>
<title>Insert a Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['insert']))
{
$user_added = $_POST['user_added'];
$insert_query = "INSERT INTO userList ";
$insert_query .= "(Book, User_0, User_1) ";
$insert_query .= "VALUES ";
$insert_query .= "('$user_added', '0', '1')";
$retval = mysqli_query($connection, $insert_query);
if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
echo "Updated data successfully\n";
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Book to Add</td>
<td><input name="user_added" type="text" id="user_added"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="insert" type="submit" id="insert" value="Insert" onclick="window.location='home.php';">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
mysqli_close($connection);
?>
When I tried it this way, it shows the Updated data successfully
text but it does not redirect. I tried to make the following approach
if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
else
{
header("Location: test.php");
echo "Updated data successfully\n";
}
but I get the following error
Warning: Cannot modify header information - headers already sent by (output started at /home/myusername/public_html/home.php:11) in /home/myusername/public_html/home.php on line 30
Updated data successfully
So the database is updated but the redirect does not work. Of course when I tried the second approach I removed the onclick="window.location='home.php';"
from the form. Any suggestions?