I have some data in a MySQL table, and I want to use them to populate form fields, so the site's admin can edit them. This is what I have so far in modify.php, but it's not working:
<?php
// configuration
require("../../includes/config.php");
// query users table to retrieve admin homepage's contents
// $users = query("SELECT * FROM users WHERE id = ?");
//Class import for image uploading
//classes is the map where the class file is stored (one above the root)
include ("../../classes/upload/upload_class.php");
$id = $_GET["id"];
$getuser = query("SELECT * FROM users WHERE id = '$id'");
// associative array
$rows = mysqli_fetch_array($getuser, MYSQLI_ASSOC);
// if form was submitted, modify user
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if ($rows == true)
{
//This gets all the other information from the form
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$username = $_POST["username"];
$usersex = $_POST["usersex"];
$password = crypt($_POST["password"]);
$email = $_POST["email"];
$phone = $_POST["phone"];
$userimage = ($_FILES["userimage"]["name"]);
}
// validate submission
if (empty($_POST["firstname"]))
{
apologize("Provide your first name.");
}
if (empty($_POST["lastname"]))
{
apologize("Provide your last name.");
}
if (empty($_POST["username"]))
{
apologize("Provide a username.");
}
if (empty($_POST["usersex"]))
{
apologize("Select your sex.");
}
else if (empty($_POST["password"]))
{
apologize("Enter a password.");
}
else if (empty($_POST["confirmation"]))
{
apologize("Confirm your password.");
}
else if ($_POST["password"] != $_POST["confirmation"])
{
apologize("Password and confirmation do not match.");
}
if (empty($_POST["email"]))
{
apologize("Provide your email address.");
}
if (empty($_POST["phone"]))
{
apologize("Enter your phone number.");
}
//This is the directory where images will be saved
$max_size = 1024*250; // the max. size for uploading
$my_upload = new file_upload;
$my_upload->upload_dir = "../images/user/"; // "files" is the folder for the uploaded files (you have to create this folder)
$my_upload->extensions = array(".png", ".gif", ".jpeg", ".jpg"); // specify the allowed extensions here
// $my_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
$my_upload->max_length_filename = 50; // change this value to fit your field length in your database (standard 100)
$my_upload->rename_file = true;
$my_upload->the_temp_file = $_FILES['userimage']['tmp_name'];
$my_upload->the_file = $_FILES['userimage']['name'];
$my_upload->http_error = $_FILES['userimage']['error'];
$my_upload->replace = "y";
$my_upload->do_filename_check = "n"; // use this boolean to check for a valid filename
if ($my_upload->upload()) // new name is an additional filename information, use this to rename the uploaded file
{
if (!empty($_POST["username"]))
{
// validate username
$username = ($_POST["username"]);
if (!preg_match("/^[a-zA-Z0-9]*$/", $username))
{
apologize("Username must contain only letters and numbers.");
}
if (strlen($username) < 4 || strlen($username) > 10)
{
apologize("Username must be from 4 to 10 characters.");
}
// validate email address
$email = ($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
apologize("Invalid email address.");
}
if ($_POST["email"] === false)
{
apologize("The email has already been taken.");
}
// Don't allow country codes to be included (assumes a leading "+")
if (preg_match('/^(\+)[\s]*(.*)$/',$phone))
{
apologize("You should not include the country code.");
}
// Remove hyphens - they are not part of a telephone number
$phone = str_replace ('-', '', $phone);
// Now check that all the characters are digits
if (!preg_match('/^[0-9]{10,11}$/',$phone))
{
apologize("Phone number should be either 10 or 11 digits");
}
// Now check that the first digit is 0
if (!preg_match('/^0[0-9]{9,10}$/',$phone))
{
apologize("The telephone number should start with a 0");
}
if ($_POST["phone"] === false)
{
apologize("The phone number is already in the database.");
}
// insert form input into database
$result = query("UPDATE users (firstname, lastname, username, usersex, hash, email, phone, userimage) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
$_POST["firstname"],
$_POST["lastname"],
$_POST["username"],
$_POST["usersex"],
crypt($_POST["password"]),
$_POST["email"],
$_POST["phone"],
$_FILES["userimage"]["name"], $_POST["id"]);
// if username is in database
if ($result === false)
{
apologize("Username has been taken");
}
// update users' DB table to reference the image's new file name
query(sprintf("UPDATE users SET userimage = '%s'", $my_upload->file_copy));
// find out user's ID
$rows = query("SELECT LAST_INSERT_ID() AS id");
$id = $rows[0]["id"];
// redirect to portfolio
redirect("list-users.php");
}
}
}
// render portfolio
adminrender("modify-user_template.php", ["title" => "Admin - Modify User"]);
?>
The following is the modify-user_template.php:
<a href="index.php">Home</a> | <a href="myprofile.php">My Profile</a> | <a href="list-users.php">List Users</a> | <a href="add-user.php">Add User</a> | <a href="history.php">History</a> | <a href="resetpass.php">Modify Password</a> | <a href="logout.php">Sign Out</a>
<h1>Admin - Modify User</h1>
<?php
$id = $_GET["id"];
$getuser = query("SELECT * FROM users WHERE id = '$id'");
// associative array
while ($rows = mysqli_fetch_array($getuser, MYSQLI_ASSOC))
{
printf('<form enctype="multipart/form-data" action="add-user.php" method="post">');
printf('<fieldset>');
printf('<div class="form-group">');
printf('<input autofocus class="form-control" name=" . $rows["firstname"] . placeholder="First Name" type="text"/>');
printf('<input type="hidden" name=" . $rows["id"] . id="id"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<input autofocus class="form-control" name="lastname" placeholder="Last Name" type="text"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<input autofocus class="form-control" name="username" placeholder="Username" type="text"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<select autofocus class="form-control" name="usersex" value="usersex">');
printf('<option value="Male" selected="selected">Male</option>');
printf('<option value="Female">Female</option>');
printf('</select>');
printf('</div>');
printf('<div class="form-group">');
printf('<input class="form-control" name="password" placeholder="Password" type="password"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<input class="form-control" name="confirmation" placeholder="Confirm Password" type="password"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<input autofocus class="form-control" name="email" placeholder="Email" type="text"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<input autofocus class="form-control" name="phone" placeholder="Phone" type="text"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<input autofocus class="form-control" name="userimage" id="fileimage" placeholder="Your Photo" type="file"/>');
printf('</div>');
printf('<div class="form-group">');
printf('<button type="submit" class="btn btn-default" name="Register" value="Register">Register</button>');
printf('</div>');
printf('</fieldset>');
printf('</form>');
}
?>
<div>
</div>
<br/>
Please, I need all your help. Thanks in advance.