-
Posts
110 -
Joined
-
Last visited
Everything posted by bullbreed
-
Lets say I have a product (T-Shirt) that has 5 different designs available in 5 colours each design ; e.g. Design 1 - Black - White - Blue - Red - Pink Design 2 - Black - White - Blue - Red - Pink Design 3 - Black - White - Blue - Red - Pink Design 4 - Black - White - Blue - Red - Pink Design 5 - Black - White - Blue - Red - Pink The user will select a design and then the colour and be shown the T-Shirt in the selected colour The default for Design 1 will be Black. Clicking on a Red Colour Square will load the Red T-Shirt in place of the Black T-Shirt How would these products be best inserted in to the database e.g. Should Design 1 have 5 images uploaded to its row in the database Should I have a different table for all the shirt designs and JOIN them
-
Upload image to directory and store url of image in database
bullbreed replied to bullbreed's topic in PHP Coding Help
Well I have a lot of other data stored with that product as well as the image. Here is my code for this page <div id="pagecont"> <div id="leftcol"> <div class="navtop">Navigation</div> <?php include('includes/side-nav.php'); ?> </div> <div id="rightcol"> <?php if (isset($_POST['submit'])) { $submit = $_POST['submit']; //Form data $existprodcat = mysql_real_escape_string($_POST['existprod_cat']); $newprodcat = mysql_real_escape_string($_POST['newprod_cat']); $prodname = mysql_real_escape_string($_POST['prod_name']); $prodptitle = mysql_real_escape_string($_POST['prod_ptitle']); $prodpdescription = mysql_real_escape_string($_POST['prod_pdescription']); $prodpkeywords = mysql_real_escape_string($_POST['prod_pkeywords']); $pcolours = mysql_real_escape_string(serialize($_POST['pcolours'])); $psizes = mysql_real_escape_string(serialize($_POST['psizes'])); $prodprice = mysql_real_escape_string($_POST['prod_price']); $proddesc = mysql_real_escape_string($_POST['prod_desc']); $date = date('d/m/Y \a\t g:i.s a'); //Open the database mysql_connect("localhost","root",""); mysql_select_db("***********"); //Select the database if ($existprodcat !== ""){ $pcat = $existprodcat; }else{ $pcat = $newprodcat; } // Check for a front image. if ((($_FILES["pfrontimage"]["type"] == "image/gif") || ($_FILES["pfrontimage"]["type"] == "image/jpeg") || ($_FILES["pfrontimage"]["type"] == "image/pjpeg")) && ($_FILES["pfrontimage"]["size"] < 20000)) { if ($_FILES["pfrontimage"]["error"] > 0) { echo "Return Code: " . $_FILES["pfrontimage"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["pfrontimage"]["name"] . "<br />"; echo "Type: " . $_FILES["pfrontimage"]["type"] . "<br />"; echo "Size: " . ($_FILES["pfrontimage"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["pfrontimage"]["tmp_name"] . "<br />"; if (file_exists("uploads/" . $_FILES["pfrontimage"]["name"])) { echo $_FILES["pfrontimage"]["name"] . " This image already exists. "; } else { move_uploaded_file($_FILES["pfrontimage"]["tmp_name"], "uploads/" . $_FILES["pfrontimage"]["name"]); echo "Stored in: " . "uploads/" . $_FILES["pfrontimage"]["name"]; mysql_query("INSERT INTO `customfight`.`products` (`prod_fimage`) VALUES ('uploads/".$_FILES["pfrontimage"]["name"]."')") or die(mysql_error()); } } } else { echo "Invalid file"; } // Check for a back image. if ((($_FILES["pbackimage"]["type"] == "image/gif") || ($_FILES["pbackimage"]["type"] == "image/jpeg") || ($_FILES["pbackimage"]["type"] == "image/pjpeg")) && ($_FILES["pbackimage"]["size"] < 20000)) { if ($_FILES["pbackimage"]["error"] > 0) { echo "Return Code: " . $_FILES["pfrontimage"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["pbackimage"]["name"] . "<br />"; echo "Type: " . $_FILES["pbackimage"]["type"] . "<br />"; echo "Size: " . ($_FILES["pbackimage"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["pbackimage"]["tmp_name"] . "<br />"; if (file_exists("uploads/" . $_FILES["pbackimage"]["name"])) { echo $_FILES["pbackimage"]["name"] . " This image already exists. "; } else { move_uploaded_file($_FILES["pbackimage"]["tmp_name"], "uploads/" . $_FILES["pbackimage"]["name"]); echo "Stored in: " . "uploads/" . $_FILES["pbackimage"]["name"]; mysql_query("INSERT INTO `customfight`.`products` (`prod_bimage`) VALUES ('uploads/".$_FILES["pbackimage"]["name"]."')") or die(mysql_error()); } } } else { echo "Invalid file"; } //Check for existing fields if($prodname && $pcat && $prodptitle && $prodpdescription && $prodpkeywords && $pcolours && $psizes && $prodprice && $proddesc && $date){ $pagecheck = mysql_query("SELECT prod_name FROM products WHERE prod_name = '$prodname'");echo mysql_error(); $count = mysql_num_rows($pagecheck); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">That product already exists. Please edit the existing product!</font>");echo mysql_error(); }else{ //Enter into Database $queryreg = mysql_query("INSERT INTO `********`.`products` (`id`, `prod_cat`, `prod_name`, `prod_ptitle`, `prod_pdescription`, `prod_pkeywords`, `prod_colours`, `prod_sizes`, `prod_price`, `prod_desc`, `date`) VALUES (NULL, '$pcat', '$prodname', '$prodptitle', '$prodpdescription', '$prodpkeywords', '$pcolours', '$psizes', '$prodprice', '$proddesc', '$date')");echo mysql_error(); echo("<font size=\"2\" color=\"#00cc00\">Your product has been created! </font>");echo mysql_error(); } }else{ echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");echo mysql_error(); } } //end Check for existing fields //end if submit $sql = "SELECT * FROM `products`"; $query = mysql_query($sql);echo mysql_error(); ?> <div class="conttop">Add a product</div> <div class="contdata"> <form action="addproduct.php" method="post" enctype="multipart/form-data" name="addpage" target="_self" id="addpage"> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap">Product Name</div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap">Create product Category</div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap">Select existing category</div> </div> <div class="genwrap"> <div class="titlewrap"> <input name="prod_name" type="text" id="prod_name" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"> <input name="newprod_cat" type="text" id="newprod_cat" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("********"); //Select the database $sql = "SELECT `prod_cat` FROM products"; $query = mysql_query($sql) or die(mysql_error()); $sections = array(); while ($row = mysql_fetch_array($query)){ $sections[] = $row[0]; } $sections = array_unique($sections); ?> <select name="existprod_cat" id="existprod_cat"> <option selected></option> <?php foreach($sections as $section){ echo '<option value="'.$section.'">'.$section.'</option>'; } ?> </select> </div> </div> <div class="clear"></div></div> <div class="conttop">Meta Data</div> <div class="contdata"> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap">Title</div> </div> <div class="genwrapmeta"><input name="prod_ptitle" id="prod_ptitle" type="text" size="80" maxlength="100" /></div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap">Description</div> </div> <div class="genwrapmeta"><input name="prod_pdescription" type="text" id="prod_pdescription" size="80" maxlength="200" /></div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap">Keywords</div> </div> <div class="genwrapmeta"><input name="prod_pkeywords" type="text" id="prod_pkeywords" size="80" maxlength="200" /></div> <div class="clear"></div> </div> <div class="conttop">Product Front Image</div> <div class="contdata"><input name="pfrontimage" type="file" /> </div> <div class="conttop">Product Back Image</div> <div class="contdata"><input name="pbackimage" type="file" /></div> <div class="conttop">Product Sizes</div> <div class="contdata"> <label>S1</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S2</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S3</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S4</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S5</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S6</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S7</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <br /><br /> <label>S8</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S9</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S10</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <div class="clear"></div> </div> <div class="conttop">Product Colour</div> <div class="contdata"> <div class=" colourswrap"> <div class="colours"><img src="uploads/black.jpg" alt="Black" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="uploads/black.jpg" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/white.jpg" alt="White" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="uploads/white.jpg" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/blue.jpg" alt="Blue" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="uploads/blue.jpg" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/red.jpg" alt="Red" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="uploads/red.jpg" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/pink.jpg" alt="Pink" width="22" height="22" /></div> <br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="uploads/pink.jpg" /> </div><div class="clear"></div> </div> <div class="conttop">Product Price</div> <div class="contdata"><input type="text" name="prod_price" id="prod_price" /> <div class="clear"></div> </div> <div class="conttop">Page Content</div> <div class="contdata"> <textarea name="prod_desc" cols="80" rows="20" id="prod_desc"></textarea> <br /> <input type="submit" value="Save" name="submit" /> </div> </form> </div><div class="clear"></div></div> I thought maybe store the uploads/" . $_FILES["pbackimage"]["name"] as a variable like $pfrontimage = my_real_escape_string (uploads/" . $_FILES["pbackimage"]["name"]); (not sure if I have to use my real escape string) and do the same for the back image then I could INSERT into with the rest at the same time at the //Enter into database part of the script. I would think that this would add them to the correct row associated with that product. Just unsure how to call that into a variable with the $FILES in there -
Upload image to directory and store url of image in database
bullbreed replied to bullbreed's topic in PHP Coding Help
Thanks for that it make a lot of sense. However when I do that it creates a new row in the table to store only the url. I would like it to be stored in a row that has all the other data that goes with the product so; How do I store the result in to a variable so I can add it to the database further down the script with the rest? $pfrontimage = mysql_real_escape_string(uploads/".$_FILES["pbackimage"]["name"]."); It doesnt look right and i'm new. -
Hi everyone. I'm trying to upload an image and move it to a folder in the root directory of my server which I have done (wahoo); However, when I upload the image to the folder I would also like to store the url of that image into the database; Heres my code for uploading and moving the image // Check for a front image. if(isset($_FILES['pfrontimage'])){ if (is_uploaded_file ($_FILES['pfrontimage']['tmp_name'])) { if (move_uploaded_file($_FILES['pfrontimage']['tmp_name'], "uploads/{$_FILES['pfrontimage']['name']}")) { // Move the file over. echo '<p>The front image has been uploaded!</p>'; } else { // Couldn't move the file over. echo '<p><font color="red">The image could not be moved.</font></p>'; $i = FALSE; } $i = $_FILES['pfrontimage']['name']; } else { $i = FALSE; } } What do I add to this code that will put the link to the image into the database table.
-
Hi and thank you very much for your reply. The images don't upload to the database, they upload to a folder in the server directory. What I can't seem to achieve at the moment is put the string with the url to new image location in your database. How would you do that please.
-
ok so heres what i did. I thought that because the $pfrontimage and $pbackimage were actually Input File Fields so you can select an image from your local computer to upload in to a folder on the server, I decided to remove the variables altogether because the File Upoad and Move script on the page will upload it to the folder on the server and it all works. However, I obviously don't get a value in the database for the image because the image file is just sent straight to the uploads folder. Would this be the correct ay of doing it because I thought that if I were to; SELECT * FROM wherever to show the user all the data related to that product they wont get to see the images of that product.
-
Should I be declaring he $pfrontimage as a variable from the submitted form data if the submitted data is a file?
-
Ah right pfrontimage and pbackimage are indeed image files and because I am new to php I thought I had to use my_real_escape_string. What i'm trying to do is upload a front image and a back image of a product but the code suggests that I havent filled in all the form in the form. But when I delete all references to the images the form submits o the database. Not sure what to do here because the file upload bit works and puts the images in to a file called uploads so I know the form is being filled in
-
Hi Does anyone know why I get the following notice; Notice: Undefined variable: pfrontimage in C:\xampp\htdocs\admin\addproduct2.php on line 116 Also when I submit the form I get the "Please complete all fields" message. But if I remove any referrence to the images including the variables, file uplead and form fields the form submits to the database ok. <?php if (isset($_POST['submit'])) { $submit = $_POST['submit']; //Form data $existprodcat = mysql_real_escape_string($_POST['existprod_cat']); $newprodcat = mysql_real_escape_string($_POST['newprod_cat']); $prodname = mysql_real_escape_string($_POST['prod_name']); $prodptitle = mysql_real_escape_string($_POST['prod_ptitle']); $prodpdescription = mysql_real_escape_string($_POST['prod_pdescription']); $prodpkeywords = mysql_real_escape_string($_POST['prod_pkeywords']); if (isset($_POST['pfrontimage'])) { $pfrontimage = mysql_real_escape_string($_POST['pfrontimage']); } if (isset($_POST['pbackimage'])) { $pbackimage = mysql_real_escape_string($_POST['pbackimage']); } $pcolours = mysql_real_escape_string(serialize($_POST['pcolours'])); $psizes = mysql_real_escape_string(serialize($_POST['psizes'])); $prodprice = mysql_real_escape_string($_POST['prod_price']); $proddesc = mysql_real_escape_string($_POST['prod_desc']); $date = date('d/m/Y \a\t g:i.s a'); //Open the database mysql_connect("localhost","root",""); mysql_select_db("customfight"); //Select the database if ($existprodcat !== ""){ $pcat = $existprodcat; }else{ $pcat = $newprodcat; } // Check for a front image. if(isset($_FILES['pfrontimage'])){ if (is_uploaded_file ($_FILES['pfrontimage']['tmp_name'])) { if (move_uploaded_file($_FILES['pfrontimage']['tmp_name'], "uploads/{$_FILES['pfrontimage']['name']}")) { // Move the file over. echo '<p>The image has been uploaded!</p>'; } else { // Couldn't move the file over. echo '<p><font color="red">The image could not be moved.</font></p>'; $i = FALSE; } $i = $_FILES['pfrontimage']['name']; } else { $i = FALSE; } } // Check for a back image. if(isset($_FILES['pbackimage'])){ if (is_uploaded_file ($_FILES['pbackimage']['tmp_name'])) { if (move_uploaded_file($_FILES['pbackimage']['tmp_name'], "uploads/{$_FILES['pbackimage']['name']}")) { // Move the file over. echo '<p>The image has been uploaded!</p>'; } else { // Couldn't move the file over. echo '<p><font color="red">The image could not be moved.</font></p>'; $i = FALSE; } $i = $_FILES['pbackimage']['name']; } else { $i = FALSE; } } //Check for existing fields if($prodname && $pcat && $prodptitle && $prodpdescription && $prodpkeywords && $pfrontimage && $pbackimage && $pcolours && $psizes && $prodprice && $proddesc && $date){ $pagecheck = mysql_query("SELECT prod_name FROM products WHERE prod_name = '$prodname'");echo mysql_error(); $count = mysql_num_rows($pagecheck); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">That product already exists. Please edit the existing product!</font>");echo mysql_error(); }else{ //Enter into Database $queryreg = mysql_query("INSERT INTO `customfight`.`products` (`id`, `prod_cat`, `prod_name`, `prod_ptitle`, `prod_pdescription`, `prod_pkeywords`, `prod_fimage`, `prod_bimage`, `prod_colours`, `prod_sizes`, `prod_price`, `prod_desc`, `date`) VALUES (NULL, '$pcat', '$prodname', '$prodptitle', '$prodpdescription', '$prodpkeywords', '$pfrontimage', '$pbackimage', '$pcolours', '$psizes', '$prodprice', '$proddesc', '$date')");echo mysql_error(); echo("<font size=\"2\" color=\"#00cc00\">Your product has been created! </font>");echo mysql_error(); } }else{ echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");echo mysql_error(); } } //end Check for existing fields //end if submit $sql = "SELECT * FROM `products`"; $query = mysql_query($sql);echo mysql_error(); ?> <div class="sechead"><p>Add a product</p><br /> </div> <form action="addproduct2.php" method="post" enctype="multipart/form-data" name="addproduct" target="_self" id="addproduct"> <div class="seccont"> <div class="infocont"> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Product Name</p></div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Create product category</p></div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Select existing category</p></div> </div> <div class="genwrap"> <div class="titlewrap"><input name="prod_name" type="text" id="prod_name" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"><input name="newprod_cat" type="text" id="newprod_cat" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("customfight"); //Select the database $sql = "SELECT `prod_cat` FROM products"; $query = mysql_query($sql) or die(mysql_error()); $sections = array(); while ($row = mysql_fetch_array($query)){ $sections[] = $row[0]; } $sections = array_unique($sections); ?> <select name="existprod_cat" id="existprod_cat"> <option selected></option> <?php foreach($sections as $section){ echo '<option value="'.$section.'">'.$section.'</option>'; } ?> </select> </div> </div> </div> </div> <div class="sechead"> <p>Meta Data</p> </div> <div class="seccont"> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Title</p></div> </div> <div class="genwrapmeta"> <input name="prod_ptitle" id="prod_ptitle" type="text" size="80" maxlength="100" /> </div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Description</p></div> </div> <div class="genwrapmeta"> <input name="prod_pdescription" type="text" id="prod_pdescription" size="80" maxlength="200" /> </div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Keywords</p></div> </div> <div class="genwrapmeta"> <input name="prod_pkeywords" type="text" id="prod_pkeywords" size="80" maxlength="200" /> </div> </div> <div class="sechead">Product Front Image</div> <div class="seccont"><input name="pfrontimage" type="file" /></div> <div class="sechead">Product Back Image</div> <div class="seccont"><input name="pbackimage" type="file" /></div> <div class="sechead">Product Sizes</div> <div class="seccont"> <label>S1</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S2</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S3</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S4</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S5</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S6</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S7</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <br /><br /> <label>S8</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S9</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> <label>S10</label> <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </div> <div class="sechead">Page Colours</div> <div class="seccont"> <div class=" colourswrap"> <div class="colours"><img src="uploads/black.jpg" alt="Black" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Black" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/white.jpg" alt="White" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="White" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/blue.jpg" alt="Blue" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Blue" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/red.jpg" alt="Red" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Red" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/pink.jpg" alt="Pink" width="22" height="22" /></div> <br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Pink" /> </div> </div> <div class="sechead">Product Price</div> <div class="seccont"> <input type="text" name="prod_price" id="prod_price" /> </div> <div class="sechead">Page Content</div> <div class="seccont"> <textarea name="prod_desc" cols="80" rows="20" id="prod_desc"></textarea> </div> <div class="secbtm"><input type="submit" value="Save" name="submit" /></div> </form> </div> </div> </div>
-
Oh and I changed the missing $ from prodpkeywords in the //check for existing fields
-
Hi guys Please could you take a look at this code and tell me why it won't put the data from the form in to the database. I don't get an error. I get "Please complete all fields" as per the echo statement in the code. Thanks in advance <?php $submit = $_POST['submit']; //Form data $prodname = mysql_real_escape_string($_POST['prod_name']); $newprodcat = mysql_real_escape_string($_POST['newprod_cat']); $existprodcat = mysql_real_escape_string($_POST['existprod_cat']); $prodptitle = mysql_real_escape_string($_POST['prod_ptitle']); $prodpdescription = mysql_real_escape_string($_POST['prod_pdescription']); $prodpkeywords = mysql_real_escape_string($_POST['prod_pkeywords']); $pfrontimage = mysql_real_escape_string($_POST['pfrontimage']); $pbackimage = mysql_real_escape_string($_POST['pbackimage']); $pcolours = mysql_real_escape_string(serialize($_POST['pcolours'])); $psizes = mysql_real_escape_string(serialize($_POST['psizes'])); $prodprice = mysql_real_escape_string($_POST['prod_price']); $proddesc = mysql_real_escape_string($_POST['prod_desc']); $date = date('d/m/Y \a\t g:i.s a'); if (isset($_POST['submit'])) { if ($existprodcat !== ""){ $pcat = $existprodcat; }else{ $pcat = $newprodcat; } //Check for existing fields if ($prodname && $prodptitle && $prodpdescription && prodpkeywords && $pfrontimage && $pbackimage && $pcolours && $psizes && $prodprice && $proddesc && $date){ $pagecheck = mysql_query("SELECT `prod_name` FROM products WHERE `prod_name` = '$prodname'");echo mysql_error(); $count = mysql_num_rows($pagecheck); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">Product already exists. Please edit the existing product!</font>");echo mysql_error(); }else{ //Enter into Database $queryreg = mysql_query("INSERT INTO products (`prod_name`, `prod_cat`, `prod_ptitle`, `prod_pdescription`, `prod_pkeywords`, `prod_fimage`, `prod_bimage`, `prod_colours`, `prod_sizes`, `prod_price`, `prod_desc`, `date`) VALUES ('$prodname,' '$pcat', '$prodptitle', '$prodpdescription', '$prodpkeywords', '$pfrontimage', '$pbackimage', '$pcolours', '$psizes', '$prodprice', '$proddesc', '$date')");echo mysql_error(); echo("<font size=\"2\" color=\"#00cc00\">Your product has been created! </font>");echo mysql_error(); } }else{ echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");echo mysql_error(); } } //end Check for existing fields //end if submit $sql = "SELECT * FROM `products`"; $query = mysql_query($sql);echo mysql_error(); ?> <div class="sechead"><p>Add a product</p><br /> </div> <form action="" method="post" enctype="multipart/form-data" name="addproduct" target="_self" id="addproduct"> <div class="seccont"> <div class="infocont"> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Product Name</p></div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Create product category</p></div> </div> <div class="genwrap"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Select existing category</p></div> </div> <div class="genwrap"> <div class="titlewrap"><input name="prod_name" type="text" id="prod_name" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"><input name="newprod_cat" type="text" id="newprod_cat" size="25" /> </div> </div> <div class="genwrap"> <div class="titlewrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("customfight"); //Select the database $sql = "SELECT `prod_cat` FROM products"; $query = mysql_query($sql) or die(mysql_error()); $sections = array(); while ($row = mysql_fetch_array($query)){ $sections[] = $row[0]; } $sections = array_unique($sections); ?> <select name="existprod_cat" id="existprod_cat"> <option selected></option> <?php foreach($sections as $section){ echo '<option value="'.$section.'">'.$section.'</option>'; } ?> </select> </div> </div> </div> </div> <div class="sechead"> <p>Meta Data</p> </div> <div class="seccont"> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Title</p></div> </div> <div class="genwrapmeta"> <input name="prod_ptitle" id="prod_ptitle" type="text" size="80" maxlength="100" /> </div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Description</p></div> </div> <div class="genwrapmeta"> <input name="prod_pdescription" type="text" id="prod_pdescription" size="80" maxlength="200" /> </div> <div class="genwrapmeta"> <div class="arrowwrap"></div> <div class="titlewrap"><p>Keywords</p></div> </div> <div class="genwrapmeta"> <input name="prod_pkeywords" type="text" id="prod_pkeywords" size="80" maxlength="200" /> </div> </div> <div class="sechead">Product image front</div> <div class="seccont"><label><input type="file" name="pfrontimage" id="pfrontimage" /></label></div> <div class="sechead">Product image back</div> <div class="seccont"><label><input type="file" name="pbackimage" id="pbackimage" /></label></div> <div class="sechead">Product Sizes</div> <div class="seccont"> <label>S1 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S2 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S3 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S4 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S5 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S6 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S7 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label><br /><br /> <label>S8 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S9 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label> <label>S10 <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" /> </label></div> <div class="sechead">Page Colours</div> <div class="seccont"> <div class=" colourswrap"> <div class="colours"><img src="uploads/black.jpg" alt="Black" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Black" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/white.jpg" alt="White" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="White" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/blue.jpg" alt="Blue" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Blue" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/red.jpg" alt="Red" /></div><br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Red" /> </div> <div class=" colourswrap"> <div class="colours"><img src="uploads/pink.jpg" alt="Pink" width="22" height="22" /></div> <br /><br /> <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Pink" /> </div> </div> <div class="sechead">Product Price</div> <div class="seccont"> <label> <input type="text" name="prod_price" id="prod_price" /> </label> </div> <div class="sechead">Page Content</div> <div class="seccont"> <textarea name="prod_desc" cols="80" rows="20" id="prod_desc"></textarea> </div> <div class="secbtm"><input type="submit" value="Save" name="submit" /></div> </form>
-
Can I dynamically create a form from info in the database?
bullbreed replied to bullbreed's topic in PHP Coding Help
Ok. I think i'll use Javascript and GD to create the images (well i'll try anyway lol) But could you please give me a clue where to start echoing out the for each loop in a checkbox. Heres what I came up with but haven't been doing php for that long. Also could someone explain how I set up the database with test data because I know I have to serialize the data from the checkboxes but how can I create serialized data just to test the array works? Heres what I initially came up with the echo the array in a checkbox but have no data to check it yet (idiot, i know) What do you think? Would this work? $sql = "SELECT `colours` FROM `products` WHERE `prod_name` = '$prodname' "; $query = mysql_query($sql) or die(mysql_error()); $colours = array(); while ($row = mysql_fetch_array($query)){ $colours[] = $row[0]; $colours = unserialize ($data['colours']); } $colours = array_unique($colours); ?> <label> <input type="checkbox" name="prod_colours" id="prod_colours" value="<?php foreach($colours as $colour){ echo '<option value="'.$colour.'">'.$colour.'</option>'; } ?>"> -
Do you know who you will be using as the payment service provider? I think they will probably have their own method of taking payment. I would personally take their details, submit them to the database then direct them to payment.php where they could start the payment process
-
Can I dynamically create a form from info in the database?
bullbreed replied to bullbreed's topic in PHP Coding Help
I suppose I am being restrictive by not wanting to use javascript but the reason I didn't want to is because if I were to develop this as a working project for a client who didn't have javascript turned on they would not be able to add a colour option. Saying that I don't have statistics showing how many people actually turn javascript off. If it were all PHP then we wouldn't have any issues. I could use javascript to allow the user to select a colour and that colour hex value could be inserted in to a text field then read by the GD script to make the image once submitted. But once these are in the database I want the admin page add-product.php to echo out the colour images with a check box next to it so the user simply clicks the checkbox to associate a colour for a product. This i'm stuck on. Dynamically generating checkbox fields associated with a colour. -
HI everyone. I am learning PHP by developing an ecommerce system. What I am trying to do is create an admin facility so the admin can add a product to the database. I have most of this done but i'm having a problem with the colour options of the products. I want the admin to be able to either select an existing colour from colours existing in the database#and allow them to create a colour if one doesn't exist. But I want to use images to represent the colours and i dont want to use javascript. So heres what I want to do. When the admin adds a product (lets say a T-Shirt) it may be available in different colours so I want to echo 2 small colour images from the database. black.jpg and white.jpg. But I want to also echo out a checkbox benieth the colour image that represents that colour. So that when the admin selects the colours that a particukar t-shirt is available in he simply checks the check boxes relevant to the colours available. When he submits the form to add the product it stores the selected colour check boxes in the database relative to that particular t-shirt. However, if the colour doesnt exist in the database (lets say red) I would like the admin to be able to create a colour image (red.jpg). Now they could do this by either creating an image then uploading it or enter the hex colour code in to a form field and ley phpo gd dcreate the image, save it to the database and call it up to the screen with a checkbox. I think it will look good and make it simple for the user to add colour options even though it might be complicated to do in PHP. But thats why i'm here, lerning php Could you point me in the right direction to get this started please
-
Hi, thanks very much for that. I checked the database and there was a space after the second Animal value so it returned it twice Your a star.
-
HI. I had help from the forum the other day about inserting data from the database in to a menu form field This was the code I was taught and it works. However, If I have 2 articles in the database with the same category the menu shows the category twice. e.g Category----------------Article Animals----------------------Cats Animals----------------------Dogs Category----------------Article Vegetables----------------------Carrotts The Menu Form Field Shows Animals Animals Vegetables <div class="infowrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("********"); //Select the database $sql = "SELECT DISTINCT(article_section) FROM articles"; $query = mysql_query($sql)or die;(mysql_error()); if(mysql_num_rows($query) > 0){ ?> <label> <select name="exist_cat" id="exist_cat"> <option selected></option> <?php while($r = mysql_fetch_array($query)){ echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>'; } ?> </select> <?php } ?> </label> </div> How do I restrict the output to show the category only once. Because if I have 50 articles in the animal section the menu form element will show Animals 50 times
-
Checking 2 variables and entering only one in to database
bullbreed replied to bullbreed's topic in PHP Coding Help
Sorted it. I'm not sure why 2 rows were being created bit I changed the code to read; if ($existcat !== ""){ $cat = $existcat; }else{ $cat = $newcat; } Then added $cat to the INSERT function further down the page to combine the INSERT function. Thanks for your help everyone. Thanks for the answer wide_load and libeco -
post your code please
-
Checking 2 variables and entering only one in to database
bullbreed replied to bullbreed's topic in PHP Coding Help
It is probably my code because i've only been learning php for a month or so. Also tried Cool Geeks example as well and still got 2 rows. Heres the entire code <?php $submit = $_POST['submit']; //Form data $existcat = mysql_real_escape_string($_POST['exist_cat']); $newcat = mysql_real_escape_string($_POST['new_cat']); $articletitle = mysql_real_escape_string($_POST['article_title']); $articleptitle = mysql_real_escape_string($_POST['article_ptitle']); $articlepdescription = mysql_real_escape_string($_POST['article_pdescription']); $articlepkeywords = mysql_real_escape_string($_POST['article_pkeywords']); $articlecontent = mysql_real_escape_string($_POST['article_content']); $date = date('d/m/Y \a\t g:i.s a'); if (isset($_POST['submit'])) { //Open the database mysql_connect("localhost","root",""); mysql_select_db("*********"); //Select the database $cat = empty($existcat)? $newcat:$existcat; $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$cat')"); //Check for existing fields if ($articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){ $pagecheck = mysql_query("SELECT `article_title` FROM articles WHERE `article_title` = '$articletitle'");echo mysql_error(); $count = mysql_num_rows($pagecheck); if ($count != 0){ echo("<font size=\"2\" color=\"#ff0000\">Article already exists. Please edit the existing article!</font>");echo mysql_error(); }else{ //Enter into Database $queryreg = mysql_query("INSERT INTO articles (`article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$articletitle', '$articleptitle', '$articlepdescription', '$articlepkeywords', '$articlecontent', '$date')"); echo("<font size=\"2\" color=\"#00cc00\">Your article has been created! </font>"); } }else{ echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>"); } } //end Check for existing fields //end if submit $sql = "SELECT * FROM `articles` ORDER BY `id` ASC"; $query = mysql_query($sql); ?> </div> <!-- Page submission Result --> <form action="addarticle.php" method="post" enctype="multipart/form-data" name="addarticle" target="_self" id="addarticle"> <!-- Form Starts --> <div class="titlearea">Add News Article</div> <div class="dataarea"> <div class="infowrap">Article title</div> <div class="infowrap">Create Article Section</div> <div class="infowrap">Select Article Section</div> <div class="infowrap"> <input name="article_title" type="text" id="article_title" size="25" maxlength="20" /> </div> <div class="infowrap"> <input name="new_cat" type="text" id="new_cat" size="25" maxlength="100" /> </div> <div class="infowrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("customfight"); //Select the database $sql = "SELECT DISTINCT(article_section) FROM articles"; $query = mysql_query($sql)or die;(mysql_error()); if(mysql_num_rows($query) > 0){ ?> <label> <select name="exist_cat" id="exist_cat"> <option selected></option> <?php while($r = mysql_fetch_array($query)){ echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>'; } ?> </select> <?php } ?> </label> </div></div> <div class="titlearea">Meta Data</div> <div class="dataarea"> <div class="metawrap">Title - Please use approx 65 characters</div> <div class="metawrap"> <input name="article_ptitle" type="text" id="article_ptitle" size="80" maxlength="20" /> </div> <div class="metawrap">Description - Please use approx 155 characters inc spaces</div> <div class="metawrap"> <input name="article_pdescription" type="text" id="article_pdescription" size="80" maxlength="200" /> </div> <div class="metawrap">Keywords - Please use approx 5 keyword per page</div> <div class="metawrap"> <input name="article_pkeywords" type="text" id="article_pkeywords" size="80" maxlength="200" /> </div> </div> <div class="titlearea">Article Content</div> <div class="dataarea"> <textarea name="article_content" cols="80" rows="20" id="article_content"></textarea> </div> <div class="dataarea"> <input type="submit" value="Save" name="submit" /> </div> </form> -
Checking 2 variables and entering only one in to database
bullbreed replied to bullbreed's topic in PHP Coding Help
Thank you but I still get the same. 2 rows. One with the article_section only and another with all the article information. Strange one this is -
Hi. I'm trying to check 2 variables from a form. If one variable exists then put the value in the article_section column of the database If it doesnt have a value then put the value of a different variable i to the article_section column of the database I tried this if ($existcat !== ""){ $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$existcat')");echo mysql_error(); }else{ $queryreg = mysql_query("INSERT INTO `articles` (`article_section`) VALUES ('$newcat')");echo mysql_error(); } The code is actually longer than this and checks other variables from the form like aticle content, title etc But the part of the code aove puts 2 rows in to the database. One row with only the article_category and no other article information One row with the article information with no article_category How do I check 2 variables and select which one I want to put in the particular database column 'article_section' Thanks
-
It doesnt put the category variable in to the article_section column of the database and creates 2 rows of info in the db? Not sure why. Well I have an idea. My code is rubbish
-
Has anyone added a category to a news article section before? The reason I want a category associated with the articles is so the visitor can easily find the articles they want. e.g. if it was a golfing website the article section could have hundreds of articles but if they were sorted by category they would be easier to find. Category = Clubs Article about clubs no 1 Article about clubs no 2 Article about clubs no 3 Category = Drivers Article about drivers no 1 Article about drivers no 2 Article about drivers no 3
-
Thanks for that. I can now submit the form but it adds 2 rows to the database What im trying to do is this I have a news article system on my site and when the admin wants to add an article he can either select a category from a drop down menu or create a new category Hence if ($existcat != ""){ $queryreg = mysql_query("INSERT INTO articles (`article_section`) VALUES ('$existcat')");echo mysql_error(); }else{ if ($existcat == ""){ $queryreg = mysql_query("INSERT INTO articles (`article_section`) VALUES ('$newcat')");echo mysql_error(); } } The $existcat variable is a menu form field and has an empty value as default but also echos the column of the database that holds the categories So what im trying to achieve is someting like; If the $existcat has no value then they must have selected someting below the empty value so put it in the article_section column If the $existcat does have no value then check the other form field called $newcat and store that value in the article_section column