【问题标题】:Error "Undefined variable"错误“未定义的变量”
【发布时间】:2014-01-02 11:00:53
【问题描述】:

我的代码一直显示错误undefined variable: code,但它会添加到我的数据库中。我认为问题出在第一个循环$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;
}

【问题讨论】:

  • 你需要先初始化$code变量,然后才能像$code .= $tmp一样使用它。

标签: php variables syntax-error concatenation string-concatenation


【解决方案1】:

在使用变量之前总是要练习初始化变量,所以正如我在上面的评论中提到的,你需要在使用它之前初始化 $code 变量,就像在某个地方使用 $code .= $tmp 一样,

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

【讨论】:

  • 非常感谢。我忘了如何初始化变量。再次,谢谢你。新年快乐:)
  • 很高兴为您提供帮助。也祝你新年快乐。如果您的问题由此解决,请accept回答。
【解决方案2】:
<?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;
            $code='';
            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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2018-05-03
    • 2014-04-25
    • 2021-04-10
    • 2011-12-21
    相关资源
    最近更新 更多