Reputation: 7
Hello I'm working with PHP.At this moment i have just inserted data in database by php code.Just name and email + submit. But i want when i will click on submit then it will back to homepage.As far i know i did true code. Please check ->
$conn = new mysqli('localhost','root','','ajx');
$name = $_POST['name'];
$email = $_POST['email'];
if(empty($name) || empty($email)){
echo "<h3 style ='color:red; text-align:center;'>Filed must not be empty</h3>";
}else{
$sql = "INSERT INTO user(name,email) VALUES('$name','$email')";
$conn-> query($sql);
echo "Data insert Okay";
header("location : index.php");
}
The front end shows this message:
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
If you think this is a server error, please contact the webmaster.
Error 500
localhost Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.4
Upvotes: 1
Views: 137
Reputation: 2201
Your redirection is not working, because you have already sent headers. Remove the echo before the header() and it should work. If you want to send any headers there MUST NOT be any output before you do so. Not even a single space.
Also, as I said in my comment, please dont use mysqli the way you do, look into prepared statements.
Upvotes: 1