Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the solution I've come up with taking some suggestions here.</p> <p>Instead of using implode to save them in img_path in the same table, I've created a new one which is <strong><em>img</em></strong> to save the image path and link them using pid (product_id) from the <strong><em>products</em></strong> (id) table.</p> <p>Database </p> <pre><code> 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 ; </code></pre> <p>Here's the edited code (currently working... until I break it again XD):</p> <pre><code>&lt;?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 "&lt;p&gt;Product Added! &lt;/p&gt;"; foreach ($_FILES["img"]["error"] as $key =&gt; $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('&amp;', 'and', $px); $px = str_replace("'", '-', $px); $target = 'img/'.$category.'/'; if (is_dir($target) == false) { mkdir($target, 0755); echo "Directory Created&lt;/br&gt;"; } $u = $target . $px; move_uploaded_file($tmp_name, $u); echo "". $px."&lt;/br&gt;"; mysqli_query($con,"INSERT INTO img (img_path, pid) VALUES ('$px', (SELECT MAX(id) FROM products))"); } } mysqli_close($con); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload