⬅ Back to Projects

Contact Management — Source Code

db.php

<?php
$host = 'localhost';
$user = 'ahmeuesz_dino';
$pwd = 'X39IW2ho3^5h6d_5';
$dbname = 'ahmeuesz_myapp';
$conn = mysqli_connect($host, $user, $pwd, $dbname);
if (!$conn) {
    echo "Connection failed.";
}
?>

index.php

<?php
include 'db.php';
if (isset($_POST['submit'])) {
    $prefix = $_POST['prefix'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $image_name = $_FILES['image']['name'];
    $tmp_name = $_FILES['image']['tmp_name'];
    $target = "uploads/" . basename($image_name);
    move_uploaded_file($tmp_name, $target);
    $sql = "INSERT INTO user_record (prefix, fullname, email, phone, image)
            VALUES ('$prefix', '$fullname', '$email', '$phone', '$image_name')";
    mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="" method="post" enctype="multipart/form-data">
    <label>Prefix:</label>
    <select name="prefix" required>
        <option>Mr</option>
        <option>Mrs</option>
        <option>Dr</option>
        <option>Ms</option>
        <option>Prof</option>
    </select><br>
    <label>Full Name:</label>
    <input type="text" name="fullname" required><br>
    <label>Email:</label>
    <input type="email" name="email" required><br>
    <label>Phone Number:</label>
    <input type="text" name="phone" required><br>
    <label>Profile Picture:</label>
    <input type="file" name="image" required><br>
    <button type="submit" name="submit">Submit</button>
</form>
<hr>
<h2>Registered Users</h2>
<?php
$result = mysqli_query($conn, "SELECT * FROM user_record ORDER BY id DESC");
while ($row = mysqli_fetch_assoc($result)) {
    $img = "uploads/" . $row['image'];
    $email_link = "mailto:" . $row['email'];
    $call_link = "tel:" . $row['phone'];
    $whatsapp_link = "https://wa.me/" . $row['phone'];
    echo "
    <div>
        <img src='$img'>
        <div>
            <h3>{$row['prefix']} {$row['fullname']}</h3>
            <p><b>Email:</b> <a href='$email_link'>{$row['email']}</a></p>
            <p><b>Phone:</b> {$row['phone']}</p>
        </div>
        <div>
            <a href='$call_link'>📞 Call</a>
            <a href='$whatsapp_link' target='_blank'>💬 WhatsApp</a>
            <a href='edit.php?id={$row['id']}'>✏️ Edit</a>
            <a href='delete.php?id={$row['id']}'>🗑️ Delete</a>
        </div>
    </div>
    ";
}
?>
</body>
</html>

update.php

<?php
include 'db.php';
$id = $_GET['id'];
$result = mysqli_query($conn, "SELECT * FROM user_record WHERE id=$id");
$row = mysqli_fetch_assoc($result);
if (isset($_POST['update'])) {
    $prefix = $_POST['prefix'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    if (!empty($_FILES['image']['name'])) {
        $image_name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        move_uploaded_file($tmp_name, "uploads/" . $image_name);
        $update = "UPDATE user_record SET prefix='$prefix', fullname='$fullname', email='$email', phone='$phone', image='$image_name' WHERE id=$id";
    } else {
        $update = "UPDATE user_record SET prefix='$prefix', fullname='$fullname', email='$email', phone='$phone' WHERE id=$id";
    }
    mysqli_query($conn, $update);
    header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Edit Record</title>
</head>
<body>
<h2>Edit User</h2>
<form method="post" enctype="multipart/form-data">
    <label>Prefix:</label>
    <select name="prefix">
        <option <?= $row['prefix'] == 'Mr' ? 'selected' : '' ?>>Mr</option>
        <option <?= $row['prefix'] == 'Mrs' ? 'selected' : '' ?>>Mrs</option>
        <option <?= $row['prefix'] == 'Dr' ? 'selected' : '' ?>>Dr</option>
        <option <?= $row['prefix'] == 'Ms' ? 'selected' : '' ?>>Ms</option>
        <option <?= $row['prefix'] == 'Prof' ? 'selected' : '' ?>>Prof</option>
    </select><br>
    <label>Full Name:</label>
    <input type="text" name="fullname" value="<?= $row['fullname'] ?>"><br>
    <label>Email:</label>
    <input type="email" name="email" value="<?= $row['email'] ?>"><br>
    <label>Phone Number:</label>
    <input type="text" name="phone" value="<?= $row['phone'] ?>"><br>
    <label>Profile Picture:</label>
    <input type="file" name="image"><br>
    <button type="submit" name="update">Update</button>
</form>
</body>
</html>

delete.php

<?php
include 'db.php';
$id = $_GET['id'];
mysqli_query($conn, "DELETE FROM user_record WHERE id=$id");
header("Location: index.php");
?>