1

I have an employee table but when submitted the form data are updated in the employee table of SQL database but the employee data page (header('location:employeedata.php')) is not displayed. And when I remove sometimes field it work perfectly displaying data on the employee data page. Here below is my PHP code. somebody, please help me

<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-state=1"/>
<link type="text/css" rel="stylesheet" href="css/bootstrap.css"/>
<script src ="js/jquery-3.4.1.min.js" ></script>
<script src ="js/bootstrap.js" ></script>
<link rel="stylesheet" href="stylemenu.css">
<style type="text/css">
body
{
    background-image:url(water.png);
    background-size:cover;
}
</style>
</head>
<body>
<?php
session_start();
include("samadonconnection.php");
error_reporting(0);
?>
<div class ="container">    
<div class="row">
<div class="col-sm-6"> <h2> Test </h2> </div>
<div class="col-sm-6"> 
<?php echo "<div style=\"float:right\"> Welcome ". $_SESSION['user']['username']."! </div>"; ?> </div>
</div>
 <div class="topnav">
<div class="col-sm-12">
<a href="home.php">HOME</a>
<a href="create_user.php"> + ADD USER</a>
<a class="active" href="employee.php" style="color:blue;">EMPLOYEE</a>
<a href="employeedata.php">EMPLOYEE DATA</a>
<a href="home.php?logout='1'" style="color: red;">LOG OUT</a>
<a href="cont.php">CONTACT US</a>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div id="ui"> 
<form action="" method="GET" class="form-group">
<div class="row">
<div class ="col-lg-3">
<label> Today Date :</label>
<input type ="date" name ="date" class ="form-control"  placeholder=".....">
</div>
<div class ="col-lg-3">
<label> Birth Date :</label>
<input type ="date" name ="birth" class ="form-control"  placeholder="......">
</div>
<div class ="col-lg-3">
<label> Mobile :</label>
<input type ="text" name ="mobile" class ="form-control"  placeholder="......">
</div>
<div class ="col-lg-3">
<label> Father Name :</label>
<input type ="text" name ="father" class ="form-control"  placeholder=".....">
</div>
</div>
<br>
<div class="row">
<div class ="col-lg-3">
<label> First Name :</label>
<input type ="text" name ="fname" class ="form-control"  placeholder=".....">
</div>
<div class ="col-lg-3">
<label> Last Name :</label>
<input type ="text" name ="lname" class ="form-control"  placeholder="......">
</div>
<div class ="col-lg-3">
<label> Gender :</label>
<select name="gender" class="form-control"> 
<option> Choose ... </option>
<option> Male </option>
<option> Female </option>
</select>
</div>
<div class ="col-lg-3">
<label> Marital :</label>
<select name="marital" class="form-control"> 
<option> Choose ... </option>
<option> Married </option>
<option> Unmarried </option>
</select>
</div>
</div>
<br>
<div class="row">
<div class ="col-lg-3">
<label> Email :</label>
<input type ="text" name ="email" class ="form-control"  placeholder="......">
</div>
<div class ="col-lg-3">
<label> Qualification :</label>
<input type ="text" name ="qualification" class ="form-control"  placeholder=".....">
</div>
<div class ="col-lg-6">
<label> Address :</label>
<input type ="text" name ="address" class ="form-control"  placeholder="......">
</div>
</div>
<br>
<div class="row">
<div class ="col-lg-3">
<label> Pin Code :</label>
<input type ="text" name ="pin" class ="form-control"  placeholder="......">
</div>
<div class ="col-lg-3">
<label> District :</label>
<input type ="text" name ="district" class ="form-control"  placeholder=".....">
</div>
<div class ="col-lg-3">
<label> State :</label>
<input type ="text" name ="state" class ="form-control"  placeholder="......">
</div>
<div class ="col-lg-3">
<label> Country :</label>
<input type ="text" name ="country" class ="form-control"  placeholder="......">
</div>
</div>
<br>
<div class="row">
</div>
<br>
<input type ="submit" name ="submit" value ="SUBMIT" class ="btn btn-primary btn-block btn-lg">
</form>
<?php
if($_GET['submit'])
{
$da = $_GET['date'];
$bi = $_GET['birth'];
$mo = $_GET['mobile'];
$fa = $_GET['father'];
$fn = $_GET['fname'];
$ln = $_GET['lname'];
$ge = $_GET['gender'];
$ma = $_GET['marital'];
$em = $_GET['email'];
$qu = $_GET['qualification'];
$ad = $_GET['address'];
$pi = $_GET['pin'];
$di = $_GET['district'];
$st = $_GET['state'];
$co = $_GET['country'];
if( $fn!="" && $ln!="") 
{
$query = "INSERT INTO employee VALUES 
( '$sl','$da','$bi','$mo','$fa','$fn','$ln','$ge','$ma','$em','$qu','$ad' ,'$pi','$di','$st','$co')";
$data = mysqli_query($conn,$query);
if($data)
{
header('location:employeedata.php');
}
}
else
{
echo "All fields are required";
}
}
?>
</div>
</div>
</div>
</div>  
</body>
</html>
Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
Uma
  • 11
  • 1
  • canyoupleaseindentyourcodesoitbecomesreadable – Cid Mar 04 '20 at 10:41
  • 1
    Change Method `GET` to `POST` and change `if($_GET['submit'])` to `if(isset($_POST['submit'])) {` .. – Simone Rossaini Mar 04 '20 at 10:43
  • @CD001 I don't think this is the issue. I'd rather think, as Simone stated, that there are too much datas to fit a GET – Cid Mar 04 '20 at 10:48
  • 2
    @Cid the OP says the data is inserted, but the redirect doesn't happen. I'd agree that POST is more appropriate when data is being updated / inserted, but if that parts is working OK, that suggests the data is within limits. I'm not sure what the comment about "removing sometime field" means. – droopsnoot Mar 04 '20 at 10:50
  • @droopsnoot good catch, I missed that part. Since OP stated that the redirect works when one removes some HTML, this is definitely the buffer that is big enough to be output to the client – Cid Mar 04 '20 at 10:52
  • The first variable in the query is `$sl`, although the OP says it updates the data correctly I can't see where this var comes from. – droopsnoot Mar 04 '20 at 10:54
  • if i delete the some fields say from
    . It work perfectly
    – Uma Mar 04 '20 at 11:05
  • i change to POST it didn't work and also remove $sl . – Uma Mar 04 '20 at 11:07

1 Answers1

0

You can't do a header redirect after you've sent output to the browser, you'll get a "headers already sent" error. Move your form processing code to before the html output.

droopsnoot
  • 931
  • 1
  • 7
  • 11
  • 1
    If this is the answer, the question's a duplicate ;) – CD001 Mar 04 '20 at 10:45
  • This answer isn't necessary true. The output can be "bufferized" before being sent and the redirection may work, even if there are some output before – Cid Mar 04 '20 at 10:46
  • Yes, good point. I looked for the buffering functions in the OPs code and didn't see them, but forgot that it can be set independently of code. – droopsnoot Mar 04 '20 at 10:47
  • @droopsnoot it can be set in php.ini if I recall correctly. I already had that *issue* when wanting to explain to a friend that one shouldn't send output before doing a `header(location:...)` and the redirection worked correctly :) – Cid Mar 04 '20 at 10:50