0

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;
}
1
  • 1
    You need to initialize $code variable before using it like $code .= $tmp. Commented Jan 2, 2014 at 11:02

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much. i forgot on how to initialize variables. again thank you so much. and happy new year :)
Glad to help you. Happy new year you too. Kindly accept the answer if your problem is solved by this.
0
<?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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.