1

我正在编写一个将文件上传到 MySQL 数据库的脚本。我收到一条错误消息: Notice: Undefined variable: code in C:\wamp\www\application\letters.php on line 82 这是 ** 中的字代码。任何可以发现错误的人请告诉我。

if ($name)
    if ($title && $description)
        {       
            $date = date("d m Y");
            $charset ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            //length of value to generate
            $length = 15;

            //create variable and run through and randomly select fromcharset.
            for ($i = 0; $i <= $length; $i++)
            {   
                //position to start at. rand function.
                $rand = rand() % strlen($charset);
                $tmp = substr($charset, $rand, 1);

                //append onto code
                **$code .= $tmp;**
            }
            //checking for existence of code which is generated. 
            $query= mysql_query("SELECT code FROM letter_details WHERE code = '$code'");

            //if code is found
            $numrows = mysql_num_rows($query);

            // if that code exists, generate code again
            while ($numrows != 0)
            {
                for ($i=0; $i <= $length; $i++)
                {
                    //position to start at. rand function.
                    $rand = rand() % strlen($charset);
                    $tmp = substr($charset, $rand, 1);

                    //append onto code
                    $code .=$tmp;
                }

                //checking for existence of code which is generated.
                $query= mysql_query("SELECT code FROM letter_details WHERE code = '$code'");

                //if code is found
                $numrows = mysql_num_rows($query);
            }

            //create directory
            mkdir("files/$code");

            //put file into it
            move_uploaded_file($tmpname, "files/$code/"."$name".$ext);
            $query = mysql_query("INSERT INTO letter_details VALUES ('$letter_id', $title','$code','$description','$student_info_id', '$staff_info_id', '$date')");
            echo "Your file '$title' was Succesfully uploaded.<br><br><a href='download.php?file=$code'>Download</a>";
4

5 回答 5

1

$code='';在循环外添加一个for。在这种情况下,变量将在使用之前声明,即连接操作。

于 2013-04-07T18:36:45.530 回答
0

您必须$code在添加更多内容之前定义$code .= '' (放在$code = '';循环之前)

于 2013-04-07T18:37:37.537 回答
0

如果要在for循环外读取变量,则必须先定义它。

于 2013-04-07T18:38:07.520 回答
0

这是警告您未定义变量的通知。

程序的执行不受通知影响。要解决此问题,您可以在脚本开头的某处初始化 $code 变量,例如$code = '';

于 2013-04-07T18:39:13.203 回答
0

这是您必须在脚本中用于上传文件的代码片段:

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

检查并查看它是否适合您。

于 2013-04-07T18:56:13.707 回答