Question

The code I have worked except, it's only saving the first filename in the database. I want to be able to save them under img_path in one row separated by commas or | --->

 img1.jpg, img2.jpg, img3.jpg

or

 img123.jpg|img456.jpg|img789.jpg

Form:

<input type="file" name="img[]" multiple="" />

Here's the full code

 <?php
 $con = mysql_connect( 'localhost', '', '') or die('Could not connect to mysql server.' );
   mysql_select_db('shop', $con) or die('Could not select database.');

 $product=mysql_real_escape_string($_POST['product']); 
 $description=mysql_real_escape_string($_POST['description']);
 $category=mysql_real_escape_string($_POST['category']); 
 $color=mysql_real_escape_string($_POST['color']); 
 $sizes=mysql_real_escape_string($_POST['sizes']); 
 $style=mysql_real_escape_string($_POST['style']); 
 $material=mysql_real_escape_string($_POST['material']); 
 $stock=mysql_real_escape_string($_POST['stock']); 
 $ws_price=mysql_real_escape_string($_POST['ws_price']); 
 $rt_price=mysql_real_escape_string($_POST['rt_price']);  
 $sp_code=mysql_real_escape_string($_POST['sp_code']); 


 foreach ($_FILES["img"]["error"] as $key => $error) {
 if ($error == UPLOAD_ERR_OK) {
    $tmp_name =$_FILES["img"]["tmp_name"][$key];
    //$tmp_name = implode(",", $_FILES["img"]["tmp_name"][$key]);
    $name = $_FILES["img"]["name"][$key];
    $rand = rand(0000,9999); 
    $px = $rand . $name;
    $px = preg_replace('/\s+/','_', $px);
    $px = str_replace('&', 'and', $px);
    $target = 'img/'.$category.'/';
        if (is_dir($target) == false) {
            mkdir($target, 0755);    
        echo "Directory Created</br>";
        } 
    $u = $target . $px;
    move_uploaded_file($tmp_name, $u);
    echo "". $px.", ";
    $a = array($px);
    $x = implode("|", $a);
}
 } 

 $sql = "INSERT INTO items (
    product, description,   category,   color,   sizes,   style,   material,   stock,   ws_price,   rt_price,   sp_code,   img_path
    )VALUES(
    '$product', '$description', '$category','$color','$sizes','$style','$material','$stock','$ws_price','$rt_price','$sp_code','$x'
    )";

 $result = mysql_query($sql);
 if($result){ echo("<br>Product Added!"); } 
 else{ echo("<br>Failed! Please try again.");}

 mysql_close($con);
 ?>

I've used this

$x = implode(',', $px)
$sql = "INSERT INTO items (img_path)VALUES('$x');";

and this

$sql = "INSERT INTO items (img_path)VALUES('". implode(',', $px)."')";

it would always give me an error.

 Warning: implode(): Invalid arguments passed in [...]
Was it helpful?

Solution

Here's the solution I've come up with taking some suggestions here.

Instead of using implode to save them in img_path in the same table, I've created a new one which is img to save the image path and link them using pid (product_id) from the products (id) table.

Database

 CREATE TABLE IF NOT EXISTS `img` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `img_path` varchar(255) NOT NULL, 
 `pid` int(10) DEFAULT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Here's the edited code (currently working... until I break it again XD):

<?php
$con=mysqli_connect("localhost","user","password","db");
if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }

$product=mysql_real_escape_string($_POST['product']); 
$description=mysql_real_escape_string($_POST['description']);
$category=mysql_real_escape_string($_POST['category']); 
$color=mysql_real_escape_string($_POST['color']); 
$sizes=mysql_real_escape_string($_POST['sizes']); 
$style=mysql_real_escape_string($_POST['style']); 
$material=mysql_real_escape_string($_POST['material']); 
$stock=mysql_real_escape_string($_POST['stock']); 
$ws_price=mysql_real_escape_string($_POST['ws_price']); 
$rt_price=mysql_real_escape_string($_POST['rt_price']);  
$sp_code=mysql_real_escape_string($_POST['sp_code']); 

$sql = "INSERT INTO products (product, description,   category,   color,   sizes,   style,   material,   stock,   ws_price,   rt_price,   sp_code)
    VALUES('$product',  '$description', '$category','$color','$sizes','$style','$material','$stock','$ws_price','$rt_price','$sp_code')";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
  } echo "<p>Product Added! </p>";

foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {

    $tmp_name =$_FILES["img"]["tmp_name"][$key];
    $name = $_FILES["img"]["name"][$key];
    $rand = rand(0000,9999); 
    $px = $rand . $name;
    $px = preg_replace('/\s+/','_', $px);
    $px = str_replace('&', 'and', $px);
    $px = str_replace("'", '-', $px);
    $target = 'img/'.$category.'/';
        if (is_dir($target) == false) {
            mkdir($target, 0755);    
        echo "Directory Created</br>";
        } 
    $u = $target . $px;
    move_uploaded_file($tmp_name, $u);
    echo "". $px."</br>";       
    mysqli_query($con,"INSERT INTO img (img_path, pid) VALUES ('$px', (SELECT MAX(id) FROM products))");
}
}       

mysqli_close($con);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top