我正在尝试制作一个带有图像的教师注册文件。
对我来说,一切似乎都是正确的,但我找不到我犯的错误在哪里。
- 我检查了用户数据是否正确提供
- 我做了一个简单的函数来检查我的图像扩展是否正确
- 我已将所有用户提供的信息分配给变量
- 当我检查时,这些都是正确的
- 我将上传的文件移到了服务器文件夹中,并执行了sql
我的表列似乎是正确的,但每次都会出现错误:
Call to undefined method mysqli::error()
代码:
<?php
require_once("db_const.php");
if (!$_SERVER['REQUEST_METHOD']=='POST'
|| !$_POST['teacher_submit']=='add_teacher'
|| empty($_POST['teacher_name'])
|| empty($_POST['teacher_username'])
|| empty($_POST['teacher_password'])
|| empty($_POST['teacher_department'])
|| empty($_POST['teacher_phone'])
|| empty($_POST['teacher_email'])
|| empty($_FILES['teacher_image']))
{
$_SESSION['error'] = "All fields are required";
echo "All fields are required";
header('Location: ../admin.php?page=add-teacher');
exit;
} else {
## connect mysql server
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} :
{$mysqli->connect_error}</p>";
exit();
}
function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");
if (in_array($file['type'], $valid_types))
return 1;
return 0;
}
// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "teacher_photo/";
# prepare data for insertion
$teacher_name = $_POST['teacher_name'];
$teacher_username = $_POST['teacher_username'];
$teacher_password = $_POST['teacher_password'];
$teacher_department = $_POST['teacher_department'];
$teacher_phone = $_POST['teacher_phone'];
$teacher_email = $_POST['teacher_email'];
$teacher_image = $_FILES['teacher_image'];
$teacher_name = mysql_real_escape_string($teacher_name);
$teacher_username = mysql_real_escape_string($teacher_username);
$teacher_password = mysql_real_escape_string($teacher_password);
$teacher_department = mysql_real_escape_string($teacher_department);
$teacher_phone = mysql_real_escape_string($teacher_phone);
$teacher_email = mysql_real_escape_string($teacher_email);
$teacher_image['name'] =
mysql_real_escape_string($teacher_image['name']);
$TARGET_PATH .= $teacher_image['name'];
if (!is_valid_type($teacher_image))
{
$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
echo"You must upload a jpeg, gif, png or bmp";
exit;
}
if (file_exists($TARGET_PATH))
{
$_SESSION['error'] = "A file with that name already exists";
echo"A file with same name exists already";
exit;
}
if (move_uploaded_file($teacher_image['tmp_name'], $TARGET_PATH))
{
// NOTE: This is where a lot of people make mistakes.
// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
$sql = "INSERT INTO teachers (teacher_name, teacher_username,
teacher_password, teacher_department, teacher_phone, teacher_email,
teacher_image)
values ('$teacher_name', '$teacher_username', '$teacher_password',
'$teacher_department', '$teacher_phone', '$teacher_email', '" .
"main/teacher_photo/".$teacher_image['name'] . "')";
$result = $mysqli->query($sql) or die ("Could not insert data into
DB: " . $mysqli->error());
echo "<P>Registration Successfully Completed.";
echo "<script>setTimeout('"location.href =
'../admin.php?page=add-teacher';'",3500);</script>";
exit;
}
else
{
// A common cause of file moving failures is because of bad permissions
on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$_SESSION['error'] = "Could not upload file. Check read/write
persmissions on the directory";
header("Location: ../admin.php?page=add-teacher");
exit;
}
}
?>
$mysqli->error
不是一个方法。不使用()
。