daebat Posted October 8, 2009 Share Posted October 8, 2009 I am creating a submission form for somebody to come in and submit products. These products are separated by brand. How can I get them to display according to the brand? Here is the form code: <? include("../include/session.php"); ?> <?php if ($submit) { $sql = "UPDATE productimages SET title='".$_POST['title']."', upjpg='".$_POST['upjpg']."', uptiff='".$_POST['uptiff']."', uppng='".$_POST['uppng']."', chungshi='".$_POST['chungshi']."', stretchwalker='".$_POST['stretch_walker']."', akaishi='".$_POST['akaishi']."', bellamargiano='".$_POST['bellamargiano']."', mbt='".$_POST['mbt']."', upthumb='".$_POST['upthumb']."' where id ='".mysql_real_escape_string($_POST['id'])."'"; $result = mysql_query($sql) or die(mysql_error()); print("Product Added"); } else { $result = mysql_query("SELECT * FROM productimages WHERE id = '$id'"); while ($row=mysql_fetch_array($result)) { $id = $row[id]; $uptiff = $row[uptiff]; $upjpg = $row[upjpg]; $uppng = $row[uppng]; $chungshi = $row[chungshi]; $stretchwalker = $row[stretchwalker]; $akaishi = $row[akaishi]; $bellamargiano = $row[bellamargiano]; $mbt = $row[mbt]; $upthumb = $row[upthumb]; } print (" <form method=post action=productimages.php> Product Title:<br> <input type=text name=title size=60><br><br> Choose Categories that this story is relevant to:<br> <input type=checkbox name=chungshi value=1> Chung Shi<br> <input type=checkbox name=stretchwalker value=1> Stretchwalker<br> <input type=checkbox name=akaishi value=1> Akaishi<br> <input type=checkbox name=bellamargiano value=1> Bellamargiano<br> <input type=checkbox name=mbt value=1> MBT<br><br> <table width=500 cellpadding=0 cellspacing=0> <tr><td colspan=2 class=top><strong>Images</strong></td></tr> <tr><td>Upload JPG</td><td><br><input type=file name=upjpg></td></tr> <tr><td colspan=2 class=top> </td></tr> <tr><td>Upload TIFF</td><td><br><input type=file name=uptiff> </td></tr> <tr><td colspan=2 class=top> </td></tr> <tr><td>Upload PNG</td><td><br><input type=file name=uppng> </td></tr> <tr><td colspan=2 class=top> </td></tr> <tr><td>Upload Thumbnail</td><td><br><input type=file name=upthumb> </td></tr> </table> <br> <input type=submit name=submit value=submit><br> <br> </form> "); } ?> Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/ Share on other sites More sharing options...
jon23d Posted October 8, 2009 Share Posted October 8, 2009 Escape all of your data, not just the ID. Consider using a separate table for categories, this will enable you to more easily add/remove categories and sort by them. If you don't know what database normalization is, read about it, it'll help convince you. Once you've done this, your sql to sort by category could be: SELECT * FROM `products` INNER JOIN `categories` ON `products`.`category` = `categories`.`id` ORDER BY `category` ASC Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/#findComment-933190 Share on other sites More sharing options...
daebat Posted October 8, 2009 Author Share Posted October 8, 2009 So you're saying I should put: if ($submit) { $sql = "UPDATE productimages SET title='".mysql_real_escape_string($_POST['title']."', upjpg='".mysql_real_escape_string($_POST['upjpg']."', uptiff='".mysql_real_escape_string($_POST['uptiff']."', uppng='".mysql_real_escape_string($_POST['uppng']."', chungshi='".mysql_real_escape_string($_POST['chungshi']."', stretchwalker='".mysql_real_escape_string($_POST['stretch_walker']."', akaishi='".mysql_real_escape_string($_POST['akaishi']."', bellamargiano='".mysql_real_escape_string($_POST['bellamargiano']."', mbt='".mysql_real_escape_string($_POST['mbt']."', upthumb='".mysql_real_escape_string($_POST['upthumb']."' where id ='".mysql_real_escape_string($_POST['id'])."'"; $result = mysql_query($sql) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/#findComment-933207 Share on other sites More sharing options...
jon23d Posted October 9, 2009 Share Posted October 9, 2009 Yep. But still, what will you do when you add a new category? How about 10 of them? What if you want to change the name of a category? Will you also change it everywhere in your code? Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/#findComment-933531 Share on other sites More sharing options...
daebat Posted October 9, 2009 Author Share Posted October 9, 2009 Yeah, I'm going to create a table for categories. The problem is I don't know the code to join the tables to display them on the page. Also, when I create the table for categories how should I lay them out? By id number? Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/#findComment-933714 Share on other sites More sharing options...
jon23d Posted October 10, 2009 Share Posted October 10, 2009 I'd guess alphabetical by title? An example query to get all products in a given category: SELECT `id` FROM `products` INNER JOIN `categories` ON `products`.`category` = `categories`.`id` WHERE `categories`.`id` = x; Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/#findComment-934100 Share on other sites More sharing options...
daebat Posted October 13, 2009 Author Share Posted October 13, 2009 I am getting Column 'id' in field list is ambiguous. $data = mysql_query("SELECT `id` FROM `productimages` INNER JOIN `pimagecategories` ON `productimages`.`category` = `categories`.`id` WHERE `categories`.`id` = 1;") or die(mysql_error()); I know it is because I have two 'id' fields but I'm not sure what to do to the code to make it work. Link to comment https://forums.phpfreaks.com/topic/176982-set-categories/#findComment-936181 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.