Well, I want to save images that user upload. This is my register.php
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Web JC. High School | By MD Khokon</title>
<link rel="stylesheet" type="text/css" href="../style.css">
<link rel="stylesheet" href="../css/techlog.css">
</head>
<body>
<div id="main_wrapper">
<div class="header">
<div class="head_part">
<h2>Register Teacher</h2>
</div>
</div>
<div class="body">
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" style="text-transform: uppercase;" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Full Name</label>
<input type="text" name="fullname" value="<?php echo $fullname; ?>">
</div>
<div class="input-group">
<label>Photo</label>
<input type="hidden" name="size" value="1000">
<input type="file" name="image" value="<?php echo $img; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
</form>
</div>
</div>
</body>
</html>
And this is my server.php
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$fullname = "";
$image = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'school_database');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$target = "../images/".basename($_FILES['image']['name']);
$username = mysqli_real_escape_string($db, $_POST['username']);
$fullname = mysqli_real_escape_string($db, $_POST['fullname']);
$image = mysqli_real_escape_string($db, $_FILES['image']['name']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($email)) { array_push($errors, "Image is required"); }
if (empty($fullname)) { array_push($errors, "Full Name is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM teachers WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO teachers (username, full_name, email, img, password)
VALUES('$username', '$fullname', '$email', '$image', '$password')";
mysqli_query($db, $query);
if (move_uploaded_file($_FILES['image']['tmp_name'],$target)) {
echo 'File Uploaded Successfully';
}else {
echo 'Something went wrong';
}
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: ../admin/index.php');
}
}
?>
But when I try to run my register.php it says me
Notice: Undefined index: image in C:\xampp\htdocs\custom\jcschool\php\server.php on line 17
Notice: Undefined index: image in C:\xampp\htdocs\custom\jcschool\php\server.php on line 20 which are these two lines,
$target = "../images/".basename($_FILES['image']['name']);
$image = mysqli_real_escape_string($db, $_FILES['image']['name']);
Please, someone give the solution. What should I Do now?