SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert to db? upload?

    Well for alll those who read my problem (the one about querys) i have to say that is done, i fixed it but now the ppl of the team asked me to make an option of uploading a file to the server and inserting the data to the database.
    Like filling some fields that are asociated to the columns in the db.
    Can you please help me?

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote a completely self contained script that does this with images, from table creation to final retreival and display. You should be able to use roughly the same thing in your use so I will post it.

    PHP Code:
    <?php


    // Database-specific information:
        
    DEFINE (DB_USER"{$_SERVER['mysql_user']}"); // Set yours.
        
    DEFINE (DB_PASSWORD"{$_SERVER['mysql_pass']}"); // Set yours.
        
    DEFINE (DB_HOST"localhost"); // Set yours.
        
    DEFINE (DB_NAME"my_database"); // Set yours.
        
    DEFINE (DB_TABLE"images"); // Set yours.

    if(isset($_GET['create'])){
    // Establishing a database connection
        
    if(!$dbh=mysql_connect(DB_HOSTDB_USERDB_PASSWORD)){
            echo 
    'Could not connect to MySQL  because: '.mysql_error().'. <br />';
        }
        
        if(!
    mysql_select_db(DB_NAME)){
            echo 
    'Couldn\'t connect to the database "'.DB_NAME.'" because: '.mysql_error().'. <br />';
        }
        
    //mysql_query('DROP TABLE '.DB_TABLE);
        
        
    if(mysql_query('CREATE TABLE '.DB_TABLE.' (
          image_id int(15) unsigned NOT NULL AUTO_INCREMENT,
          image MEDIUMBLOB,
          image_type VARCHAR(10),
          width int(5),
          height int(5),
          image_name VARCHAR(30),
          KEY image_id (image_id)
        );'
    )){
            echo 
    'Created table: "'.DB_TABLE.'".';
        }else{
            echo 
    'Table: "'.DB_TABLE .'" could not be created because: '.mysql_error().'.';
        }
        
        print 
    '<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Continue</a>';

    }elseif(isset(
    $_GET['upload'])){
        if(!empty(
    $_FILES['file']['name'])) {
            
            
                
    // Check MIME type
                
    $size getimagesize($_FILES['file']['tmp_name']);
                if(!
    eregi('image/'$size['mime'])){
                    exit(
    '<b>Image</b> uploads only! Max size: <b>200 kilobytes</b>.');
                }
                
    // Connect to MySQL:
                
    $db_connection mysql_connect (DB_HOSTDB_USERDB_PASSWORD) or die ('Could not connect to MySQL: ' mysql_error());
                
                
                
                
    // Select the database:
                
    mysql_select_db (DB_NAME) or die ('Could not select the database: ' mysql_error());
            
                
    // Read the uploaded file.
                
    $image addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"), $_FILES['file']['size']));
                 list(
    $filename$ext) = spliti('\.'$_FILES['file']['name']);
                 list(
    $width$height) = getimagesize($_FILES['file']['tmp_name']);
                 
                
    // Generate the query. 
                
    $query "INSERT INTO images VALUES (0, '$image', '{$_FILES['file']['type']}', '$width', '$height', '$filename')";
                
                
    // Execute the query and report on its success.
                
    if (mysql_query ($query)) {
                    echo 
    'Image stored. ID: "' mysql_insert_id() . '"!';
                    print 
    '<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Continue</a>';
                }  else {
                    echo 
    'Image storage error! ' mysql_error();
                }
                
            
                
    // Close the database connection.
                
    mysql_close();
                
        }elseif(isset(
    $_POST) and !$_FILES['file']['error'][$i]==0){
            exit(
    '<b>Image</b> uploads only! Max size: <b>200 kilobytes</b>.');
        }else{
                
    echo <<<END
                        
                <form action="http://
    {$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?upload" method="post" enctype="multipart/form-data">
                <input type="hidden" name="MAX_FILE_SIZE" value="200000" />
                Select a file to upload: <input type="file" name="file" />
                <br />
                <input type="submit" name="submit" value="Submit!" />
                </form>
                
    END;
                
                
        }
    }elseif(isset(
    $_GET['index'])){
    // Connect to MySQL:
        
    $db_connection mysql_connect (DB_HOSTDB_USERDB_PASSWORD) or die ('Could not connect to MySQL: ' mysql_error());
        
        
    // Select the database:
        
    mysql_select_db (DB_NAME) or die ('Could not select the database: ' mysql_error());

        
    // Retrieve the image information.
        
    $query "SELECT image_id, image_name FROM images";
        
    $result=mysql_query($query);
        while(
    $row mysql_fetch_array($result)){
            
    extract($row);
            print(
    '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?page='.$row ['image_id'].'">'.$row['image_name'].'</a><br />');
        }
        print 
    '<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Return to main page!</a>';
        
    }elseif(isset(
    $_GET['display'])){

    // Connect to MySQL:
        
    $db_connection mysql_connect (DB_HOSTDB_USERDB_PASSWORD) or die ('Could not connect to MySQL: ' mysql_error());
        
        
    // Select the database:
        
    mysql_select_db (DB_NAME) or die ('Could not select the database: ' mysql_error());

        
    // Retrieve the image information.
        
    $query "SELECT image, image_type FROM images WHERE image_id={$_GET['display']}";
        if (
    $query_result mysql_query ($query)) {
            
    $image mysql_fetch_array($query_result);
            
    header ("Content-type: $image[1]");
            echo 
    $image[0];
        } 
        
        
    // Close the database connection.
        
    mysql_close();
            
    }elseif(isset(
    $_GET['page'])){
    // Connect to MySQL:
        
    $db_connection mysql_connect (DB_HOSTDB_USERDB_PASSWORD) or die ('Could not connect to MySQL: ' mysql_error());
        
        
    // Select the database:
        
    mysql_select_db (DB_NAME) or die ('Could not select the database: ' mysql_error());

        
    // Retrieve the image information.
        
    $query "SELECT image_name, width, height FROM images WHERE image_id={$_GET['page']}";
        
    $query_result=mysql_query($query) or die (mysql_error());
        
    $row mysql_fetch_array($query_result) or die (mysql_error());;
        print 
    '<img src="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?display='.$_GET['page'].'" width="'.$row['width'].'" height="'.$row['height'].'" alt="'.$row['image_name'].'">';
        print 
    "\n\n".'<br /><br /><a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?index">Back</a>';
        
        
        
    }else{
        
    echo <<<END

    Step 1: <a href="http://
    {$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?create">Create a MySQL table.</a><br />
    Step 2: <a href="http://
    {$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?upload">Upload an image.</a><br />
    Step 3: <a href="http://
    {$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?index">Show the image index</a><br />

    END;

    }

    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ill have a look thanks

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mmmmm I meant something like this.

    like the guy fills race1 race2 and the nicknames and uploads the file, file is uploaded to the server and the fields are created in the respective columns in the db. :S

  5. #5
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I wouldn't bother storing the file in the DB. Just details about it.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats what i meant pal, upload the file to the server and insert the details to the database


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •