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