Created
March 3, 2021 13:14
-
-
Save SSmale/15b938159f98eb25d65e94f07a12feb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $page_title = "Stock Manager | Add a Product"; | |
| $page_topper = "Please Fill in all the Fields to Add a New Product"; | |
| include("inc/template/header.inc.php"); | |
| if ($_SERVER['REQUEST_METHOD'] == 'POST'&&isset($_POST['add_parent'])){ | |
| if(!empty($_POST['Name'])){ | |
| $name = safe_data($_POST['Name'],$dbc); | |
| if (strlen($name)>45){ | |
| $errors['Name'] = "Name is too long. Please enter a name under 45 characters"; | |
| } | |
| } | |
| else{ | |
| $errors['Name'] = "Name is empty"; | |
| } | |
| if(!empty($_POST['Description'])){ | |
| $desc = safe_data($_POST['Description'],$dbc); | |
| if (strlen($desc)>200){ | |
| $errors['Desc'] = "Description is too long. Please enter a description under 200 characters"; | |
| } | |
| } | |
| else{ | |
| $errors['Desc'] = "Description is empty"; | |
| } | |
| if(!empty($_POST['Catagory'])){ | |
| $cat=$_POST['Catagory']; | |
| } | |
| else{ | |
| $errors['Catagory'] = 'Please Select a Catagory'; | |
| } | |
| if(!empty($_POST['colour'])){ | |
| $colours=$_POST['colour']; | |
| } | |
| else{ | |
| $errors['Colour'] = 'Please Select a Colour(s)'; | |
| } | |
| if(!empty($_POST['size'])){ | |
| $sizes=$_POST['size']; | |
| } | |
| else{ | |
| $errors['Size'] = 'Please Select a Size(s)'; | |
| } | |
| if (empty($errors)){// no errors | |
| $q = "INSERT INTO Parent (Parent_Name, Parent_Description, FK_Cat_ID) VALUES ('$name','$desc',$cat)"; | |
| $r = mysqli_query($dbc, $q); | |
| echo mysqli_error($dbc); | |
| if(mysqli_affected_rows($dbc) == 1){ | |
| $q = "SELECT Parent_ID FROM Parent WHERE Parent_Name = '$name' LIMIT 1"; | |
| $r = mysqli_query($dbc, $q); | |
| if ($r){ | |
| while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){ | |
| $id=$row['Parent_ID']; | |
| } | |
| $q = "SELECT Colour_ID, Colour_Name FROM Colours"; | |
| $r = mysqli_query($dbc, $q); | |
| if ($r){ | |
| $ColourChart = array(); | |
| while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){ | |
| $ColourChart[$row['Colour_ID']] = $row['Colour_Name']; | |
| $_SESSION['ColourChart'] = $ColourChart; | |
| } | |
| } | |
| $q = "SELECT Size_ID, Size_Name FROM Sizes"; | |
| $r = mysqli_query($dbc, $q); | |
| if ($r){ | |
| $SizeChart = array(); | |
| while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){ | |
| $SizeChart[$row['Size_ID']] = $row['Size_Name']; | |
| $_SESSION['SizeChart'] = $SizeChart; | |
| } | |
| } | |
| echo '<form action="add_product.php" method="post"><input type="hidden" name="ID" value="' . $id. '"><table style="border-spacing: 4px;border-collapse:separate;border: 1px solid black;">' . "\n" . '<tr><th>Product Name</th><th>Barcode</th><th>Weight (Kg)</th><th>Size</th><th>Colour</th><th>Stock</th><th>Cost</th><th>Retail Price</th></tr>' . "\n"; | |
| $row = 0; | |
| foreach ($colours as $c){ | |
| foreach ($sizes as $s){ | |
| echo "\n" . '<tr><td><input type="hidden" name="Row" value="' . $row. '"><input type="hidden" name="Name" value="' . $name. '">' . $name . '</td><td><input type="text" name="Barcode[]" placeholder="EAN-13 Barcode" required></td><td><input type="text" name="Weight[]" placeholder="Weight in Kg" required></td><td><input type="hidden" name="Size[]" value="' . $s. '">' . $SizeChart[$s] . '</td><td><input type="hidden" name="Colour[]" value="' . $c. '">' . $ColourChart[$c] . '</td><td><input type="number" name="Stock[]" min="0" max="65535" placeholder="Stock Held" required></td><td><input type="text" name="Cost[]" placeholder="Cost Price (£)" required></td><td><input type="text" name="Retail[]" placeholder="Purchasse Price (£)" required></td></tr>' . "\n"; | |
| $row++; | |
| } | |
| } | |
| } | |
| else{ | |
| echo 'Connection error'; | |
| } | |
| } | |
| else{ | |
| echo 'Connection error'; | |
| } | |
| echo "\n " . '</table><input type="submit" name="add_children" value="Add Products"></form>' . "\n"; | |
| include("inc/template/footer.inc.php"); | |
| exit(); | |
| } | |
| }elseif($_SERVER['REQUEST_METHOD'] == 'POST'&&isset($_POST['add_children'])){ | |
| $id = $_POST['ID']; | |
| for ($i = 0; $i<=$_POST['Row']; $i++){ | |
| $size = $_POST['Size'][$i]; | |
| $colour = $_POST['Colour'][$i]; | |
| if(!empty($_POST['Barcode'][$i])){ | |
| $bc = safe_data($_POST['Barcode'][$i],$dbc); | |
| if (!preg_match("/^(\d{13})$/",$bc)){ | |
| $errors['Barcode'] = "Barcode format is incorrect. Please enter an EAN-13 barcode"; | |
| } | |
| else{ | |
| $q = "SELECT Barcode FROM sosa WHERE Barcode = $bc"; | |
| $r = mysqli_query($dbc, $q); | |
| if (mysqli_num_rows($r)>0){ | |
| $errors['Barcode'] = "Barcode already in use."; | |
| } | |
| } | |
| } | |
| else{ | |
| $errors['Barcode'] = "Barcode is empty"; | |
| } | |
| if(!empty($_POST['Stock'][$i])){ | |
| $stock = safe_data($_POST['Stock'][$i],$dbc); | |
| if ($stock>65535){ | |
| $errors['Stock'] = "Stock value is too high. Please enter a number between 0-1000"; | |
| } | |
| } | |
| else{ | |
| $errors['Stock'] = "Stock is empty"; | |
| } | |
| if(!empty($_POST['Cost'][$i])){ | |
| $cost = safe_data($_POST['Cost'][$i],$dbc); | |
| if (!preg_match('/^[0-9]+(\.[0-9]{1,2})?$/', $cost)){ | |
| $errors['Cost'] = "Cost formatt is incorrect. Please enter a valid number"; | |
| } | |
| } | |
| else{ | |
| $errors['Cost'] = "Cost is empty"; | |
| } | |
| if(!empty($_POST['Retail'][$i])){ | |
| $retail = safe_data($_POST['Retail'][$i],$dbc); | |
| if (!preg_match('/^[0-9]+(\.[0-9]{1,2})?$/', $retail)){ | |
| $errors['Retail'] = "Retail formatt is incorrect. Please enter a valid number"; | |
| } | |
| } | |
| else{ | |
| $errors['Retail'] = "Retail is empty"; | |
| } | |
| if(!empty($_POST['Weight'][$i])){ | |
| $weight = safe_data($_POST['Weight'][$i],$dbc); | |
| if ($weight>1000){ | |
| $errors['Weight'] = "Weight value is too high. Please enter a number between 0-1000"; | |
| } | |
| } | |
| else{ | |
| $errors['Weight'] = "Weight is empty"; | |
| } | |
| if (!empty($errors)){//Add errors to the global error list. | |
| $form_errors[$i] = $errors; | |
| } | |
| $children[$i] = array('bc'=>$bc, 'size'=>$size, 'colour'=>$colour, 'stock'=>$stock, 'cost'=>$cost, 'retail'=>$retail, 'weight'=>$weight); | |
| } | |
| if (empty($form_errors)){ | |
| $added = 0; | |
| foreach($children as $child){ | |
| $bc=$child['bc']; | |
| $size=$child['size']; | |
| $$colour=$child['colour']; | |
| $stock=$child['stock']; | |
| $cost=$child['cost']; | |
| $retail=$child['retail']; | |
| $weight=$child['weight']; | |
| $q = "INSERT INTO Child (FK_Size, FK_Colour, Weight, Barcode, Cost, Price, FK_Parent_ID, Stock) VALUES ($size,$colour,$weight,$bc,$cost,$retail ,$id,$stock)"; | |
| $r = mysqli_query($dbc, $q); | |
| if (mysqli_affected_rows($dbc) == 1){ | |
| $added++; | |
| } | |
| else{ | |
| echo 'Error'; | |
| } | |
| } | |
| echo '<p><h3>' . $added . ' Product(s) Added</h3></p>'; | |
| } | |
| else{//Errors within the forms. | |
| $name=$_POST['Name']; | |
| echo '<form action="add_product.php" method="post"><input type="hidden" name="Name" value="' . $id. '"><table style="border-spacing: 4px;border-collapse:separate;border: 1px solid black;">' . "\n" . '<tr><th>Product Name</th><th>Barcode</th><th>Weight (Kg)</th><th>Size</th><th>Colour</th><th>Stock</th><th>Cost</th><th>Retail Price</th></tr>' . "\n"; | |
| $row = 0; | |
| foreach ($children as $child){ | |
| echo "\n" . '<tr><td><input type="hidden" name="Row" value="' . $row. '"><input type="hidden" name="Name" value="' . $name. '">' . $name . '</td><td><input type="text" name="Barcode[]" placeholder="EAN-13 Barcode" ' . ((isset($child['bc'])) ? 'value="'.$child['bc'].'"' : '') . ' required>' . ((!empty($form_errors[$row]['Barcode'])) ? $form_errors[$row]['Barcode'] : '') . '</td><td><input type="text" name="Weight[]" placeholder="Weight in Kg" ' . ((isset($child['weight'])) ? 'value="'.$child['weight'].'"' : '') . ' required>' . ((!empty($form_errors[$row]['Weight'])) ? $form_errors[$row]['Weight'] : '') . '</td><td><input type="hidden" name="Size[]" value="' . $child['size']. '">' . $_SESSION['SizeChart'][$child['size']] . '</td><td><input type="hidden" name="Colour[]" value="' . $child['colour']. '">' . $_SESSION['ColourChart'][$child['colour']] . '</td><td><input type="number" name="Stock[]" min="0" max="65535" placeholder="Stock Held" ' . ((isset($child['stock'])) ? 'value="'.$child['stock'].'"' : '') . ' required>' . ((!empty($form_errors[$row]['Stock'])) ? $form_errors[$row]['Stock'] : '') . '</td><td><input type="text" name="Cost[]" placeholder="Cost Price (£)" ' . ((isset($child['cost'])) ? 'value="'.$child['cost'].'"' : '') . ' required>' . ((!empty($form_errors[$row]['Cost'])) ? $form_errors[$row]['Cost'] : '') . '</td><td><input type="text" name="Retail[]" placeholder="Purchasse Price (£)" ' . ((isset($child['retail'])) ? 'value="'.$child['retail'].'"' : '') . ' required>' . ((!empty($form_errors[$row]['Retail'])) ? $form_errors[$row]['Retail'] : '') . '</td></tr>' . "\n"; | |
| $row++; | |
| } | |
| echo "\n " . '</table><input type="submit" name="add_children" value="Add Products"></form>' . "\n"; | |
| include("inc/template/footer.inc.php"); | |
| exit(); | |
| }//END Error else | |
| } | |
| $q = "SELECT Catagory_Name, Catagory_ID FROM Catagory"; | |
| $r = mysqli_query($dbc, $q); | |
| echo '<form action="add_product.php" method="post">' . "\n"; | |
| echo '<p>Product Name:<input type="text" name="Name" placeholder="Product Name" ' . ((isset($_POST['Name'])) ? 'value="'.$_POST['Name'].'">': '>') . '</p>' . "\n"; | |
| if ($errors){echo $errors['Name'];} | |
| echo '<p>Product Description:<input type="text" name="Description" placeholder="Product Description" ' . ((isset($_POST['Description'])) ? 'value="'.$_POST['Description'].'">': '>') . '</p>' . "\n"; | |
| if ($errors){echo $errors['Desc'];} | |
| echo '<p>Catagory:<select name="Catagory"></p>' . "\n"; | |
| if ($errors){echo $errors['Catagory'];} | |
| if ($r){ | |
| while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){ | |
| echo '<option value="' . $row['Catagory_ID'] . '"' . ((isset($_POST['Catagory'])&&$_POST['Catagory']== $row['Catagory_ID']) ? "selected" : '') . '>' . $row['Catagory_Name'] . '</option>' . "\n"; | |
| } | |
| } | |
| else{ | |
| echo 'Connection error'; | |
| } | |
| echo '</select>' . "\n"; | |
| echo '<div class="col-md-3 col-sm-6 highlight"><h4>Select Sizes</h4><p>(1 or more)</p>'; | |
| if ($errors){echo $errors['Size'];} | |
| $q = "SELECT Size_ID, Size_Name FROM Sizes"; | |
| $r = mysqli_query($dbc, $q); | |
| if ($r){ | |
| while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){ | |
| echo '<input type="checkbox" name="size[]" value="' . $row['Size_ID'] . '"' . (((isset($_POST['size']))&&(in_array($row['Size_ID'],$_POST['size']))) ? 'checked' : '') . '>' . $row['Size_Name'] . '<br>'; | |
| } | |
| } | |
| else{ | |
| echo 'Connection error'; | |
| } | |
| echo '</div><div class="col-md-3 col-sm-6 highlight"><h4>Select Colours</h4><p>(1 or more)</p>'; | |
| if ($errors){echo $errors['Colour'];} | |
| $q = "SELECT Colour_ID, Colour_Name FROM Colours"; | |
| $r = mysqli_query($dbc, $q); | |
| if ($r){ | |
| while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){ | |
| echo '<input type="checkbox" name="colour[]" value="' . $row['Colour_ID'] . '"' . (((isset($_POST['colour']))&&(in_array($row['Colour_ID'],$_POST['colour']))) ? 'checked' : '') . '>' . $row['Colour_Name'] . '<br>'; | |
| } | |
| } | |
| else{ | |
| echo 'Connection error'; | |
| } | |
| echo '</div><div><p><input type="submit" name="add_parent" value="Add"></p></div></form>' . "\n"; | |
| include("inc/template/footer.inc.php"); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment