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 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    uploading an image and inserting values at the same time

    I am trying to insert values into a datbase on products and upload an image of it at the same time. I am able to insert all of the values in no problem when i do not try to upload and I can upload the image if I just click on browse and upload and not insert other values. The reason I am running into problems is because I used a seperate script for my upload to that of the insert. My upload code is in a file called doupload.php

    Code:
    <?php
    
    $file_dir = '/home/c/ciaracousins/public_html/clothes/';
    // Get the image..
    $image = $HTTP_POST_FILES['image'];
    // Verify for erros
    if ( $image['error'] == 0 )
    {
        // Ok, next: is it uploaded?
        if ( is_uploaded_file ( $image['tmp_name'] ))
        {
            // Copy it to folder and check
            if ( move_uploaded_file ( $image['tmp_name'] , $file_dir . $image['name'] ))
            {
                // Ok, done
                # do something here, use header.. java redirection.. don't know, use your imagination..
                print 'Done! The file was uploaded!';
            }
        }
    }
    ?>
    And my insert code code is in a file called admin_insert.php

    Code:
    <?
    include "db.php";
    
    $submit=$HTTP_POST_VARS["submit"];
    $reset=$HTTP_POST_VARS["reset"];
     
    $shopName=$HTTP_POST_VARS["shopName"];
    $prodName=$HTTP_POST_VARS["prodName"];
    $dept=$HTTP_POST_VARS["dept"];
    $brand=$HTTP_POST_VARS["brand"];
    $type=$HTTP_POST_VARS["type"];
    $price=$HTTP_POST_VARS["price"];
    $NoInStock=$HTTP_POST_VARS["NoInStock"];
    $image=$HTTP_POST_VARS["image"];
     
    
    if (!$shopName || !$prodName || !$dept || !$brand || !$type 
    	|| !$price || !$NoInStock || !$image){
    	echo "Please fill all the fields";
    	exit;
    
    
    }
    
    
    /* Performing SQL query */
    
    $query = "select * from product where prodName = '$prodName'";
    
    //$result = mysql_query($query,$connect);
    
    require_once('db.php');
    
    
    	$query = "INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type', '$image', '$price', '$NoInStock')";
    	$result = mysql_query($query);
    
    	if ($result){
    		echo "data inserted";
    		
    	}
    	else{
    		echo "This product has already been entered !!!";
    	}
    	/* Free resultset */
    	
    
    	/* Closing connection */
    	mysql_close();
    
    ?>
    What I want to do is to combine the two of these so that when I click on the "Add record" button on my admin_insert.htm page, the image will upload to the server and the values entered will be stored to the database.

    admin_insert.htm:

    Code:
    <form action="admin_insert.php" enctype="multipart/form-data" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="51200" />
    
     <tr>
        <td align="center"><p style="color: #FF6600; font size=24"><strong>Add New Records</strong></p></td></tr>
      <tr><td></td></tr>
    <table align="left" width="100%" border="0">
     
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Shop Name:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="shopName" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Product Name:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="prodName" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Department:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="dept" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>brand:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="brand" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Type:</strong>
        </td>
        <td>
          <input maxLength="45" size="45" name="type" />
        </td>
      </tr>
    <tr>
        <td align="right" style="color: #006699;">
          <strong>Price:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="price" />
        </td>
      </tr>
    <tr>
        <td align="right" style="color: #006699;">
          <strong>Quantity:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="NoInStock" />
        </td>
      </tr>
    
    <!--<tr>
        <td align="right" style="color: #006699;">
          <strong>File to Upload:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="image" />
        </td>
      </tr>-->
    
     <tr>
        <td align="right" style="color: #006699;">
          <strong>File to Upload:</strong>
        </td>
        <td>
          <input type="text" name="image" />
          <!--<input type="submit" value="Upload!" />-->
        </td>
      </tr>
      <tr>
        <td></td>
        <td align="left" colspan="2">
          <input type="submit" value="Add Record" name=submit />
          <input type=reset value=Reset name=reset />
        </td>
      </tr>
    </table>
    </form>
    			
    
                          
                        </TBODY>
                      </TABLE>
                    </FORM>
    I am also having problems with the upload part. If I have

    <input type="file" name="image" />

    it keeps saying to enter all fields. It is not recognising what is being entered because image is set to be a text value in the database, because I am not storing the image in the db, I am trying to upload the image to the server and then just store its path in the db.

    Can anyone help me with this?
    Thanks

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    your "please fill all the fields" error is because $image is being told to find $_POST['image']..... the "image" field should be called $_FILES['image']

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    try keeping the form html in a separate file and combine the rest like below

    admin_insert.htm
    Code:
    <form action="admin_insert.php" enctype="multipart/form-data" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="51200" />
    
     <tr>
        <td align="center"><p style="color: #FF6600; font size=24"><strong>Add New Records</strong></p></td></tr>
      <tr><td></td></tr>
    <table align="left" width="100%" border="0">
     
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Shop Name:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="shopName" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Product Name:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="prodName" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Department:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="dept" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>brand:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="brand" />
        </td>
      </tr>
      <tr>
        <td align="right" style="color: #006699;">
          <strong>Type:</strong>
        </td>
        <td>
          <input maxLength="45" size="45" name="type" />
        </td>
      </tr>
    <tr>
        <td align="right" style="color: #006699;">
          <strong>Price:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="price" />
        </td>
      </tr>
    <tr>
        <td align="right" style="color: #006699;">
          <strong>Quantity:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="NoInStock" />
        </td>
      </tr>
    
    <!--<tr>
        <td align="right" style="color: #006699;">
          <strong>File to Upload:</strong>
        </td>
        <td>
          <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="image" />
        </td>
      </tr>-->
    
     <tr>
        <td align="right" style="color: #006699;">
          <strong>File to Upload:</strong>
        </td>
        <td>
          <input type="text" name="image" />
          <!--<input type="submit" value="Upload!" />-->
        </td>
      </tr>
      <tr>
        <td></td>
        <td align="left" colspan="2">
          <input type="submit" value="Add Record" name=submit />
          <input type=reset value=Reset name=reset />
        </td>
      </tr>
    </table>
    </form>
    admin_insert.php
    PHP Code:
    <?php
    if(empty($_POST['MAX_FILE_SIZE'])){
    include(
    "admin_insert.htm");
    }elseif(isset(
    $_POST['MAX_FILE_SIZE'])){
    include(
    "db.php");

    $submit=$HTTP_POST_VARS["submit"];
    $reset=$HTTP_POST_VARS["reset"];
     
    $shopName=$HTTP_POST_VARS["shopName"];
    $prodName=$HTTP_POST_VARS["prodName"];
    $dept=$HTTP_POST_VARS["dept"];
    $brand=$HTTP_POST_VARS["brand"];
    $type=$HTTP_POST_VARS["type"];
    $price=$HTTP_POST_VARS["price"];
    $NoInStock=$HTTP_POST_VARS["NoInStock"];
    $image=$HTTP_POST_FILES["image"];
     

    if (!
    $shopName || !$prodName || !$dept || !$brand || !$type 
        
    || !$price || !$NoInStock || !$image){
        echo 
    "Please fill all the fields";
        include(
    "admin_insert.htm");
        exit;
    }


    /* Performing SQL query */

    $check_existence mysql_query("select * from product where prodName = '$prodName'") or die(mysql_error());

    //$result = mysql_query($query,$connect);
        
    if(mysql_num_rows($check_existence) >0){
            echo 
    "This product has already been entered !!!";
        }else{
            
    mysql_query("INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type', '$image', '$price', '$NoInStock')") or die(mysql_error());
            echo 
    "data inserted";
        }
        
    /* Free resultset */
        

        /* Closing connection */
        
    mysql_close();
        
        
    $file_dir '/home/c/ciaracousins/public_html/clothes/';
        
    // Get the image..
        
    $image $HTTP_POST_FILES['image'];
        
    // Verify for erros
        
    if ( $image['error'] == )
        {
            
    // Ok, next: is it uploaded?
            
    if ( is_uploaded_file $image['tmp_name'] ))
            {
            
    // Copy it to folder and check
            
    if ( move_uploaded_file $image['tmp_name'] , $file_dir $image['name'] ))
            {
                
    // Ok, done
                # do something here, use header.. java redirection.. don't know, use your imagination..
                
    print 'Done! The file was uploaded!';
            }
        }
    }
    }
    ?>

  • #4
    New Coder
    Join Date
    Apr 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! Its now inserting all the values, only when I look at what is stored for image is just says 'array' rather than the actual path for the image.

    What can I do to change that?

    Also the image isnt actually uploaded on the server when I check my clothes folder where I have everything stored. It was working ok when I had it in its own page doupload.php so is there some more changes need to be made? It displays the message the file was uploaded but there is nothing being stored
    Last edited by ciaracous; 08-22-2006 at 04:54 PM.

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    mysql_query("INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type', '".$file_dir.$image['name']."', '$price', '$NoInStock')") or die(mysql_error());

  • #6
    New Coder
    Join Date
    Apr 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yay i got it working! Thank you!!


  •  

    Posting Permissions

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