Screenshot_2021-12-13_112253.png

<?php 
// This page is for editing a user record.
// This page is accessed through view_users.php.

$page_title = 'Edit a User';
include ('includes/header.html');
echo '<h1>Edit a User</h1>';

// Check for a valid user ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_users.php
    $id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
    $id = $_POST['id'];
} else { // No valid ID, kill the script.
    echo '<p class="error">This page has been accessed in error.</p>';
    include ('includes/footer.html'); 
    exit();
}

require ('../mysqli_connect.php'); 

// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $errors = array(); // Collect error message

    // Check for a first name:
    if (empty($_POST['first_name'])) {
        $errors[] = 'You forgot to enter your first name.';
    } else {
        $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
    }

    // Check for a last name:
    if (empty($_POST['last_name'])) {
        $errors[] = 'You forgot to enter your last name.';
    } else {
        $ln =mysqli_real_escape_string($dbc, trim($_POST['last_name']));
    }

    // Check for an username:
    if (empty($_POST['username'])) {
        $errors[] = 'You forgot to enter your username.';
    } else {
        $us = mysqli_real_escape_string($dbc,trim($_POST['username']));
    }

    // Check for a type of pets:
    if (empty($_POST['pet_type'])) {
        $errors[] = 'You forgot to enter your type of pets.';
    } else {
        $pt =mysqli_real_escape_string($dbc, trim($_POST['pet_type']));
    }

    // Check for a pet partnership/programs:
    if (empty($_POST['pet_partnership_pro'])) {
        $errors[] = 'You forgot to enter your pet partnership/programs.';
    } else {
        $ppp =mysqli_real_escape_string($dbc, trim($_POST['pet_partnership_pro']));
    }


    if (empty($errors)) { // If everything's OK.

            // Make the query:
            $q = "UPDATE members SET first_name='$fn', last_name='$ln', username='$us' WHERE member_id=$id LIMIT 1;UPDATE pet_info SET pet_type'$pt', pet_sponsorship_pro='$ppp' WHERE pet_id=$id LIMIT 1";
            $r = @mysqli_multi_query ($dbc, $q);
            if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

                // Print a message:
                echo '<p>The user has been edited.</p>';    

            } else { // If it did not run OK.
                echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
                echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
            }

    } else { // Report the errors.

        echo '<p class="error">The following error(s) occurred:<br />';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br />\n";
        }
        echo '</p><p>Please try again.</p>';

    } // End of if (empty($errors)) IF.

} // End of submit conditional.

// Always show the form...

// Retrieve the user's information:
$q = "SELECT first_name, last_name, username FROM members WHERE member_id=$id; SELECT pet_type, pet_sponsorship_pro FROM pet_info WHERE pet_id=$id";    
$r = @mysqli_multi_query ($dbc, $q);

if (mysqli_num_rows($r) == 0) { // Valid user ID, show the form.

    // Get the user's information:
    $row= mysqli_fetch_array ($r, MYSQLI_NUM);
    // Create the form:
    echo '<form action="edit_user.php" method="post">
    <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p>
    <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p>
    <p>Username: <input type="text" name="username" size="20" maxlength="60" value="' . $row[2] . '"  /> </p>
    <p>Type of Pets:' . $row[3] . '  
    <select name="pet_type" id="pet_type">
    <option value=""></option>
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="rabbit">Rabbit</option>
    <option value="fish">Fish</option>
    </select>
    <p>Pet Sponsorship/Programs:' . $row[4] . '  <br>
    <input type="radio" id="shelter_program" name="pet_sponsorship_pro" value="Shelter Program">
    <label for="shelter_program">Shelter Program</label><br>

    <input type="radio" id="breeder_program" name="pet_sponsorship_pro" value="Breeder Program">
    <label for="breeder_program">Breeder Program</label><br>

    <input type="radio" id="pet_care_academy" name="pet_sponsorship_pro" value="Pet Care Academy">
    <label for="pet_care_academy">Pet Care Academy</label><br>

    <input type="radio" id="animal_welfare" name="pet_sponsorship_pro" value="Animal Welfare">
    <label for="animal_welfare">Animal Welfare</label><br>

    <input type="radio" id="pet_physiotheraphy " name="pet_sponsorship_pro" value="Pet Physiotheraphy">
    <label for="pet_physiotheraphy ">Pet Physiotheraphy </label><br>

    <p><input type="submit" name="submit" value="Submit" /></p>
    <input type="hidden" name="id" value="' . $id . '" />
    </form>';

} else { // Not a valid user ID.
    echo '<p class="error">This page has been accessed in error.</p>';
}

mysqli_close($dbc);

include ('includes/footer.html');
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.