PHP回调包含

时间:2014-06-12 22:33:26

标签: php mysql arrays callback

我在PHP中创建了一个类:

<?php

class _new_model extends model {

/**
 * NEW contruct
 */
public function __construct() {
    parent::__construct();
}

function newuser(){
    $password = trim($_POST['psw']);
    $password2= trim($_POST['psw1']);
    if($password === $password2){
        $hashpsw = hash('sha512', $password);
        $salt = $this->createSalt();
        $psw = hash('sha512', $salt . $hashpsw);
        $query = $this->db->prepare('INSERT INTO users (username, password, salt) VALUES (?,?,?)');
        $query->bindValue(1, $_POST['user'], PDO::PARAM_STR);
        $query->bindValue(2, $psw, PDO::PARAM_STR);
        $query->bindValue(3, $salt, PDO::PARAM_STR);
        try{
            if($query->execute()){
                echo 'ჩაიწერა!';
            }else{
                echo 'მოხდა შეცდომა!';
            }
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }else{
        echo 'პაროლები ერთმანეთს არ ემთხვევა!';
    }
}

/**
 * @return string
 */
function createSalt(){
    $text = md5(uniqid(rand(), TRUE));
    $salt = substr($text, 0, 32);
    return $salt;
}
function newlanguage(){
    $lang = trim($_POST('language'));
    $checklang = $this->checklang($lang);
    if($checklang == true){
        echo 'ეს ენა უკვე არსებობს';
    }else{
        if(is_uploaded_file($_FILES['flag']['tmp_name'])){
            $maxsize=100000;
            $size=$_FILES['flag']['size'];
            $imgdetails = getimagesize($_FILES['flag']['tmp_name']);
            $mime_type = $imgdetails['mime'];
            // checking for valid image type
            if(($mime_type=='image/jpeg')||($mime_type=='image/gif')||($mime_type=='image/bmp')||($mime_type=='image/png')){
                // checking for size again
                if($size<$maxsize){
                    $filename=$_FILES['flag']['name'];
                    $imgData =addslashes (file_get_contents($_FILES['flag']['tmp_name']));
                    $query = $this->db->prepare('INSERT INTO lang (name,mokled,flag,flagname,flagtype,flagsize) VALUES (?,?,?,?,?,?)');
                    $query->bindValue(1, $lang, PDO::PARAM_STR);
                    $query->bindValue(2, trim($_POST('shortlang')), PDO::PARAM_STR);
                    $query->bindValue(3, $imgData, PDO::PARAM_LOB);
                    $query->bindValue(4, $filename, PDO::PARAM_STR);
                    $query->bindValue(5, $mime_type, PDO::PARAM_STR);
                    $query->bindValue(6, addslashes($imgdetails[3]), PDO::PARAM_STR);
                    try{
                        if($query->execute()){
                            echo 'yes';
                        }else{
                            echo 'no query-ის გაშვებისას';
                        }
                    }catch (PDOException $e){
                        die($e->getMessage());
                    }
                }else{
                    echo "<span class='error'>Image to be uploaded is too large..Error uploading the image!!</span>";
                }
            }else{
                echo "<span class='error'>Not a valid image file! Please upload jpeg or gif image.</span>";
            }
        }else{
            switch($_FILES['flag']['error']){
                case 0: //no error; possible file attack!
                    echo "<span class='error'>There was a problem with your upload.</span>";
                    break;
                case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
                    echo "<span class='error'>The file you are trying to upload is too big.</span>";
                    break;
                case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
                    echo "<span class='error'>The file you are trying to upload is too big.</span>";
                    break;
                case 3: //uploaded file was only partially uploaded
                    echo "<span class='error'>The file you are trying upload was only partially uploaded.</span>";
                    break;
                case 4: //no file was uploaded
                    echo "<span class='error'>You must select an image for upload.</span>";
                    break;
                default: //a default error, just in case!
                    echo "<span class='error'>There was a problem with your upload.</span>";
                    break;
            }
        }
    }
}

/**
 * @param $name
 * @return bool
 */
function checklang($name){
    $query = $this->db->prepare('SELECT id FROM lang WHERE name = ?');
    $query->bindValue(1, $name, PDO::PARAM_STR);
    try{
        $query->execute();
        if($query->rowCount() > 0){
            return true;
        }else{
            return false;
        }
    }catch (PDOException $e){
        die($e->getMessage());
    }
}}

并拥有此HTML表单:

<form enctype="multipart/form-data" action="<?php echo Domain; ?>_new/newlanguage/" name="newlanguage" method="post" id="newlanguage" >
<input type="text" name="language" id="language" placeholder="New Language">
<input type="text" name="shortlang" id="shortlang" placeholder="short name"><br>
<input type="file" name="flag" id="flag" placeholder="Flag"><br>
<input type="submit" name="submit" value="submit"></form>

提交表单时,我收到此错误:

  

数组回调必须在第49行的_new_model.php中包含索引0和1   在这一行写的   返回$ salt;

这是名为create salt的函数。我不明白我什么时候打电话给新语言功能,为什么要扫描其他功能?

请帮助

1 个答案:

答案 0 :(得分:3)

$_POST('language')应为$_POST['language']