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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error putting data into database

    Hi all,
    I have a form that uploads a file to my server and puts file names into a database, this works fine, but i want to ad a title to the form and have that saved to the database too. i have 5 upload slots so i need to loop through and save all titles, file names are ok.

    Hope someone can help

    Thanks

    <?php

    error_reporting(E_ALL);

    /*** the upload directory ***/
    $upload_dir= '../uploads_gallery';

    /*** numver of files to upload ***/
    $num_uploads = 5;

    /*** maximum filesize allowed in bytes ***/
    $max_file_size = 1251200;

    /*** the maximum filesize from php.ini ***/
    $ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
    $upload_max = $ini_max * 151024;

    /*** a message for users ***/
    $msg = 'Please select files for uploading';

    /*** an array to hold messages ***/
    $messages = array();

    /*** check if a file has been submitted ***/
    if(isset($_FILES['userfile']['tmp_name']))
    {
    /** loop through the array of files ***/
    for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
    {
    // check if there is a file in the array
    if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
    {
    $messages[] = 'No file uploaded';
    }
    /*** check if the file is less then the max php.ini size ***/
    elseif($_FILES['userfile']['size'][$i] > $upload_max)
    {
    $messages[] = "File size exceeds $upload_max php.ini limit";
    }
    // check the file is less than the maximum file size
    elseif($_FILES['userfile']['size'][$i] > $max_file_size)
    {
    $messages[] = "File size exceeds $max_file_size limit";
    }
    else
    {
    // copy the file to the specified dir
    if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i]))
    {
    /*** give praise and thanks to the php gods ***/
    $upfile = $_FILES['userfile']['name'][$i];
    $upfile = $_FILES['userfile']['name'][$i];
    $uptitle = $_POST['mytitle']['name'][$i];
    $uptag = $_FILES['userfile']['name'][$i];
    $upcat = $_FILES['userfile']['name'][$i];

    $messages[] = $_FILES['userfile']['name'][$i].' uploaded';

    }
    else
    {
    /*** an error message ***/
    $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
    }
    }
    $query = "REPLACE INTO gallery (image, thumb, title, tag, cat)
    VALUES ('$upfile', '$upfile', '$uptitle', '$uptag', '$upcat')";
    mysql_query($query) or die('Error, query failed');
    }
    }
    ?>


    <h3><?php echo $msg; ?></h3>
    <p>
    <?php
    if(sizeof($messages) != 0)
    {
    foreach($messages as $err)
    {
    echo $err.'<br />';
    }
    }
    ?>
    </p>
    <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
    <?php
    $num = 0;
    while($num < $num_uploads)
    {
    echo '<div><input name="mytitle" type="text" /> <input name="userfile[]" type="file" /></div>';
    $num++;
    }
    ?>
    Last edited by Peterjones; 01-10-2010 at 12:31 AM.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Please edit your post and wrap your code in appropriate tags as suggested by the post near the top of the forum and probably the editor page where you created this post.

    I think your problem is here:
    PHP Code:
     $uptitle $_POST['mytitle']['name'][$i]; 
    I guess you copied/pasted the other lines, so you have an extra array key in there. It should probably be something like:
    PHP Code:
     $uptitle $_POST['mytitle'][$i]; 
    EDIT: By the way, do you know you don't have a closing tag for the form?
    Are you a Help Vampire?


  •  

    Posting Permissions

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