<?php
include 'include/AnonymousHeader.php';

$CON = @mysqli_connect('localhost', 'root', '', 'allyouneed') OR die('Could not connect to MySQL: ' . mysqli_connect_error());

    ?>

    <table width="700">
        <tr>
            <td>
                <h1>Sales Registration</h1>
            </td>
        </tr>


        <?php
        if (!(empty($_SESSION['LoginStatus']))) {
        $SQL1 = "SELECT *
                     FROM sales";
        $STM1 = @mysqli_query($CON, $SQL1);

        $ROW = 0;
        $SalesID = "";

        if ($STM1) {
            $ROW = @mysqli_num_rows($STM1);
        } else {
            echo mysqli_error($CON);
        }

        if ($ROW == 0) {
            $SalesID = "SALES00001";
        } else {
            $SalesID = sprintf("SALES%05d", $ROW + 1);
        }

        if (isset($_POST['SUBMIT'])) {
            $SalesProdName = $_POST['SalesProdName'];
            $SalesDescription = $_POST['SalesDescription'];
            $SalesImage = $_FILES['SalesImage'];
            $SalesQty = $_POST['SalesQty'];
            $SalesPrice = $_POST['SalesPrice'];
            $SalesCategory = $_POST['SalesCategory'];
            $MID = $_SESSION['IDInfo'];

            $error = array();


            if (!empty($SalesProdName)) {
                if (!(preg_match('/^[A-Za-z0-9]+$/', $SalesProdName))) {
                    $error[] = 'Sales Name must only contains charater(s) or digit(s)';
                }
            } else {
                $error[] = 'Sales Name must not be empty.';
            }

            if (empty($SalesDescription)) {
                $error[] = 'Sales Description must not be empty.';
            }

            if ($SalesImage['size'] == 0) {
                $error[] = 'No image was selected';
            } else if ($SalesImage['error'] > 0) {
                switch ($SalesImage['error']) {
                    case UPLOAD_ERR_NO_FILE:
                        $error[] = 'The file is not exist.';
                        break;
                    case UPLOAD_ERR_FORM_SIZE:
                        $error[] = 'image uploaded is too large. Maximum 1MB allowed.';
                        break;
                    default:
                        $error[] = 'There was an error while uploading the image.';
                        break;
                }
            } else if ($SalesImage['size'] > 1048576) {
                $error[] = 'image uploaded is too large. Maximum 1MB allowed.';
            } else {
                $ext = strtolower(pathinfo($SalesImage['name'], PATHINFO_EXTENSION));
                if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') {
                    $error[] = 'Only JPG, GIF and PNG format are allowed.';
                }
            }

            if (!empty($error)) {
                ?>
                <table border="1" bordercolor="red" bgcolor="pink" width="700">
                    <tr>
                        <td>
                            <h3>There are error(s) occured at the following area:</h3>
                            <?php
                            foreach ($error as $ERR) {
                                ?>
                                <font color="red">=> <?php printf("%s", $ERR); ?></font><br/>
                                <?php
                            }
                            ?>
                        </td>
                    </tr>
                </table>                    
                <?php
            } else {

                $save_as = $SalesID . '.' . $ext;
                move_uploaded_file($SalesImage['tmp_name'], 'image/' . $save_as);
                $SalesImage = 'image/' . $save_as;
                $SalesStatus = "Available";
                date_default_timezone_set("Asia/Kuala_Lumpur");
                $CurrentDate = date('Ymd');
                $CurrentDate = substr($CurrentDate, 0, 4) . '-' . substr($CurrentDate, 4, 2) . '-' . substr($CurrentDate, 6, 2);
                $SQLSAL2 = "INSERT INTO sales
                   (SalesID, SalesProdName, SalesCategory, SalesImage, SalesDescription, SalesQty, SalesPrice, SalesStatus, MID, SalesDate)
                    VALUES('$SalesID', '$SalesProdName', '$SalesCategory', '$SalesImage' , '$SalesDescription', '$SalesQty', '$SalesPrice', '$SalesStatus', '$MID', '$CurrentDate')";
                $SAL2 = @mysqli_query($CON, $SQLSAL2);
                if ($SAL2) {
                    ?>
                    <script language="javascript">alert("Sales Registered.")</script>
                    <?php
                } else {
                    echo mysqli_error($CON);
                }
            }
        }
    }
    ?>    


    <form action="" method="POST" ENCTYPE = "multipart/form-data">
        <table>

            <tr>
                <td>
                    Sales ID: 
                </td>
                <td>
                    <?php echo "$SalesID"; ?>
                </td>                
            </tr>

            <tr>
                <td>
                    Product Name :
                </td>
                <td>
                    <input type="text" name="SalesProdName" id="SalesProdName" maxlength="50"/>
                </td>
            </tr>

            <tr>
                <td>
                    Category :
                </td>
                <td>
                    <select name="SalesCategory" id="SalesCategory">
                        <option>Select one</option>
                        <option>Art</option>
                        <option>Book</option>
                        <option>Digital Device</option>
                        <option>Electronic</option>
                        <option>Entertainment</option>
                        <option>Model Figure</option>
                        <option>Sports</option>
                        <option>Vehicles</option>
                        <option>Video Games</option>
                        <option>Others</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    Product Image :
                </td>
                <td>
                    <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
                    <input type="file" name="SalesImage" id="SalesImage"/>
                </td>
            </tr>

            <tr>
                <td>
                    Product Description :
                </td>
                <td>
                    <textarea rows="3" cols="50" name="SalesDescription" id="SalesDescription"></textarea>
                </td>
            </tr>

            <tr>
                <td>
                    Quantity : 
                </td>
                <td>
                    <input type="text" name="SalesQty" id="SalesQty">
                </td>
            </tr>

            <tr>
                <td>
                    Product Price :
                </td>
                <td>
                    <input type="text" name="SalesPrice" id="SalesPrice"/>
                </td>
            </tr>


            <tr>
                <td align="center" colspan="2">
                    <input type="submit" value="Submit" name="SUBMIT" ID="SUBMIT" />
                    <input type="reset" value="Reset" />
                </td>
            </tr>
        </table>
    </form>   


    <?php
    include 'include/footer.php'
    ?>

