Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP/SQL Insert not working

    Any ideas why the following image insert coding is not working, but giving me a success statement?

    States that the id is 0 everytime...

    Code:
    <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 ($submit) {
    
        // connect to the database
        // (you may have to adjust the hostname,username or password)
    
        MYSQL_CONNECT("10.8.11.72","CommBizGuide","Abcd1234");
        mysql_select_db("free_listing");
    
        $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
    
        $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,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();
    
    } else {
    
        // 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
    
    }
    
    ?>
    
    </body>
    </html>
    Thanks
    Last edited by tjkuster; 03-18-2009 at 04:01 PM. Reason: mis-type

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Where is your update query? Also read and follow http://www.codingforums.com/postguide.htm, especially the one about cross/duplicate posting (I think, it's a follow up of your other thread ).
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    From the man page for mysql_insert_id():

    Return Values

    The ID generated for an AUTO_INCREMENT column by the previous INSERT query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.
    So, you're missing an auto-increment field, or perhaps your query fails. Since you're not error-checking return values from the functions, who knows?
    Are you a Help Vampire?

  • #4
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Where are '$form_description','$data','$form_data_name','$form_data_size','$form_data_type' set???

    If they are set, try this:
    PHP Code:
    <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['submit'])) {

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

        
    MYSQL_CONNECT("10.8.11.72","CommBizGuide","Abcd1234");
        
    mysql_select_db("free_listing");

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

    $query "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) VALUES ('".$form_description."','".$data."','".$form_data_name."','".$form_data_size."','".$form_data_type."')";
    $sql mysql_query($query) or die(mysql_error());

        
    $idmysql_insert_id();
        echo 
    "<p>This file has the following Database ID: <b>".$id."</b>";

        
    MYSQL_CLOSE();

    } else {

        
    // 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

    }

    ?>

    </body>
    </html>


  •  

    Posting Permissions

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