SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    South-West UK
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Blob data update help needed

    Hello all.
    Just been adding a few modifications to the script found @: http://www.phpbuilder.com/columns/florian19991014.php3.
    I am trying to add an update script so that entries can be altered / ammended. I've discovered that updating blob data isn't as easy as I thought! Is it indeed possible is what I'm starting to ask myself now as I've so far been unable to find any tutorials / scripts to do such a thing.

    Here's my script so far:
    ---------------------------------------------------------
    <HTML>
    <HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
    <BODY>
    <?php
    if ($submit) {
    MYSQL_CONNECT("localhost","matt","r1ffraff" );
    mysql_select_db("test_matt" );
    $data = addslashes(fread(fopen($form_data, "r" ), filesize($form_data)));
    $result=MYSQL_QUERY("UPDATE binary_data SET(description,bin_data,filename,filesize,filetype) ".
    "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type') WHERE id='$id'" );

    print "<p>This file has the following Database ID: <b>$id</b>";
    MYSQL_CLOSE();
    } else {
    // else show the form to submit new data:
    ?>
    <?php
    $Hostname = "localhost";
    $User = "matt";
    $Password = "r1ffraff";
    $DBName = "test_matt";
    $TableName = "binary_data";
    $Link = mysql_connect($Hostname, $User, $Password);
    $Query = "Select * from $TableName WHERE id = '$id'";
    $Result = mysql_db_query ($DBName, $Query, $Link);

    ?>
    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Descriptionbr>
    <input type="text" name="form_description" size="40" value="<?php echo ($description); ?>">
    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in databasebr>
    <input type="file" name="form_data" size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>
    <?php
    }
    ?>
    </BODY>
    </HTML>
    -----------------------------------------------------------
    I know the code is a bit messy at the moment (and obviously broken somewhere), but can anyone point out where I'm going wrong?
    Any help would be greatly appreciated.
    Thanks
    Matt
    Last edited by mattyd22; May 14, 2003 at 01:37.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Kampala Uganda
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Re: Bugs

    Hi, There quite a number of bugs in your code slowly i will lay them out what i think is Right! :-
    1- In the code:-
    $result=MYSQL_QUERY("UPDATE binary_data SET(description,bin_data,filename,filesize,filetype) ".
    "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type') WHERE id='$id'" );


    SET was not supposed to have been used! Cases where you have Variables allone and table elements allone you need not SET:
    SET works for cases like:-

    $result=MYSQL_QUERY("UPDATE binary_data SET
    "description='$form_description',".
    "bin_data='$data',".
    "filename='$form_data_name',".
    "filesize='$form_data_size',".
    "filetype='$form_data_type' WHERE id='$id'";


    2:- Secondly in your Code the variable $id seems not to have been defined! IE. in your database nothing to be updated is defined! Better way of doing this is try asking the user which ID elements to update then use the provided variable as $id to update respectively. This statement
    $Query = "Select * from $TableName WHERE id = '$id'";
    Generates and Error since $id is not defined/passed.

    Those are some of the bugs i saw try likewise and that fails then get to me/send me a PM.

    Hope i was of importance to you!.
    Vien
    And we know that all things work together for good
    To them that love God, to them who are the called
    According to his purpose. Romans 8:28

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    South-West UK
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Vien,

    Thanks for your help so far, still it doesn't work though.

    I made a few ammendments to the code so here's the latest version.:

    The trouble is, the form is not even displaying now.

    ---------------------------------------------
    <HTML>
    <HEAD><TITLE>Update binary data in MySQL Database</TITLE></HEAD>
    <BODY>
    <?php
    if ($submit) {
    MYSQL_CONNECT("localhost","matt","r1ffraff");
    mysql_select_db("test_matt");
    $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
    $result=MYSQL_QUERY("UPDATE binary_data SET
    "description='$form_description',".
    "bin_data='$data',".
    "filename='$form_data_name',".
    "filesize='$form_data_size',".
    "filetype='$form_data_type' WHERE id='$id'");
    print "<p>This file has the following Database ID: <b>$id</b>";
    MYSQL_CLOSE();
    } else {
    // else show the form with the data that needs to be updated.

    $Hostname = "localhost";
    $User = "matt";
    $Password = "r1ffraff";
    $DBName = "test_matt";
    $TableName = "binary_data";
    $Link = mysql_connect($Hostname, $User, $Password);
    $Query = "Select * from $TableName WHERE id = '$id'"; // id is passed from previous page
    $Result = mysql_db_query ($DBName, $Query, $Link);
    while ($Row = mysql_fetch_array($Result)){

    print ("<form method=post action=store2.php enctype=multipart/form-data>");
    print ("<input type=text name=form_description value=$Row[description]>");
    print ("<INPUT TYPE=hidden name=MAX_FILE_SIZE value=1000000>");
    print ("<br>File to upload/store in databasebr>");
    print ("<input type=file name=form_data>");
    print ("<p><input type=submit name=submit value=submit>");
    print ("</form>");

    }
    }
    mysql_close($link);
    ?>
    </BODY>
    </HTML>
    ----------------------------------------------

    Any help will be greatly appreciated
    Thanks
    Matt

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <HTML>
    <HEAD><TITLE>Update binary data in MySQL Database</TITLE></HEAD>
    <BODY>
    <?php
    if ($submit) {

        
    mysql_connect ("localhost","matt","r1ffraff" );
        
    mysql_select_db("test_matt" );
        
    $data addslashes(fread(fopen($form_data"r" ), filesize($form_data)));
        
        
    $updatesql "
        UPDATE binary_data SET 
        description='
    $form_description', 
        bin_data='
    $data', 
        filename='
    $form_data_name', 
        filesize='
    $form_data_size',
        filetype='
    $form_data_type
        WHERE id='
    $id'
        "
    ;
        
    $result mysql_query ($updatesql);
        print 
    "<p>This file has the following Database ID: <b>$id</b>";
        
    mysql_close ();

    } else {
        
    // else show the form with the data that needs to be updated.

        
    $Hostname "localhost";
        
    $User "matt";
        
    $Password "r1ffraff";
        
    $DBName "test_matt";
        
    $TableName "binary_data";
        
    $Link mysql_connect ($Hostname$User$Password);
        
        
    $Query "
        SELECT * 
        FROM 
    $TableName 
        WHERE id = '
    $id'
        "
    // id is passed from previous page
        
    $Result mysql_db_query ($DBName$Query$Link);

        while (
    $Row mysql_fetch_array($Result)){

            print (
    "<form method=post action=store2.php enctype=multipart/form-data>" );
            print (
    "<input type=text name=form_description value=$Row[description]>" );
            print (
    "<INPUT TYPE=hidden name=MAX_FILE_SIZE value=1000000>" );
            print (
    "<br>File to upload/store in database:<br>" );
            print (
    "<input type=file name=form_data>" );
            print (
    "<p><input type=submit name=submit value=submit>" );
            print (
    "</form>" );

        }
        
    mysql_close($link);
    }
    ?>
    </BODY>
    </HTML>
    That's your code looking a bit better. The key point though is ... you say that // id is passed from previous page, but where in your code are you getting the ID value? I don't see it in the form or in a link.

    Final point - bad idea to assume the register_globals is On.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    South-West UK
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Siteguru and others above.

    All working now.

    You were right about the id field. I did need to pass it back to the script through a hidden form field.

    Matt


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
  •