Jump to content

Need help - Upload images


Hrvoje

Recommended Posts

Hi there, I need a little help...

 

I have simple form/code for upload image and insert it into db, and its working.

<form name="unos" action="" method="post" enctype="multipart/form-data">

<select name="kategorija">
<option value="1">Album</option>
</select>

<input type="file" name="uploaded_file[]" id="file" >

</form>
<?PHP
include "config.php";

$uploads_dir = 'slike';
foreach ($_FILES["uploaded_file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key];
        $name = $_FILES["uploaded_file"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

$sql="INSERT INTO galerija_slike (kategorija_id, url_slike) 
VALUES ('".$_POST['kategorija']."', '".$_FILES['uploaded_file']['name'][$key]."')";

if (mysql_query($sql))
{
	echo "success";
} else {
	echo "error" . mysql_error();
}
?>

How can I store more image url's from input fields?

When I add more input fields..... move_uploaded_file(): uploads all images, but I don't know how to also add them in db?

 

I need something like this:

<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >

Thanks

Edited by Hrvoje
Link to comment
Share on other sites

You can move your mysql query within the foreach loop

<?PHP
include "config.php";
$uploads_dir = 'slike';
foreach ($_FILES["uploaded_file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key];
        $name = $_FILES["uploaded_file"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
   
$sql="INSERT INTO galerija_slike (kategorija_id, url_slike)
VALUES ('".$_POST['kategorija']."', '".$_FILES['uploaded_file']['name'][$key]."')";
if (mysql_query($sql))
{
echo "success";
} else {
echo "error" . mysql_error();
}
}
}
?>
Link to comment
Share on other sites

You can move your mysql query within the foreach loop

No! You don't want to be executing SQL queries in a loop if at all possible.

 

Instead you can use batch insert syntax.

 

$insertValues = array();

$kategorija = mysql_real_escape_string($_POST['kategorija']);

foreach ($_FILES["uploaded_file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key];
        $name = $_FILES["uploaded_file"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");

        $url = mysql_real_escape_string($_FILES['uploaded_file']['name'][$key]);

        $insertValues[] = "('" . $kategorija . "', '" . $url . "'");
    }
}

$sql="INSERT INTO galerija_slike (kategorija_id, url_slike) 
VALUES " . implode(',', $insertValues);
This will produce a query that looks something like:
INSERT INTO galerija_slike (kategorija_id, url_slike) 
VALUES ('some category', 'some filename'),('some category', 'some other filename'),('some category', 'yet another filename')
Link to comment
Share on other sites

I agree, and along the way each category will have to be added to an array and used, unless will always be the same category all uploaded images.

 

Should also only insert if the  array is not empty

if(!empty($insertValues)){
$sql="INSERT INTO galerija_slike (kategorija_id, url_slike)
VALUES " . implode(',', $insertValues);
}
Link to comment
Share on other sites

Thanks for your reply.

 

QuickOldCar - Your code is working.

And csootstah, your code only upload images but nothing inserts in dba.

 

Here is pharse error, I correct it but nothing happend. Only upload images.

$insertValues[] = "('" . $kategorija . "', '" . $url . "'");
$insertValues[] = "('" . $kategorija . "', '" . $url . "')";
Edited by Hrvoje
Link to comment
Share on other sites

Thanks for your reply.

 

QuickOldCar - Your code is working.

And csootstah, your code only upload images but nothing inserts in dba.

 

Here is pharse error, I correct it but nothing happend. Only upload images.

$insertValues[] = "('" . $kategorija . "', '" . $url . "'");
$insertValues[] = "('" . $kategorija . "', '" . $url . "')";

 

Oops, sorry about the error.

 

You're checking for SQL errors, are there any?

Link to comment
Share on other sites

Oops, sorry about the error.

 

You're checking for SQL errors, are there any?

 

 

Is this is a joke? :)  I do not want to be misunderstood.

I apologize if you don't understand me.

 

There are no errors in SQL

Edited by Hrvoje
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.