我的 php 编码有什么问题?[从HTML表单在数据库中保存图像]



好吧,我想保存用户上传的图像。这是我的登记册.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>

这是我的服务器.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');
}
}
?>

但是当我尝试运行我的寄存器时.php它说我

注意:未定义的索引:C:\xampp\htdocs\custom\jcschool\php\server.php 第 17 行中的图像

注意:未定义的索引:C:\xampp\htdocs\custom\jcschool\php\server.php 第 20 行中的图像 哪两条线,

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

  2. $image = mysqli_real_escape_string($db, $_FILES['图像']['名称'](;

请有人给出解决方案。我现在应该怎么做?

尝试添加

multipart/form-data 

到您的表单

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

您需要在表单enctype设置属性才能上传文件:

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

阅读更多

最新更新