0

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,

  1. $target = "../images/".basename($_FILES['image']['name']);

  2. $image = mysqli_real_escape_string($db, $_FILES['image']['name']);

Please, someone give the solution. What should I Do now?

Khokon M.
  • 402
  • 3
  • 11

2 Answers2

0

You need to set attribute enctype form in-order to upload files:

<form method="post" action="register.php" enctype="multipart/form-data">

Read more

0

try adding

multipart/form-data 

to your form

 <form method="post" enctype="multipart/form-data" action="register.php">
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • Can you please explain the reason behind this error? And Why this encryption is so needed? – Khokon M. Sep 02 '18 at 05:17
  • @MDKHOKONMIA take a look at this question, it explains that very well :) https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean/4526286 – Kanishka Panamaldeniya Sep 02 '18 at 05:24
  • Sir, I want to ask another question now. But StackOverflow is not letting me ask that question right now. So, it would be great for me if you let me ask you that question personally. Can you please give me a chance? – Khokon M. Sep 02 '18 at 05:36
  • yes you can comment your problem here. i will help :) – Kanishka Panamaldeniya Sep 02 '18 at 05:38
  • how to prevent users from inputting lowercase characters. And how to recognize a user if he is trying to log in with the wrong username and password for multiple time? – Khokon M. Sep 02 '18 at 05:54
  • Hello sir, I didn't get a response from you last time. Would you like to give me the solution, please? – Khokon M. Sep 06 '18 at 04:23