上传系统问题到服务器

时间:2012-09-16 01:19:08

标签: php html mysqli

我盯着制作自己的个人资料系统,用户可以存储用户名,密码,名字,姓氏,年龄和个人资料照片。

通过此过程上传图片时出现问题。代码出现错误,我无法理解为什么。我为来自丹麦的英语道歉(注:编辑免费翻译)

这是我的代码

<?php
    include ("inc/db/db.php");
    if (isset($_POST["godkendt_bruger"]))
    {
        $query = 'SELECT NULL FROM `bruger` WHERE `brugernavn` = ?';
        if ($stmt = $mysqli->prepare($query))
        {
            $stmt->bind_param('s', $brugernavn);
            $brugernavn = $_POST["brugernavn"];
            $stmt->execute();
            $stmt->store_result();
            $count = $stmt->num_rows;
            $stmt->close();

            if ($count > 0)
            {
               $user_found = 1;
            }
        }

        if (!isset($user_found))
        {
            if ($_POST["pass"] != $_POST["gentag"])
            {
                $errors = 1;
                echo "<li id=\"check_not\">Angive ens password på siden..</li>";
            }

            if (empty($_POST["pass"]) && empty($_POST["gentag"]))
            {
                $errors = 1;
                echo "<li id=\"check_not\">Angive et password på siden..</li>";
            }

            if (empty($_POST["navn"]))
            {
                $errors = 1;
                echo "<li id=\"check_not\">Angive et Fornavn</li>";
            }

            if (empty($_POST["efternavn"]))
            {
                $errors = 1;
                echo "<li id=\"check_not\">Angive et Efternavn</li>";
            }

            if (!isset($errors))
            {
                $pb = null;
                include "inc/img/class.upload.php";
                $handle = new Upload($_FILES["file"]);

                if ($handle->uploaded)
                {
                    //lidt mere store billeder
                    $handle->image_resize = true;
                    $handle->image_ratio_y = true;
                    $handle->image_x = 220;
                    $handle->Process("profil/store");

                    //til profil billede lign..
                    $handle->image_resize = true;
                    $handle->image_ratio_crop = true;
                    $handle->image_y = 115;
                    $handle->image_x = 100;
                    $handle->Process("profil");

                    //til profil billede lign..
                    $handle->image_resize = true;
                    $handle->image_ratio_crop = true;
                    $handle->image_y = 75;
                    $handle->image_x = 75;
                    $handle->Process("profil/lille");
                    $pb = $handle->file_dst_name;
                }
                else
                {
                    echo 'Der opstod en fejl i erklæringen: - upload ' . $mysqli->error;
                }
            }

            //Lukker $errors
            if (!isset($user_found))
            {
                $query = 'INSERT INTO `bruger` '
                       . '(`Brugernavn`, `password`, `profilbillede`, '
                       . '`navn`, `efternavn`, `alder`) '
                       . 'VALUES (?, ?, ?, ?, ?, ?)';
                if ($stmt = $mysqli->prepare($query))
                {

                $stmt->bind_param(
                    'sssssi', 
                    $brugernavn, 
                    $password, 
                    $profilbillede, 
                    $navn, 
                    $efternavn, 
                    $alder
                );

                $brugernavn    = $_POST["brugernavn"];
                $password      = $_POST["pass"];
                $profilbillede = $pb;
                $navn          = $_POST["navn"];
                $efternavn     = $_POST["efternavn"];
                $alder         = $_POST["alder"];

                $stmt->execute();
                $stmt->close();

            }
            else
            {
                /* Der er opstået en fejl */
                echo 'Der opstod en fejl i erklæringen til ligge i databasen: ' . $mysqli->error;
            }
        }
    }
    else
    {
        echo "<li id=\"check_not\">Dette brugernavn er optaget!!</li>";
    }
}
else
{
    echo "<li id=\"check_opret\">Indtast dine oplysninger herunder for at opret en bruger </a></li>";
}

