Jump to content

No record shown when editing profile in webpage


genzedu777

Recommended Posts

Hi,

 

I need some help here! I am working out a webpage which will allow administrators to edit tutors' profile online.

 

From index.php, admin will click on the link to get into individual tutor's record, and it will direct them to viewprofile.php.

 

In viewprofile.php, admin can choose to edit the tutor's profile by hitting the Edit Profile button. (Which is in viewprofile.jpg attachment)

 

Here is the issue, when admin enters into editprofile.php, there is no record of the tutor, it seems like the records/details of the tutor is not brought forward into editprofile.php. (View editprofile.jpg for an example)

 

I suspect it may be due to my $_GET statement in editprofile.php? That may have caused me not being able to retrieve the records?

 

Appreciate any help. Thanks

 

 

viewprofile.php

<?php
  // Start the session
  require_once('inc/php/db_search/admin_startsession.php');

  // Insert doctype and header
  include 'inc/php/db_search/db_doctype.php'; 
  require_once('inc/php/db_search/header_admin.php');


  // Make sure the user is logged in before going any further.
  if (!isset($_SESSION['admin_id'])) {
    echo '<p class="login">Please <a href="admin_login.php">log in</a> to access this page.</p>';
    exit();
  }
?>


<?php
  require_once('inc/elements/connectvars.php');
?>


<?php
  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);


  $query = "SELECT name, nric, gender, address, hse_phone, hp FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";

  
  $data = mysqli_query($dbc, $query);

  if (mysqli_num_rows($data) == 1) {
    // The user row was found so display the user data
    $row = mysqli_fetch_array($data);
    echo '<table>';
    if (!empty($row['name'])) {
      echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';
    }
    if (!empty($row['nric'])) {
      echo '<tr><td class="label">NRIC:</td><td>' . $row['nric'] . '</td></tr>';
    }
    if (!empty($row['last_name'])) {
      echo '<tr><td class="label">Last name:</td><td>' . $row['last_name'] . '</td></tr>';
    }
    if (!empty($row['gender'])) {
      echo '<tr><td class="label">Gender:</td><td>';
      if ($row['gender'] == 'M') {
        echo 'Male';
      }
      if ($row['gender'] == 'F') {
        echo 'Female';
      }
      echo '</td></tr>';
    }
    echo '</table>';

    if (isset($_GET['tutor_id'])) {
      echo '<p>Would you like to <a href="editprofile.php">edit your profile</a>?</p>';
    }
  } // End of check for a single row of user results
  else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }

  mysqli_close($dbc);
?>

<?php
  // Insert the page footer
  require_once('inc/elements/footer.php');
?>

 

 

 

 

editprofile.php

<?php
  // Start the session
  require_once('inc/php/db_search/admin_startsession.php');

  // Insert doctype and header
  include 'inc/php/db_search/db_doctype.php'; 
  require_once('inc/php/db_search/header_admin.php');


  // Make sure the admin is logged in before going any further.
  if (!isset($_SESSION['admin_id'])) {
    echo '<p class="login">Please <a href="admin_login.php">log in</a> to access this page.</p>';
    exit();
  }
?>


<?php
  require_once('inc/elements/connectvars.php');
?>


<?php
  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (isset($_POST['submit'])) {
    // Grab the profile data from the POST
    $name = mysqli_real_escape_string($dbc, trim($_POST['name']));
    $nric = mysqli_real_escape_string($dbc, trim($_POST['nric']));
    $gender = mysqli_real_escape_string($dbc, trim($_POST['gender']));
    $error = false;


    // Update the profile data in the database
    if (!$error) {
      if (!empty($name) && !empty($nric) && !empty($gender)) {
        
	$query = "UPDATE tutor_profile SET name = '$name', nric = '$nric', gender = '$gender' WHERE tutor_id = '" . $_GET['tutor_id'] . "'";

	mysqli_query($dbc, $query);

        // Confirm success with the user
        echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php">view your profile</a>?</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
      }
    }
  } // End of check for form submission
  else {
    // Grab the profile data from the database
    $query = "SELECT name, nric, gender FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
    
$data = mysqli_query($dbc, $query);

// The user row was found so display the user data
if (mysqli_num_rows($data) == 1) {
	$row = mysqli_fetch_array($data);

    if ($row != NULL) {
      $name = $row['name'];
      $nric = $row['nric'];
      $gender = $row['gender'];
    }
    else {
      echo '<p class="error">There was a problem accessing your profile.</p>';
    }
  }
  }

  mysqli_close($dbc);
