Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP Not Updating, refresh to selfpage and showing no error

Hi I’m here again to ask help regarding my update in php, when i click update it only refresh itself and not updating the value in the database. here is my code. I’ve also tried removing my triggers and see if thats the problem but no still not updating. please help me

<?php
// Include config file
require_once "config/connect.php";
 
// Define variables and initialize with empty values
$doc_name = $control_num = $rev_num = $effective_date = $description = $person_responsible = $ret_period = $method_disposal = $folder_destination = "";
$doc_name_err = $control_num_err = $rev_num_err = $effective_date_err = $description_err = $person_responsible_err = $ret_period_err = $method_disposal_err = $folder_destination_err = "";
 
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
    // Get hidden input value
    $id = $_POST["id"];
    
    // Validate doc_name
    $input_doc_name = trim($_POST["doc_name"]);
    if(empty($input_doc_name)){
        $doc_name_err = "Please enter a document name.";
        error_log("Validation error: $doc_name_err");
    } else {
        $doc_name = $input_doc_name;
    }
    
    // Validate control_num
    $input_control_num = trim($_POST["control_num"]);
    if(empty($input_control_num)){
        $control_num_err = "Please enter a control number.";
    } else {
        $control_num = $input_control_num;
    }
    
    // Validate rev_num
    $input_rev_num = trim($_POST["rev_num"]);
    if(empty($input_rev_num)){
        $rev_num_err = "Please enter the revision number.";
    } elseif(!ctype_digit($input_rev_num)){
        $rev_num_err = "Please enter a positive integer value.";
    } else {
        $rev_num = $input_rev_num;
    }

    // Validate effective_date
    $input_effective_date = trim($_POST["effective_date"]);
    if(empty($input_effective_date)){
        $effective_date_err = "Please select a valid effective date.";
    } else {
        $effective_date = $input_effective_date;
    }

    // Validate description
    $input_description = trim($_POST["description"]);
    if(empty($input_description)){
        $description_err = "Please enter a valid description";
    } else {
        $description = $input_description;
    }

    // Validate person_responsible
    $input_person_responsible = trim($_POST["person_responsible"]);
    if(empty($input_person_responsible)){
        $person_responsible_err = "Please enter a valid person responsible";
    } else {
        $person_responsible = $input_person_responsible;
    }

    // Validate ret_period
    $input_ret_period = trim($_POST["ret_period"]);
    if(empty($input_ret_period)){
        $ret_period_err = "Please enter a valid retention period";
    } else {
        $ret_period = $input_ret_period;
    }

    // Validate method_disposal
    $input_method_disposal = trim($_POST["method_disposal"]);
    if(empty($input_method_disposal)){
        $method_disposal_err = "Please select a valid method of disposal";
    } else {
        $method_disposal = $input_method_disposal;
    }

    // Validate folder_destination
    $input_folder_destination = trim($_POST["folder_destination"]);
    if(empty($input_folder_destination)){
        $folder_destination_err = "Please select a valid folder destination";
    } else {
        $folder_destination = $input_folder_destination;
    }
    
    // Check input errors before inserting in the database
    if(empty($doc_name_err) && empty($control_num_err) && empty($rev_num_err) && empty($effective_date_err) && empty($description_err) 
    && empty($person_responsible_err) && empty($ret_period_err) && empty($method_disposal_err) && empty($folder_destination_err)){
        // Prepare an update statement
        $sql = "UPDATE files SET doc_name=:doc_name, control_num=:control_num, rev_num=:rev_num, effective_date=:effective_date, description=:description, 
        person_responsible=:person_responsible, ret_period=:ret_period, method_disposal=:method_disposal, folder_destination=:folder_destination WHERE id=:id";
 
        if($stmt = $pdo->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bindParam(":doc_name", $param_doc_name);
            $stmt->bindParam(":control_num", $param_control_num);
            $stmt->bindParam(":rev_num", $param_rev_num);
            $stmt->bindParam(":effective_date", $param_effective_date);
            $stmt->bindParam(":description", $param_description);
            $stmt->bindParam(":person_responsible", $param_person_responsible);
            $stmt->bindParam(":ret_period", $param_ret_period);
            $stmt->bindParam(":method_disposal", $param_method_disposal);
            $stmt->bindParam(":folder_destination", $param_folder_destination);
            $stmt->bindParam(":id", $param_id);
            
            // Set parameters
            $param_doc_name = $doc_name;
            $param_control_num = $control_num;
            $param_rev_num = $rev_num;
            $param_effective_date = $effective_date;
            $param_description = $description;
            $param_person_responsible = $person_responsible;
            $param_ret_period = $ret_period;
            $param_method_disposal = $method_disposal;
            $param_folder_destination = $folder_destination;
            $param_id = $id;

            // Attempt to execute the prepared statement
            if($stmt->execute()){
                // Records updated successfully. Redirect to the landing page
                header("location: manage-files.php");
                exit();
            } else {
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
         
        // Close the statement
        unset($stmt);
    }
    
    // Close the connection
    unset($pdo);
} else {
    // Check the existence of the id parameter before processing further
    if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
        // Get the URL parameter
        $id = trim($_GET["id"]);
        
        // Prepare a select statement
        $sql = "SELECT * FROM files WHERE id = :id";
        if($stmt = $pdo->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bindParam(":id", $param_id);
            
            // Set parameters
            $param_id = $id;
            
            // Attempt to execute the prepared statement
            if($stmt->execute()){
                if($stmt->rowCount() == 1){
                    /* Fetch the result row as an associative array. Since the result set
                    contains only one row, we don't need to use a while loop */
                    $row = $stmt->fetch(PDO::FETCH_ASSOC);
                
                    // Retrieve individual field values
                    $doc_name = $row["doc_name"];
                    $control_num = $row["control_num"];
                    $rev_num = $row["rev_num"];
                    $effective_date = $row["effective_date"];
                    $description = $row["description"];
                    $person_responsible = $row["person_responsible"];
                    $ret_period = $row["ret_period"];
                    $method_disposal = $row["method_disposal"];
                    $folder_destination = $row["folder_destination"];
                } else {
                    // URL doesn't contain a valid id. Redirect to the error page
                    header("location: error.php");
                    exit();
                }
                
            } else {
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close the statement
        unset($stmt);
        
        // Close the connection
        unset($pdo);
    }  else {
        // URL doesn't contain an id parameter. Redirect to the error page
        header("location: error.php");
        exit();
    }
}
?>

and here is the form good maam and sirs

<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post" enctype="multipart/form-data">
                <input type="text" value="<?php echo $id; ?>">
                    <div class="input-group mb-3">
                    <input type="text" class="form-control <?php echo (!empty($doc_name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $doc_name; ?>" name="doc_name" placeholder="File Name" required>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-file"></span>
                        </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $doc_name_err;?></span>
                    </div>
                    <div class="input-group mb-3">
                    <input type="text" class="form-control <?php echo (!empty($control_num_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $control_num; ?>" name="control_num" placeholder="Form Number" required>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-qrcode"></span>
                        </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $control_num_err;?></span>
                    </div>
                    <div class="input-group mb-3">
                    <input type="number" class="form-control<?php echo (!empty($rev_num_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $rev_num; ?>" name="rev_num" placeholder="Revision Number" required>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-clone"></span>
                        </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $rev_num_err;?></span>
                    </div>
                          <!-- Date -->
                    <div class="form-group">
                      <label for="">Effectivity Date:</label>
                    <div class="input-group mb-3">
                    <input type="date" class="form-control<?php echo (!empty($effective_date_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $effective_date; ?>" name="effective_date"  required>
                    <div class="input-group-append">
                    </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $effective_date_err;?></span>
                    </div>
                    <div class="input-group mb-3">
                    <textarea class="form-control<?php echo (!empty($description_err)) ? 'is-invalid' : ''; ?>" name="description" id="description" cols="30" rows="5" placeholder="Description" required><?php echo $description; ?></textarea>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-paragraph"></span>
                        </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $description_err;?></span>
                    </div>
                    <div class="input-group mb-3">
                    <input type="text" class="form-control<?php echo (!empty($person_responsible_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $person_responsible; ?>" name="person_responsible" placeholder="Person Responsible" required>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-user"></span>
                        </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $person_responsible_err;?></span>
                    </div>
                    <div class="form-group">
                      <label for="">Retention Period:</label>
                    <div class="input-group mb-3">
                    <select name="ret_period" id="ddlModels" class="form-control<?php echo (!empty($ret_period_err)) ? ' is-invalid' : ''; ?>" onChange="checkOption(this)">
                        <option value="Revised" <?php if ($ret_period == 'Revised') echo 'selected'; ?>>Revised</option>
                        <option value="3 Years" <?php if ($ret_period == '3 Years') echo 'selected'; ?>>3 Years</option>
                        <option value="5 Years" <?php if ($ret_period == '5 Years') echo 'selected'; ?>>5 Years</option>
                        <option value="10 Years" <?php if ($ret_period == '10 Years') echo 'selected'; ?>>10 Years</option>
                        <option value="Others">Others<input type="text" id="txtOther" disabled="disabled" name="ret_period"></option>
                      </select>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-user"></span>
                        </div>
                    </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $ret_period_err;?></span>
                    </div>

                    <div class="form-group">
                      <label for="">Method of Disposal:</label>
                    <div class="input-group mb-3">
                      <select name="method_disposal" id="" class="form-control<?php echo (!empty($method_disposal_err)) ? ' is-invalid' : ''; ?>">
                      <option value="Shredding" <?php if ($method_disposal == 'Shredding') echo 'selected'; ?>>Shredding</option>
                      <option value="Recycling" <?php if ($method_disposal == 'Recycling') echo 'selected'; ?>>Recycling</option>
                      </select>
                    <!-- <input type="text" class="form-control" name="doc_name" placeholder="Retention Period" required> -->
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-user"></span>
                        </div>
                    </div>
                    </div>
                    <span class="invalid-feedback"><?php echo $method_disposal_err;?></span>
                    </div>
                    
                    <?php include "config/fetch_conn.php"; ?>
                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
                    <div class="input-group mb-3">
                   <select onChange="getdistrict(this.value);"  name="sector" id="state"  class="form-control" required>
                        <!--- Fetching States--->
                        <option value="">Select Sector</option>
                        <?php
                        $sql="SELECT * FROM academic";
                        $stmt=$dbh->query($sql);
                        $stmt->setFetchMode(PDO::FETCH_ASSOC);
                        while($row =$stmt->fetch()) { 
                        ?>
                        
                        <option value="<?php echo $row['dept_name'];?>"><?php echo $row['dept_name'];?></option>
                        <?php }?>
                   </select>
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-school"></span>
                        </div>
                    </div>
                    </div>
                    <div class="input-group mb-3">
                    <select name="folder_destination" id="district-list" class="form-control">
                    <option value="">Select Folder</option>
                    </select>    
                    <div class="input-group-append">
                        <div class="input-group-text">
                        <span class="fas fa-folder"></span>
                        </div>
                    </div> 
                    </div>
                    <br>
                    <!-- <label for="files">Upload PDF File</label>
                    <input type="file" name="file_name" required>
                    <br>
                    <label for="files">Upload IMG File</label>
                    <input type="file" name="file_name" required> -->
                    <div class="row">
                    <div class="col-8">
                    </div>
                    <!-- /.col -->
                    <div class="col-4">
                        <button type="submit" name="submit" class="btn btn-primary btn-block">Update</button>
                    </div>
                    <!-- /.col -->
                    </div>
                </div>
            </form>

I hope someone could help me, i’m going crazy.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried alot of different things, still no good. I’m searching for 4 hours now still nothing, i’ve check the code and seems good to me. its just a bummer no error is showing to give me hint on what or where to fix this issue

>Solution :

The problem with your code is this line:

<input type="text" value="<?php echo $id; ?>">

You have to write it like this to include the id field in your post request:

<input type="text" name="id" value="<?php echo $id; ?>">

Without name field, the PHP can’t know that, there is any ID field. Hope it
will resolve your issue.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading