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

Link to comment
https://forums.phpfreaks.com/topic/294306-need-help-upload-images/
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();
}
}
}
?>

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')

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);
}

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 . "')";

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?

No, not a joke. :)

 

You're looking for query errors here:

if (mysql_query($sql))
{
echo "success";
} else {
echo "error" . mysql_error();
}
So, you said that my query didn't work, and there is no errors? Try printing the $sql variable and posting the output.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.