?>

  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
    <fieldset>
      <legend>Personal Information</legend>
      <label for="firstname">First name:</label>
      <input type="text" id="firstname" name="firstname" value="<?php if (!empty($name)) echo $name; ?>" /><br />
      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($nric)) echo $nric; ?>" /><br />
      <label for="gender">Gender:</label>
      <select id="gender" name="gender">
        <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option>
        <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option>
      </select><br />
    </fieldset>
    <input type="submit" value="Save Profile" name="submit" />
  </form>
  
  
<?php
  // Insert the page footer
  require_once('inc/elements/footer.php');
?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

      <input type="text" id="firstname" name="firstname" value="<?php if (!empty($name)) echo $name; ?>" /><br />
      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($nric)) echo $nric; ?>" /><br />
      <label for="gender">Gender:</label>
      <select id="gender" name="gender">
        <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option>
        <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option>

 

i think curley brackets are needed.

 

     

<input type="text" id="firstname" name="firstname" value="<?php if (!empty($name)){ echo $name; } ?>" /><br />
      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($nric)){  echo $nric; } ?>" /><br />
      <label for="gender">Gender:</label>
      <select id="gender" name="gender">
        <option value="M" <?php if (!empty($gender) && $gender == 'M'){  echo 'selected = "selected"'; } ?>>Male</option>
        <option value="F" <?php if (!empty($gender) && $gender == 'F'){  echo 'selected = "selected"'; } ?>>Female</option>

Link to comment
Share on other sites

Unless I've completely misunderstood what you're trying to do here: it appears you have nothing in that code that would actually pass the ID from viewprofile.php to editprofile.php. The link in viewprofile.php has no query string appended to it. (?tutor_id=some_value)

Link to comment
Share on other sites

Also, just to check they are not for whatever reason actually empty, this code insertion just before the form will tell you...

 

echo '<br />Values: '.$nric.' '.$name.' '.$gender;
?>

  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Link to comment
Share on other sites

Hi Pikachu2000,

 

it appears you have nothing in that code that would actually pass the ID from viewprofile.php to editprofile.php. The link in viewprofile.php has no query string appended to it. (?tutor_id=some_value)

 

I have followed your advice, added the (?tutor_id=some_value), but it turned out that ?tutor_id = Blank (Example: http://www.123.com/editprofile.php?tutor_id= )

 

I believe there is something amiss in my code…I have added a code in red for the link.

'<p>Would you like to <a href="editprofile.php?tutor_id=' . $row['tutor_id'] . '">edit your profile</a>?</p>';

 

<?php
  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);


  $query = "SELECT name, nric, gender, address, hse_phone, hp FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";

  
  $data = mysqli_query($dbc, $query);

  if (mysqli_num_rows($data) == 1) {
    // The user row was found so display the user data
    $row = mysqli_fetch_array($data);
    echo '<table>';
    if (!empty($row['name'])) {
      echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';
    }
    if (!empty($row['nric'])) {
      echo '<tr><td class="label">NRIC:</td><td>' . $row['nric'] . '</td></tr>';
    }
    if (!empty($row['last_name'])) {
      echo '<tr><td class="label">Last name:</td><td>' . $row['last_name'] . '</td></tr>';
    }
    if (!empty($row['gender'])) {
      echo '<tr><td class="label">Gender:</td><td>';
      if ($row['gender'] == 'M') {
        echo 'Male';
      }
      if ($row['gender'] == 'F') {
        echo 'Female';
      }
      echo '</td></tr>';
    }
    echo '</table>';


    [color=red]echo '<p>Would you like to <a href="editprofile.php?tutor_id=' . $row['tutor_id'] . '">edit your profile</a>?</p>';[/color]
    

  } // End of check for a single row of user results
  else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }

  mysqli_close($dbc);
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.