jwwceo Posted August 28, 2006 Share Posted August 28, 2006 I am making my first php mySQL project and I'm at the toughest part...I'm a little confused where to even start on this part...my project has t-shirts being held in a databse. One of the tables in the database is just for all the colors that various shirts come in. Many shirts come in lots of different colors. So I have created a third table with just 2 fields (shirt_id and color_id) that will match the shirts with all the different colors thay come in...However, on my page for adding a t-shirt to the database I would like to have all the available colors listed with checkboxes so when I add the shirt I can also click on all the colors they come in and submit everything at the same time...can this be done at the same time???....it seems like if I haven't added the shirt first then there won't be a shirt_id to be added to the joined third table. Does this make sense... Quote Link to comment Share on other sites More sharing options...
shocker-z Posted August 28, 2006 Share Posted August 28, 2006 so basicaly you would use somthing simular to this[code]<form action="" method="post"><input type="text" name="shirt_id"><Br><input type="checkbox" name="color[]" value="red">Red<Br><input type="checkbox" name="color[]" value="green">Green<Br><input type="checkbox" name="color[]" value="blue">blue<Br><input type="submit" name="submit" value="Submit"></form><?phpif ($_POST['submit']) {$shirt_id=mysql_real_escape_string($_POST['shirt_id']); foreach($_POST['color'] as $color) { $color=mysql_real_escape_string($color); $query=mysql_query("INSERT INTO table_name (shirt_id,color_id) VALUES('$shirt_id','$color')"); }}?>[/code]That should work :) hope it atleast helps you.RegardsLiam Quote Link to comment Share on other sites More sharing options...
jwwceo Posted August 28, 2006 Author Share Posted August 28, 2006 Nice. That worked awesome...THANKS!!!any tips on how to write a little code for the edit page which will see if the color values should be checked already or not...I can't seem to get my head around this....been working too long... Quote Link to comment Share on other sites More sharing options...
shocker-z Posted August 28, 2006 Share Posted August 28, 2006 [code]$acolor=array();$query=mysql_query("SELECT color_id FROM tablen_name WHERE shirt_id = '1'");while ($row=mysql_fetch_array($query)) {$acolor[] = $row['color_id'];}$query2=mysql_query("SELECT color FROM colors");while ($row2=mysql_fetch_array($query2)) { if (in_array($row2['color'],$acolor)) { echo "<input type=\"checkbox\" name=\"color[]\" checked=\"checked\" value=\"$row2[color]\">$row2[color]<Br>"; } else { echo "<input type=\"checkbox\" name=\"color[]\" value=\"$row2[color]\">$row2[color]<Br>"; }}[/code]that *should do the trickhad to rush tho else gf gonna lkill me if im much later picking her up! lol :)have funLiam Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.