Try this:

$SalesImage = $_FILES['SalesImage']['tmp_name'];
Member Avatar for LastMitch

@aldred

Undefined Index for Sales Image

Which line?

line 40 at SUBMIT post there

$SalesImage = $_FILES['SalesImage']['tmp_name'];
Coreyavis,
I have tried this, it will got undefined index on line 40

Have you tried this to ensure you are getting the data passed through?

print "<pre>";
print_r($_FILES);
print "</pre>";

it unable to print that out the images

What exactly prints out, please post here (is it just an empty array?)

Array
(
)

empty array

$icon=$FILE['icon']['name']; in line 40
move_uploaded_file($_FILES['SalesImage']['tmp_name'], 'image/' . $save_as); in line 104

Having that empty array means that the image is not being passed through with the form, maybe the file size is too big or there is some other error with the file (have you got any checks in place for situations like this)?

The file size is below 1MB and the file do not have any problem. I even tried others 10 image file but still cannot pass through the form.

@febinameer

$icon=$FILE['icon']['name']; in line 40
move_uploaded_file($_FILES['SalesImage']['tmp_name'], 'image/' . $save_as); in line 104

is it the $_FILES in the line 40?
then the icon should be the salesimage name?
even i change it or use it the original,i still unable to upload the image

It cannot be anything to do with the code on your receiving page as the error is Undefined Index, meaning the variable $salesimage has received no value (no data has been posted for the image), and this has been confirmed by you printing out the file array and it being empty.

I have just noticed that you have no action defined in your opening form tag so how are you actually sending the form data to the script?? Change it to

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">

And see if that helps. If not then I have no other solutions or suggestions apart from stripping all the other form elements and php out apart from those elements related to the file upload and see if it works then and if so, slowly add bits back in to find what is causing the issue.

Thanks, simplypixie.
The form action is still could not solve the problem. I will try find others solution. Thank you for your trouble.

I ran a test of this code, with a few modifications since I don't have your database, and I didn't receive any errors. Perhaps your error isn't with this code, but some kind of configuration setting on your server or something. Have you tried changing your error reporting settings to give you more details on the error.

i think the error is it unable to POST the file to the variable when submit the button. But on here, if i excluded the header then it did not have any problem to submit the form.

But the header is only unusable on the insert page. The others page is available.

Member Avatar for LastMitch

i think the error is it unable to POST the file to the variable when submit the button.

$SalesImage = $_FILE['SalesImage'];

Try $_POST['SalesImage'];

Since you already have a colmun and id for each images.

Hi,

copy and save whateverFileNameYouWant.php, upload to your server, and then direct your browser to this file..

<?php

phpinfo();

?>

Look for the following values, and they should have something more than 2MB at least 100MB or higher depending on your application

post_max_size
upload_max_filesize

If those above php.ini settings still on the default value you need to change them.. To change it, you need to ask your host. If you are testing it on WAMPP or XAMPP, look for the loaded configuration file on the page above..

Loaded Configuration File // this should have a value of /php/php.ini depending on your server setttings

If your host does not allow you modify your php.ini file look for the "server API" value of your server". Is it fast CGI? or is it Apache Module?

If it is CGI, FAST CGI, or anything with CGI. create a new file and save it as php.ini. paste codes below to this file

post_max_size = 200M
upload_max_filesize = 200M

Upload this file to the root directory of your server or your domain...CANNOT be in the same level as the public_html file.

If server API says, apache module.. create a new document on your notepad and save it as .htaccess

php_value post_max_size 200M
php_value upload_max_filesize 200M

WARNING! If you have an existing .htaccess file anywhere where the upload script is running, just ADD the above codes to it.

If you are running your script in LAMPPP or XAMPP just edit the php.ini file with notepad.

If you are running xampp on linux, ubuntu and all other distros, type in your terminal

sudo -s
sudo gedit /opt/lampp/php/php.ini

hit enter, search for the post_max_size and the upload_max_filesize and change their value. On the gedit, click on the save button.. the editor.. restart your LAMPP ... sudo /opt/lampp/lampp restart

end of my response... time to get back to my school homework...

Thanks guys, it is solved. I just change the header. Somehow it is the original header problem.

Member Avatar for LastMitch

@aldred

Thanks guys, it is solved. I just change the header. Somehow it is the original header problem.

It's great to hear that you solve it! Can you click Mark Question Solve on the bottom left corner so the thread is close and solve so noone can add stuff to it. Thanks.

commented: To Rectify what some retard did to LastMitch +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.