dty63504 2014-01-02 11:00
浏览 50
已采纳

错误“未定义的变量”

My code keeps displaying the error undefined variable: code but it adds to my database. The problem I think is in the first loop $code .= $tmp;

<?php
include '../library/config.php';

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
    case 'create' : newTitle();
    break;
    case 'delete': deleteRecord();
    break;
    case 'archive': archiveRecord();
    break;

    default : header("Location: ../index.php");
}

function archiveRecord(){
    $title = $_POST['title'];
    $desc = $_POST['desc'];
    $adviser = $_POST['adviser'];
    $group = $_POST['group'];
    $category = $_POST['category'];
    $name = $_FILES['myfile']['name'];
    $type = $_FILES['myfile']['type'];
    $size = $_FILES['myfile']['size'];
    $tmpname = $_FILES['myfile']['tmp_name'];
    $ext = substr($name,strrpos($name, '.'));

    if ($name)
    {
        if($title && $desc)
        {

            $charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
            $length = 15;
            for ($i = 0; $i <= $length; $i++)
            {
                $rand = rand() % strlen($charset);
                $tmp = substr($charset, $rand, 1);
                $code .= $tmp;
            }

            $query = mysql_query("SELECT code FROM archive WHERE code = '$code'");
//this is the cause of my error it adds to my database but it keeps saying Notice: Undefined variable: code 

            $numrows = mysql_num_rows($query);
            while($numrows != 0)
            {
                for ($i = 0; $i <= $length; $i++)
            {
                $rand = rand() % strlen($charset);
                $tmp = substr($charset, $rand, 1);
                $code .= $tmp;
            }

            $query = mysql_query("SELECT code FROM archive WHERE code = '$code'");
            $numrows = mysql_num_rows($query);
            }

                mkdir("../file/$code");
                move_uploaded_file($tmpname, "../file/$code/"."$name" );

                $file = "$name";
                $query = mysql_query("INSERT INTO archive VALUES ('', '$title','$desc','$adviser','$group','$category','$code','$name',NOW(),NOW(),NOW(),NOW())");

                $_SESSION['message']['type'] = "success";
                $_SESSION['message']['content'] = "Your file has been uploaded";

            header("Location: ../index.php?page=archive");
            exit;
            //echo "Your file has been uploaded.<br><br><a href='download.php?code=$code'>Download</a>";

        }else
            $_SESSION['message']['type'] = "danger";
            $_SESSION['message']['content'] = "You did not fill in the form";

            header("Location: ../index.php?page=archive");
    exit;


            //echo "You did not fill in the form. $form";
    }
    else
        $_SESSION['message']['type'] = "danger";
        $_SESSION['message']['content'] = "You did not put any file";

    header("Location: ../index.php?page=archive");
    exit;
}
  • 写回答

2条回答 默认 最新

  • dqzow3859 2014-01-02 11:07
    关注

    Always have practice to intialize your varaibles before using them, So as I mention in comment above you need to initialize $code variable before using it like $code .= $tmp somewhere around,

    $code = '';
    $length = 15;
    for ($i = 0; $i <= $length; $i++)
    {
        $rand = rand() % strlen($charset);
        $tmp = substr($charset, $rand, 1);
        $code .= $tmp;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值