Care este problema în codificare mea php? [Salvarea imaginii pe bază de date dintr-un formular html]

voturi
0

Ei bine, vreau să salvați imagini de încărcare al utilizatorului. Aceasta este register.php mea

<?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>

Și acest lucru este server.php meu

<?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 IGNORE  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');
  }
}
?>

Dar când încerc să curgă register.php meu mi spune

Notă: indicele nedefinita: imagine în C: \ xampp \ htdocs \ personalizat \ jcschool \ php \ server.php pe linia 17

Notă: indicele nedefinita: imagine în C: \ xampp \ htdocs \ personalizat \ jcschool \ php \ server.php pe linia 20 care sunt aceste două linii,

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

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

Te rog, cineva da solutia. Ce ar trebui să fac acum?

Întrebat 02/09/2018 la 05:02
sursa de către utilizator
În alte limbi...                            


2 răspunsuri

voturi
0

încercați să adăugați

multipart/form-data 

pentru a forma

 <form method="post" enctype="multipart/form-data" action="register.php">
Publicat 02/09/2018 la 05:09
sursa de către utilizator

voturi
0

Trebuie să setați atributul enctypeformular în scopul de a încărca fișiere:

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

Citește mai mult

Publicat 02/09/2018 la 05:07
sursa de către utilizator

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more