?>

此块中出现错误

if ($handle->uploaded)
{
    //lidt mere store billeder
    $handle->image_resize = true;
    $handle->image_ratio_y = true;
    $handle->image_x = 220;
    $handle->Process("profil/store");

    //til profil billede lign..
    $handle->image_resize = true;
    $handle->image_ratio_crop = true;
    $handle->image_y = 115;
    $handle->image_x = 100;
    $handle->Process("profil");

    //til profil billede lign..
    $handle->image_resize = true;
    $handle->image_ratio_crop = true;
    $handle->image_y = 75;
    $handle->image_x = 75;
    $handle->Process("profil/lille");
    $pb = $handle->file_dst_name;

}
else
{
    echo 'Der opstod en fejl i erklæringen: - upload ' . $mysqli->error;
}

错误是: Der opstod en fejlierklæringen: - upload 声明中有错误),没有别的。数据不存储在数据库中。

这是我的html她;

<form name="opret_bruger" method="post" action="#" enctype="multipart/form-data">
<tr>
    <td><p>Brugernavn</p></td>
    <td><input type="text" name="brugernavn"></td>
</tr>
<tr>
    <td><p>Password</p></td>
    <td><input type="password" name="pass"></td>
</tr>
<tr>
    <td><p>Password gentag</p></td>
    <td><input type="password" name="gentag"></td>
</tr>
<tr>
    <td><p>Fornavn</p></td>
    <td><input type="text" name="navn"></td>
</tr>
<tr>
    <td><p>Efternavn</p></td>
    <td><input type="text" name="efternavn"></td>
</tr>
    <tr>
    <td><p>Alder</p></td>
    <td>
        <select name="alder_1">
    <?php
    if ($stmt = $mysqli->prepare('SELECT `alder` FROM `alder`')) { 
        $stmt->execute();

        /* Bind resultatet */
        $stmt->bind_result($alder);

        /* Hent rækker og udskriv data */
        while ($stmt->fetch()) {
        ?>
            <option><?php echo $alder;?></option>
        <?php
        }

            /* Luk statement */
            $stmt->close();

        } else {
            /* Der er opstået en fejl */
            echo 'Der opstod en fejl i erklæringen: ' . $mysqli->error;
        }
        ?>
        </select>
    </td>
</tr>
<tr>
    <td><p>Upload Profilbillede</p></td>
    <td><input type="file" name="profilbillede"></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="godkendt_bruger"></td>
</tr>
</form>

如果您有任何建议我会欢迎他们,甚至更好地处理代码,以便可以存储图像。

Jesper - Danmark

2 个答案:

答案 0 :(得分:0)

您的文件上传字段实际上名为profilbillede

                     <td><input type="file" name="profilbillede"></td>
                                                  ^^^^^^^^^^^^^

这意味着当你使用它时它将是$_FILES['profilbillede'],而不是$_FILES['file']

答案 1 :(得分:0)

一些建议:

检查文件的上传路径。

$handle->Process("profil/store");

是存储文件的位置。如果文件未上传,只需检查文件权限即可。

此外,下面的代码是否有效?我可能错了,但是在绑定之前不应该设置参数吗?即。

            $query = 'INSERT INTO `bruger` '
                   . '(`Brugernavn`, `password`, `profilbillede`, '
                   . '`navn`, `efternavn`, `alder`) '
                   . 'VALUES (?, ?, ?, ?, ?, ?)';
            if ($stmt = $mysqli->prepare($query))
            {

            $brugernavn    = $_POST["brugernavn"];
            $password      = $_POST["pass"];
            $profilbillede = $pb;
            $navn          = $_POST["navn"];
            $efternavn     = $_POST["efternavn"];
            $alder         = $_POST["alder"];

            $stmt->bind_param(
                'sssssi', 
                $brugernavn, 
                $password, 
                $profilbillede, 
                $navn, 
                $efternavn, 
                $alder
            );

我希望这会有所帮助。