为用户添加图像到数据库

时间:2019-06-15 19:11:03

标签: php mysql

如何将用户头像添加到数据库? 我已经通过php和ajax在网站上授权和注册了用户!有一个数据库,其中有一个varchar类型的“ img”列!我将图片下载到目录中,但无法将其添加到数据库中!请帮帮我 !!! ??? 这是代码: 文件ajax.php

    <?php 
    include 'classes/Auth.class.php';
    include 'classes/AjaxRequest.class.php';
    if (!empty($_COOKIE['sid'])) {session_id($_COOKIE['sid']);}
    session_start();
    class AuthorizationAjaxRequest extends AjaxRequest
    {
        public $actions = array(
            "login" => "login",
            "logout" => "logout",
            "register" => "register",
        );

        public function login()
        {
            if ($_SERVER["REQUEST_METHOD"] !== "POST") {
                http_response_code(405);
                header("Allow: POST");
                $this->setFieldError("main", "Method Not Allowed");
                return;
            }
            setcookie("sid", "");
            $username = $this->getRequestParam("username");
            $password = $this->getRequestParam("password");
            $remember = !!$this->getRequestParam("remember-me");
            if (empty($username)) {
              $this->setFieldError("username", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Введите ваш E-mail</span></p>");
                return;
            }

          elseif(isset($username) && !preg_match("/^([a-z-,._,0-9])+@([a-z,._,0-9])+(.([a-z])+)+$/", $username)) {
          $this->setFieldError("username", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Только действующий E-mail</span></p>");
                return; }


            if (empty($password)) {
                $this->setFieldError("password", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Введите пароль</span></p>");
                return;
            }

            $user = new Auth\User();
            $auth_result = $user->authorize($username, $password, $remember);

            if (!$auth_result) {
                $this->setFieldError("password", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Неверный Логин или Пароль</span></p>");
                return;
            }

            $this->status = "ok";
            $this->setResponse("redirect", " ");
            $this->message = sprintf("Hello, %s! Access granted.", $username);
        }

        public function logout()
        {
            if ($_SERVER["REQUEST_METHOD"] !== "POST") {
                http_response_code(405);
                header("Allow: POST");
                $this->setFieldError("main", "Method Not Allowed");
                return;
            }

            setcookie("sid", "");

            $user = new Auth\User();
            $user->logout();

            $this->setResponse("redirect", " ");
            $this->status = "ok";
        }

        public function register()
        {
            if ($_SERVER["REQUEST_METHOD"] !== "POST") {
                http_response_code(405);
                header("Allow: POST");
                $this->setFieldError("main", "Method Not Allowed");
                return;
            }

            setcookie("sid", "");
            $username = $this->getRequestParam("username"); 
            $names = $this->getRequestParam("names"); 
            $password1 = $this->getRequestParam("password1");
            $password2 = $this->getRequestParam("password2");


            if (empty($username)) {
                $this->setFieldError("username", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Enter your E-mail
</span></p>");
                return;
            }

           elseif(isset($username) && !preg_match("/^([a-z-,._,0-9])+@([a-z,._,0-9])+(.([a-z])+)+$/", $username)) {
          $this->setFieldError("username", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Only valid E-mail</span></p>");
                return; }

          if (empty($names)) {
                $this->setFieldError("names", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Enter your name</span></p>");
                return;
            }
          elseif(isset($names) && !preg_match("/[А-Яа-я]/i", $names)) {
            $this->setFieldError("names", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Name contains only Russian letters.</span></p>");
                  return; }

            if (empty($password1)) {
                $this->setFieldError("password1", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>enter password</span></p>");
                return;
            }

            if (empty($password2)) {
                $this->setFieldError("password2", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Confirm the password
</span></p>");
                return;
            }

            if ($password1 !== $password2) {
                $this->setFieldError("password2", "<p style='text-align:center;margin:0 0 10px 0;'><span style='color:#900000;'>Passwords do not match</span></p>");
                return;
            }

          $username = htmlspecialchars(trim($username));
          $names = htmlspecialchars(trim($names));
          $user = new Auth\User();
            try {
                $new_user_id = $user->create($username, $names, $password1);
            } catch (\Exception $e) {
                $this->setFieldError("username", $e->getMessage());
                return;
            }
            $user->authorize($username,  $password1);

            $this->message = sprintf("Hi% s! You have successfully registered!
.", $username);
            $this->setResponse("redirect", "/");
            $this->status = "ok";
        }
    }
    $ajaxRequest = new AuthorizationAjaxRequest($_REQUEST);
    $ajaxRequest->showResponse();
    ?>

文件配置文件/load_photo.php

<?php
if (!empty($_COOKIE['sid'])) {session_id($_COOKIE['sid']);}
session_start();
require_once '../classes/Auth.class.php';
require_once '../stayt.php';
?>
<?php 

if (isset($_POST['upload'])) {
    if (isset($_FILES['uploadfile'])) {
        $foto_name = time()."_".basename($_FILES['uploadfile']['name']);
        $error_flag = $_FILES['uploadfile']['errors'];

        if ($error_flag == 0) {
            $upfile = getcwd()."/profiles_photos/full_img/".time()."_".basename(iconv('utf-8', 'windows-1251', $_FILES['uploadfile']['name']));
            if ($_FILES['uploadfile']['tmp_name']) {
                $allowed = array('jpg','jpeg','png');
                $ext = pathinfo($_FILES['uploadfile']['name'], PATHINFO_EXTENSION);
                if (!in_array($ext, $allowed)) {
                    $errors[] = "Wrong format!";                
                }else if(move_uploaded_file($_FILES['uploadfile']['tmp_name'], $upfile)){
                    mysql_query("UPDATE users SET img = '$foto_name' WHERE id = ".$_SESSION['names']);
                }
            }else {
                $errors[] = "Error";
            }
        }
    }elseif ($_FILES['uploadfile']['size'] == 0) $errors[] = "Select image!";
}

 ?>

连接数据库是可行的,因为我已经在站点上进行了注册和授权,并且一切都在这里进行。

1 个答案:

答案 0 :(得分:0)

可能的问题是:

  1. mysql_query()PHP5.5.0中已弃用,在PHP7.0.0中已删除。如果服务器正在运行PHP version >= 7,则可能是问题的原因。
  2. 在您的mysql_connect文件中没有创建指向数据库的链接load_photo。您可能无权访问数据库。
  3. 将数据库调用附加到变量并检查结果。例如
    $query = mysql_query(...
    if($query) echo "...
    else echo mysql_error()...