Skip to main content

Develop a PHP application to make following Operation: Registration of user, Insert the details of user, Modify the details

Introdution

Are you looking for an application to register, insert and change details of a user hassle-free? 🤔 Look no further – developing a simple PHP application for user registration and modification is easier than it seems! 🤩 With this user-friendly PHP code, you can easily build a basic app to manage user details. 💻 From registration to modification, this tutorial gives you all the basics to get started! 🎉

Code

<?php 

// Create database connection
$db = mysqli_connect('localhost', 'root', '', 'user_db');

// Initialize message variable
$msg = "";

// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
";

if (mysqli_query($db, $sql)) {
// If image is upload successfully
if ($_FILES['image']['error'] == 0) {
// Get details of the user
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$image = $_FILES['image']['name'];
// Save image in the server
$target = "images/" . basename($image);

// Execute query
mysqli_query($db, $sql);

// Insert user details in the table
$sql_details = "INSERT INTO user_details
(first_name, last_name, email,
phone, image) VALUES
('$first_name', '$last_name', '$email',
'$phone', '$image')";

// Execute query
mysqli_query($db, $sql_details);

// Move uploaded image into images folder
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "User registered successfully!";
// No image selected
} else {
$msg = "Failed to register user";
}
}
// No image uploaded
} else {
$msg = "Please choose an image";
}
}

// Retrieve image from the images folder and render it in the browser
$result = mysqli_query($db, "SELECT * FROM user_db");
?>

<html>
<body>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/".$row['image']."' >";
echo "</div>";
}
?>

<form method="POST" action="register_user.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<input type="text" name="first_name" placeholder="First Name">
</div>
<div>
<input type="text" name="last_name" placeholder="Last Name">
</div>
<div>
<input type="text" name="email" placeholder="E-mail">
</div>
<div>
<input type="text" name="phone" placeholder="Phone Number">
</div>
<div>
<input type="submit" name="upload" value="Upload Now">
</div>
</form>
</body>
</html>
``

#### Output
User registered successfully!

:::tip[Explanation]
This code creates a user registration form with input fields to enter a user's First Name, Last Name, Email and Phone Number. When the user submits the form, the details are inserted into a users database along with the image if it is uploaded.

:::

## Related Links
- [Tutorialspoint](https://www.tutorialspoint.com/php/php_and_mysql.htm)
- [php.net](https://www.php.net/manual/en/features.file-upload.php)
- [w3schools](https://www.w3schools.com/php/php_file_upload.asp)
- [phpzag](https://www.phpzag.com/simple-php-user-registration-form/)
- [YouTube](https://www.youtube.com/watch?v=rF1_N8pN0k0)

## Get Help
- [Github BCA Discussions](https://github.com/xmi1an/bca/discussions)
- [Github](https://github.com/xmi1an)
- [Twitter](https://twitter.com/xmi1an)
- [Instagram](https://www.instagram.com/xmi1an)