我正在编写一个文件上传到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>";发布于 2013-04-08 02:37:38
在通过$code .= ''向其中添加更多内容之前,必须先定义$code (将$code = '';放在循环之前)
发布于 2013-04-08 02:36:46
在这种情况下,在for loop.Variables之外添加一个$code='';是要声明的,即连接操作,然后再使用它们。
发布于 2013-04-08 02:38:08
如果你想在for循环之外读取一个变量,你必须在之前定义它。
https://stackoverflow.com/questions/15866171
复制相似问题