0

I am trying to update my db table. All the code is on same page. I have applied several conditions but none of them is working. When i click on submit button it refreshes the page and doesn't change table row. Any suggestions please?

Code

<?php
session_start();
include "db.php";
if(isset($_SESSION['sess_user'])) {
    $id = $_GET['id'];
    ?>

    <form name="form1" method="post" enctype="multipart/form-data">
        <?php
        $sql = "SELECT * FROM categories WHERE cat_id=" . $id;
        $result = mysqli_query($con, $sql);
        if (mysqli_num_rows($result) > 0) {
            while ($rows = mysqli_fetch_array($result)) {
                ?>

                <h4>Name:</h4>  <input type="text" name="cat_name" value=" <?= $rows['cat_name'] ?>">
                <input type="file" name="file" value="<?= $rows['cat_image']; ?>">

                <input name="id" type="hidden" id="id" value="<?= $rows['cat_id']; ?>">

                <?php
            }
        }
        ?>

        <input type="submit" name="Submit" value="Submit">


    </form>


    <?php
    if(isset($_POST['submit'])) {
        $name = $_POST['cat_name'];
        $image = $_FILES['file']['name'];
        $temp_name = $_FILES["file"]["tmp_name"];
        $id = $_POST['id'];
        $query1 = "select * from categories where cat_name='$name' OR cat_image='$image'";
        $search_pic = mysqli_query($con, $query1);
        $count = mysqli_num_rows($search_pic);
        if ($count == 0) {
            // update data in mysql database
            $sql = "UPDATE categories SET cat_name='$name', cat_image='$image' WHERE cat_id=$id";
            $result = mysqli_query($con, $sql);
            if ($result) {
                $m = move_uploaded_file($_FILES['file']['tmp_name'], 'images/' . $image);

                echo "<script>alert('Row updated succesfully! ')</script>";
                header('Location:category_admin.php');
            } else {
                echo "<script>alert('Row does not updated ')</script>";

            }
        } else {
            echo "<script>alert('Row  with the same name exists! ')</script>";
        }
    }
    }
 else
{
    echo "You must Login First goto : <a href='index.php'>Admin Login </a>";
}
?>
tabia
  • 631
  • 2
  • 10
  • 33

0 Answers0