2015-03-24 5 views
0

Я пытаюсь создать файл регистрации учителя с изображением.Звонок на неопределенный метод mysqli :: error()

Все кажется мне правильным, но я не могу найти, где ошибка, которую я совершил.

  • Я проверил, правильно ли указаны данные пользователя.
  • Я сделал простую функцию, чтобы проверить, правильно ли было расширение изображения.
  • Я усвоил всю предоставленную пользователем информацию, присвоенную переменным
  • Все это было правильно, когда я проверил.
  • Я переместил загруженный файл в папку с сервером и выполнил мой 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; 
} 

} 

?> 

ответ

1

$mysqli->error это не метод. Используйте без ().

+0

Действительно здорово! Это отлично работает для меня. Какой я немой. Большое спасибо г-ну Хавену –

Смежные вопросы