Onloac Posted July 31, 2009 Share Posted July 31, 2009 I know my code isn't the best and I know it isn't very secure. I'm just beginning to learn PHP/MySQL and was hoping some of you can give me some help. I'm trying to create a script that will help me manage news. I can add/edit/remove news and news categories from my browser. The only problem is every time I add a new category I have to manually add a line of code so that its included when adding new items to the database. What I want to do is have it look for all the categories from the database, check the previous form data to see which were selected, then have add an entry for each category that was selected. Whats the best method of doing this? I have post information for each category already, I just need away to handle it once its submitted. Thanks in advance! $articleID = $db->insert_id(); $category3 = $_POST["mycategory3"]; $category2 = $_POST["mycategory2"]; $category1 = $_POST["mycategory1"]; $category5 = $_POST["mycategory5"]; $category4 = $_POST["mycategory4"]; if ($category1){ $db->query("INSERT INTO articles_rel (aid, cid) VALUES('$articleID', '$category1')") or die(mysql_error()); } if ($category2){ $db->query("INSERT INTO articles_rel (aid, cid) VALUES('$articleID', '$category2')") or die(mysql_error()); } if ($category3){ $db->query("INSERT INTO articles_rel (aid, cid) VALUES('$articleID', '$category3')") or die(mysql_error()); } if ($category4){ $db->query("INSERT INTO articles_rel (aid, cid) VALUES('$articleID', '$category4')") or die(mysql_error()); } if ($category5){ $db->query("INSERT INTO articles_rel (aid, cid) VALUES('$articleID', '$category5')") or die(mysql_error()); } Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 Well you obviously have the hang of arrays, form processing, and database interaction. So I'll give you a tiny hint and see if that doesn't help. Say you had this HTML markup in your form: <input type="text" name="first_name[]" value="Larry" /> <input type="text" name="first_name[]" value="Bob" /> ...many more inputs <input type="text" name="first_name[]" value="Betty" /> Notice the square brackets on the name-attribute? That causes the $_POST array to contain a sub array. echo $_POST['first_name'][0]; // Larry echo $_POST['first_name'][1]; // Bob //.. more echos echo $_POST['first_name'][count( $_POST['first_name'] ) - 1]; // Betty By carefully manipulating the name-attribute of your INPUT elements you can cause $_POST to already contain a structure similar to your database layout. <input type="text" name="records[0][first_name]" value="Jon" /> <input type="text" name="records[0][last_name]" value="Jones" /> <input type="text" name="records[1][first_name]" value="Sam" /> <input type="text" name="records[1][last_name]" value="Smith" /> <input type="text" name="records[2][first_name]" value="Karl" /> <input type="text" name="records[2][last_name]" value="Kluck" /> echo '<pre>' . print_r( $_POST ) . '</pre>'; 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.