【发布时间】:2019-03-26 22:34:12
【问题描述】:
我正在尝试创建一个用户必须填写其详细信息并上传其选择的图片的注册页面。当我按下注册按钮时,出现以下错误:“(!)注意:未定义索引:/strath-cis/2018/SmartCommute/php/register.php 中的图像”和“(!)警告:file_get_contents(): /strath-cis/2018/SmartCommute/php/register.php 中的文件名不能为空" 在我的数据库中,我确实有一个名为“图像”的 BLOB 类型的字段。 我确信我连接数据库的详细信息是正确的。 有谁知道为什么会这样?
下面是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<link rel="stylesheet" type="text/css" href="../css/normalize.css"/>
<link rel="stylesheet" type="text/css" href="../css/register_css.css"/>
<title>Smart Commute</title>
</head>
<body>
<header id="headerBar">Smart Commute - Register</header>
<form class="needs-validation" novalidate action="" method="post">
<p><input type="file" accept="image/*" name="image" id="file" onchange="loadFile(event)" style="display: none;"></p>
<p><label for="file" style="cursor: pointer;">Upload Image</label></p>
<p><img id="output" width="200" /></p>
<script>
var loadFile = function(event) {
var image = document.getElementById('output');
image.src = URL.createObjectURL(event.target.files[0]);
};
</script>
<p>Name: <input type="text" class="form-control" name="name" id="name" placeholder="John Doe" autocomplete="off" value="<?php if(isset($_POST['Register'])){echo $_POST['name'];} ?>" required></p>
<p>Email: <input type="text" class="form-control" name="email" id="email" placeholder="fake@mail.com" autocomplete="off" value="<?php if(isset($_POST['Register'])){echo $_POST['email'];} ?>" required></p>
<p>Password: <input type="password" class="form-control" name="password" id="password" placeholder="Enter password" autocomplete="off" value="<?php if(isset($_POST['Register'])){echo $_POST['password'];} ?>" required></p>
<br><br>
<!-- <input type="submit" value="Submit" name="Register">-->
<button name="Register">Register</button>
</form>
</body>
</html>
<?php
session_start();
if (isset($_POST['Register']) && isset($_POST["image"]) ) {
if (empty($_POST['password']) || preg_match('/\s/', $_POST['password'])) {
echo "No spaces allowed in new password!";
} else {
include("../include/db_config.php");
include("../include/db_connect.php");
// image file directory
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL I
$name = mysqli_real_escape_string($db, $_POST['name']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$encryptedPassword = md5($password);
$sql = "SELECT * FROM `cs317madt`.`useracc` WHERE `email` = '$email'";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$result = $db->query($sql);
if (mysqli_num_rows($result) == 1) {
echo "User already exists";
} else {
$sql2 = "INSERT INTO `cs317madt`.`useracc` (`id`,`name`,`password`,`email`, `image`) VALUES (NULL, '$name', '$encryptedPassword', '$email','$image');";
if ($result2 = $db->query($sql2)) {
header("location: ../php/home.php");
$_SESSION['logged-in'] = true;
$_SESSION['name'] = $name;
} else {
echo "Something went wrong with creating your account!";
}
}
} else {
echo "Invalid email! Please enter a valid email";
}
}
}
?>
【问题讨论】:
-
切勿以明文形式或使用 MD5/SHA1 存储密码! 仅存储密码哈希值。使用 PHP 的
password_hash()和password_verify()。如果您运行的 PHP 版本低于 5.5(我真的希望您不是),您可以使用 password_compat 库来获得相同的功能。 -
警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。
标签: javascript php mysqli