Skip to content
Snippets Groups Projects
Commit b88ef768 authored by adyanf's avatar adyanf
Browse files

add form handler and image uploader

parent 444878f0
No related merge requests found
<?php
require('includes/config.php');
$id_active = $_GET["id_active"];
$sql = "SELECT name, phone_number, driver, image FROM users WHERE ID=$id_active LIMIT 1";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$name = $row["name"];
$phone_number = $row["phone_number"];
$driver = $row["driver"];
$image = $row["image"];
}
if(isset($_POST["back"])){
header('Location: profile.php?id_active=' . $id_active);
}
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$phone_number = $_POST["phone"];
$driver = (!isset($_POST["driver"])) ? 0 : 1;
$target_dir = "img/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
//echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
//echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
//echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 2000000) {
//echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
//echo "Sorry, your file was not uploaded.";
$sql = "UPDATE users SET name='$name', phone_number='$phone_number', driver=$driver WHERE ID=$id_active";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$image = './' . $target_file;
$sql = "UPDATE users SET name='$name', phone_number='$phone_number', driver=$driver, image='$image' WHERE ID=$id_active";
} else {
//echo "Sorry, there was an error uploading your file.";
$sql = "UPDATE users SET name='$name', phone_number='$phone_number', driver=$driver WHERE ID=$id_active";
}
}
if ($conn->query($sql) === TRUE) {
//echo "Record updated successfully";
} else {
//echo "Error updating record: " . $conn->error;
}
}
?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
...@@ -10,40 +86,46 @@ ...@@ -10,40 +86,46 @@
<div class="heading"> <div class="heading">
<h3>EDIT PROFILE INFORMATION</h3> <h3>EDIT PROFILE INFORMATION</h3>
</div> </div>
<div class="edit-img"> <form action="" method="post" enctype="multipart/form-data">
<div class="profile-img"> <div class="edit-img">
<img src="./img/profile-placeholder.png" alt="profile image" class="thumbnail"> <div class="profile-img">
</div> <img src="<?php echo $image ?>" alt="profile image" class="thumbnail">
<div class="update-img"> </div>
<h4>Update profile picture</h4> <div class="update-img">
<input id="uploadFile" type="text" disabled="disabled"> <h4>Update profile picture</h4>
<div class="fileUpload"> <input id="uploadFile" type="text" disabled="disabled">
<span>Browse...</span> <div class="fileUpload">
<input id="uploadBtn" type="file" class="upload"> <span>Browse...</span>
<input id="uploadBtn" name="fileToUpload" type="file" class="upload">
</div>
</div> </div>
</div> </div>
</div> <div class="edit-data">
<div class="edit-data"> <div class="form-group">
<div class="form-group"> <label for="name">Your Name</label>
<label for="name">Your Name</label> <input id="name" type="text" name="name" placeholder="your name" value="<?php echo $name ?>" required>
<input id="name" type="text" name="name" placeholder="your name" required> </div>
</div> <div class="form-group">
<div class="form-group"> <label for="phone">Phone</label>
<label for="phone">Phone</label> <input id="phone" type="text" name="phone" placeholder="your phone" value="<?php echo $phone_number?>" required>
<input id="phone" type="text" name="phone" placeholder="your phone" required> </div>
</div> <div class="form-group">
<div class="form-group"> <label for="driver">Status Driver</label>
<label for="driver">Status Driver</label> <label class="switch">
<label class="switch"> <?php if ($driver == 1){ ?>
<input type="checkbox"> <input name="driver" type="checkbox" checked>
<span class="slider round"></span> <?php } else { ?>
</label> <input name="driver" type="checkbox">
</div> <?php } ?>
<div class="submit"> <span class="slider round"></span>
<input type="submit" class="cancel" value="BACK"> </label>
<input type="submit" class="save" value="SAVE"> </div>
<div class="submit">
<input type="submit" class="cancel" name="back" value="BACK">
<input type="submit" class="save" name="submit" value="SAVE">
</div>
</div> </div>
</div> </form>
</div> </div>
<script src="js/profile.js"></script> <script src="js/profile.js"></script>
</body> </body>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment