Code lab · PHP + MySQL

PHP Profile Update

Edit name, email, and avatar path in database.

Learning guide

Practising PHP and MySQL? Edit name, email, and avatar path in database. We focus on PHP Profile Update here.

Configuration belongs in config.php (PHP) or a single app.js entry (browser labs)—not sprinkled through every page.

Read a section, predict what should happen, then run it. Logs and the browser network tab are part of the lesson.

If local setup is new to you, block an hour once for Apache/MySQL or Live Server, then reuse that stack for other guides.

Start from index.php when you open the reference tree. The optional package at the bottom matches this article.

Concepts you should understand

Authentication labs cover identity, sessions, and password hashing. Upload labs cover validation, storage layout, and metadata. AJAX labs cover HTTP verbs, JSON contracts, and separating view from API. Map each file in the reference section to one of these ideas.

Optional local lab setup

PHP labs assume a local MySQL schema imported from the teaching database.sql file. These steps describe a learning environment, not commercial software installation.

  1. Create a folder under your local web root named php-profile-update for this exercise.
  2. Start Apache and MySQL services on your machine (only needed for PHP/MySQL lessons).
  3. Import the bundled database.sql through phpMyAdmin to create practice tables.
  4. Adjust config.php credentials to match your local database user.
  5. Open http://localhost/php-profile-update/index.php and step through each screen while reading the source.
  6. Credentials for sandbox testing, if any, are documented only inside the private README.txt shipped with the practice archive—not on this public page.

Guided walkthrough

Begin by using the feature as a learner: submit empty forms, invalid emails, and edge cases. Watch how the UI responds, then locate the PHP or JavaScript responsible for that message. Trace variables from $_POST or fetch bodies down to SQL or DOM updates.

Rewrite one branch in your own words—rename fields, add comments, or log values with error_log or console.debug. If behavior changes unexpectedly, you have found a learning moment about sessions, scope, or async timing.

For topics like PHP Profile Update, compare your work against the annotated snippets below before peeking at the full reference tree. Spaced repetition (revisit the lesson days later) cements syntax into long-term memory better than a single rushed pass.

Annotated code samples

Each block is meant to be edited, not pasted blindly. Read the note under it, then try one small change.

Shared bootstrap for PHP Profile Update

<?php
require __DIR__ . '/config.php';
// Use db() for mysqli connection

Central configuration avoids repeating host, user, and database name in every script. A helper like db() returns a single connection per request, which is easier to test and secure. Keep credentials out of version control and use different values on staging versus production.

Upload handlers are a frequent target because they touch the filesystem. Validate type and size on the server, store files outside the web root when possible, and never execute uploaded content. These steps mirror what security auditors expect in real products, not just classroom demos.

Prepared statement pattern

<?php
require __DIR__ . '/config.php';
$stmt = db()->prepare('SELECT id, name FROM users WHERE email = ? LIMIT 1');
$stmt->bind_param('s', $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();

require loads shared database settings so every script uses one connection helper. The prepared statement uses a ? placeholder so user input is never concatenated into SQL. bind_param attaches the email string safely, and fetch_assoc returns one row as an associative array for login checks.

Prepared statements separate SQL structure from user data, which closes the most common SQL injection holes taught in security courses. Even when you trust your own form today, future features (search boxes, admin filters) often reuse the same query patterns—building them safely from day one prevents expensive refactors.

Redirect after a successful action

<?php
header('Location: index.php');
exit;

Post/Redirect/Get prevents duplicate form submissions when users refresh. Always call exit after header('Location: …') so no extra output is sent. Use relative paths when possible to survive domain changes.

Security notes for students

Practice code deliberately simplifies reality. Before any production deployment, add HTTPS, rate limiting, logging, and professional review. Never reuse tutorial passwords in public systems. Remove sample accounts and disable verbose errors on live hosts.

Legal: Globaltuts.com is not responsible for security vulnerabilities if practice code is deployed on a live production server without proper sanitization. Read our practice project policy, Privacy Policy, and Disclaimer.

Reference materials for this lesson

Optional practice files to follow along after reading the guide above. Not required to understand the concepts.

  • README.txt
  • assets/style.css
  • config.php
  • dashboard.php
  • database.sql
  • index.php
  • login.php
  • logout.php

Archive folder name: php-profile-update/

Get practice archive (.zip)