Page 1 of 1

saving file help...

Posted: Tue Feb 08, 2005 9:29 pm
by pleigh
hi there,

i have this code below where the user can upload image to mysql:

Code: Select all

<html>
<head><title>Store binary data into SQL Database</title></head>
<body>

<?php
// code that will be executed if the form has been submitted:

if (isset($_POST&#1111;'submit'])) &#123;

    // connect to the database
    // (you may have to adjust the hostname,username or password)

    MYSQL_CONNECT("localhost","root","password");
    mysql_select_db("sdmc");

    $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

    $result=MYSQL_QUERY("INSERT INTO binary_ata (description,bindata,filename,filesize,filetype) ".
        "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

    $id= mysql_insert_id();
    print "<p>This file has the following Database ID: <b>$id</b>";

    MYSQL_CLOSE();

&#125; else &#123;

    // else show the form to submit new data:
?>

    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description"  size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>

<?php

&#125;

?>

</body>
</html>
my problem is that the database is not updated with the file being uploaded. whats wrong with the code?

thanks.

pleigh

Posted: Tue Feb 08, 2005 9:45 pm
by feyd
your code is using a mix of register_globals and not.. which is it? are register_globals on?

Posted: Tue Feb 08, 2005 9:58 pm
by pleigh
thanks for the reply feyd...my register_globals is turned off...how can i solve this problem?

Posted: Tue Feb 08, 2005 10:00 pm
by feyd
you need to switch out all the form field names to $_POST and $_FILES hooks...

Posted: Tue Feb 08, 2005 10:10 pm
by pleigh
thanks feyd, i'll try to switch them...

Posted: Tue Feb 08, 2005 10:19 pm
by pleigh
oh man....which is which? i can't figure out which is for $_POST and which is for $_FILES....need help...

Posted: Tue Feb 08, 2005 10:20 pm
by feyd
lets see.... $_FILES is for... files.. and $_POST is for posted data that isn't files..... :roll:

Posted: Tue Feb 08, 2005 10:37 pm
by pleigh
seems to me that i'm a slow learner, so i just turned on the register_globals and change the

Code: Select all

if (isset($_POST'submit']))
code to

Code: Select all

if ($submit)
it does save the file, but there is one error:
Notice: Undefined variable: submit in c:\inetpub\wwwroot\mysample\TMP4c6e9bmncr.php on line 8
how can i resolve it?

or, is it safe for me to turn the register globals ON?

thanks again.

pleigh

Posted: Tue Feb 08, 2005 10:55 pm
by feyd
it's not the best idea to have register_globals on.. If you submitted the form without clicking/hitting 'submit' then $submit will not exist.