mykmallett Posted May 25, 2011 Share Posted May 25, 2011 Hi, I have a form that I want to allow users to be able to add specific links, some are set types (domains) and just require the iser to enter the link, however, one is for whatever they wish and they must enter a name to describe it. Now im going to enter it into a database with the following columns: USER_ID || LINK_URL || LINK_TYPE || LINK_NAME Link name is optional (only needed if the link type is their own custom one), the link types are ID's related to a table of link types (facebook, twitter etc). Now I'm assuming that to differentiate the form elements for insertion into the database I would need to have a hidden form element with the type ID inside? However I don't really know how to go on from here. A small version of the form would be <input type="text" name="facebook_link" /> <input type="hidden" name="facebook_id value="1" /> <input type="text" name="twitter_link" /> <input type="hidden" name="twitter_id value="2" /> <input type="text" name="own_site" /> <input type="text" name="own_site_description" /> <input type="hidden name="own_site_id" value="3" /> However, I'm assuming this is wrong because I'll need to enter everything into an array won't I? I just can't get my head round it to out put thi results into a foreach statement. Can anybody help me please? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2011 Share Posted May 25, 2011 No need for hidden fields. Create your form fields AS arrays with the link ID as the array index. Something like this: Facebook Link: <input type="text" name="links[1]" /><br> Twitter Link: <input type="text" name="links[2]" /><br> Custom Link: <input type="text" name="links[3]" /><br> Link Name: <input type="text" name="link_name" /> Then in your PHP code you can loop through all the entries using foreach($_POSTS['links'] as $linkID => $linkValue) { //Do something with input } Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2011 Share Posted May 25, 2011 Also, I want to advise against doing a separate INSERT query for each record - it is a very bad practice. Here is a more complete script for the processing logic $userID = (int) $_SESSION['user_id']; //Generate insert values for each record $linkValues = array(); foreach($_POST['links'] as $linkID => $linkValue) { //Parse input $linkID = (int) $linkID; $linkValue = mysql_real_escape_string(trim($linkValue)); //Create insert record if value entered if(!empty($linkValue)) { $linkName = ($linkID!=3) ? 'NULL' : mysql_real_escape_string(trim($_POST['link_name'])); $linkValues[] = "($userID, '$linkValue', $linkID, '$linkName')"; } } //Create and run ONE insert query for all entered values $query = "INSERT INTO [table_name] (`USER_ID`, `LINK_URL`, `LINK_TYPE`, `LINK_NAME`) VALUES " . implode(', ', $linkValues); $result = mysql_query($query) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
mykmallett Posted May 25, 2011 Author Share Posted May 25, 2011 Wow, fantastic! Thanks very much. Just so I'm clear, as I'm unfamiliar with this syntax, this: $linkName = ($linkID!=3) ? 'NULL' : mysql_real_escape_string(trim($_POST['link_name'])); Means if $linkID does not equal 3, then $linkName = 'NULL', else it equals the escaped value of $_POST['link_name'] Am I correct? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2011 Share Posted May 25, 2011 ... Just so I'm clear, as I'm unfamiliar with this syntax, this: $linkName = ($linkID!=3) ? 'NULL' : mysql_real_escape_string(trim($_POST['link_name'])); Means if $linkID does not equal 3, then $linkName = 'NULL', else it equals the escaped value of $_POST['link_name'] Am I correct? Yes, that is called a ternary operator and acts as a quasi IF/ELSE statement. I use it most frequently when I need to assign one of two values to a variable based on a condition. But, it has other uses as well. Here is the basic structure: ([CONDITION]) ? [TRUE_RESULT] : [FALSE_RESULT]; Quote Link to comment Share on other sites More sharing options...
mykmallett Posted May 25, 2011 Author Share Posted May 25, 2011 Superb thankyou 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.