Help in update

Please help and thanks in advance!
my table is called makes, with coloums -->makeCode int(5) auto increment & Primary Key, make char(32), make_status(6).

I can insert and delete data as required. , however header(“Location:index.php”); in if(isset($_REQUEST[‘delete’])) {… does not take me to index.php

Main Issue is with editing , data is not storing.
in error log I am getting PHP Warning: Cannot modify header information - headers already sent…
on account of header('Location: ’ . $_SERVER[‘PHP_SELF’]);

<?php
setlocale(LC_MONETARY, 'en_IN');
date_default_timezone_set('Asia/Kolkata');
include "config.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<?php include "head.php"; ?>
<?php include "left.php"; ?> 
    <div class="parent">
    <header class="section header">
MAKES
    </header>
<main class="section main">
<table class='tg'><tr>
    <td class='tg-h2xt80'>Make Code</td>
    <td class='tg-h2xt90'>Make</td>
    <td class='tg-h2xt90'>Action</td>
    </td></tr>
<?php
$sql = "SELECT * FROM makes WHERE make_status is NULL ORDER BY makeCode";
$result=mysqli_query($conn, $sql);
while ($row= mysqli_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td class='tg-n7df'>" .$row['makeCode']. "</td>";
    echo "<td class='tg-0pky'>" .$row['make']. "</td>";
    echo "<td class='tg-dvpl'><form class='new' action= ''  method='POST'>
<input type='hidden'  name='id_delete'  value=" .$row['makeCode']. ">
<input type='submit'  class='cancelbtn1' name='delete' value='DEL''>
<input type='hidden'  name='id_edit'  value=" .$row['makeCode']. ">
<input type='submit'  class='blue-thin' name='edit' value='EDIT''>
</form></td></tr>";
}
?>
</table></main>
<aside class="section rightSidebar">
<table class="tg">
<?php
if (isset($_REQUEST['edit'])) {
    $id=$_REQUEST['id_edit'];
    echo "Editing....." .$id;
    ?>
<table class='tg'><tr>
    <td class='tg-h2xt80'>Make Code</td>
    <td class='tg-h2xt90'>Make</td>
    </td></tr>
    <?php
    $query="SELECT * from makes WHERE makeCode='$id'";  
    $result = mysqli_query($conn, $query) or die ('Unable to execute query quotea.php'.mysqli_error($conn));
    while ($data=mysqli_fetch_assoc($result)) {
echo "<form name='update action= ' ' method='POST'>";
echo "<td class='tg-n7df'>" .$data['makeCode'];
echo "<td class='tg-0pky'><input name='make' value=".$data['make'].">";
echo "</table>";
echo "<input class='blue'  type='submit' name='update' value='Update' />";
echo "<input class='red' type='reset'  name='Reset' value='Reset'/>";

}
if(isset($_REQUEST['update'])) {
    $id=$_REQUEST['id_edit'];
    echo "<td class='tg-n7df'>".$id;
    $make=$_REQUEST['make'];
    $query = "UPDATE makes SET  make='$make' WHERE makeCode=$id";
    echo "Updated......." .$make .$id;
    mysqli_query($conn,$query) or die ('Unable to execute makes.php'. mysqli_error($conn));
    //header('Location: ' . $_SERVER['PHP_SELF']);
    //header("Location:makes.php");  
echo "</form>"; 
}
}
if (isset($_REQUEST['delete'])) {
    $id=$_REQUEST['id_delete'];
    $query = "UPDATE makes SET  make_status='DEL' WHERE makeCode='$id'";
        mysqli_query($conn,$query) or die ('Unable to execute makes.php'. mysqli_error($conn));
        
        header("Location:index.php");
         exit();
}


?>
</aside>
<footer class="section footer ">
<form name="user" action=""  method="POST"> 
<label>Make</label><input type="text" name="make"  />

<input class='blue'  type=submit name="save" value="Save" />
<input class='red' type=reset  name="Reset" value="Reset"/>
</form> 
</footer>
<?php
if(isset($_REQUEST['save'])) {
$make=$_REQUEST['make'];

$query = "INSERT INTO makes (make)  VALUES ('$make')";

mysqli_query($conn,$query)or die ('Unable to execute user_insert.php'.mysqli_error($conn));
header("Location:makes.php"); 
}

?>
</div>
</div>
</body>
</html>

Post method form processing code must be located above the start of the html document so that nothing is being output to the browser before the header() statements.

Add this to the top of your PHP page.
ob_start();

@katsry, please don’t suggest adding output buffering to hide problems in code.

The OP has several problems, only one of which is a header() redirect that isn’t working in a part of the code that is actually running. By rearranging the code as has been suggested, which will simplify it, the OP may be able to see the mistakes in the html markup and two logic problems that are preventing the update code from working. Simpler is always better and hiding problems doesn’t make them go away.

@phdr if this was my project, I would edit this code to make it work perfectly without any “headers already sent” error.
Remember, they’re a lot of variables that can cause that error no matter your solution. Believe me, I have tried that.
The situation here is a solution. That provides a solution.

“As long as it works” is not a solution. It is just a hack to hide the real problems.

this is what got it working

Sponsor our Newsletter | Privacy Policy | Terms of Service