recset Posted September 7, 2006 Share Posted September 7, 2006 I am trying to store multiple selections from a List Menu into one db field, but I get an error msg everytime I try. Here is a snippet of code:$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;-- this line is where the error is and<select name="NOTES[]" size="10" multiple="multiple" id="NOTES">-- this is the line of my List menu<?phpif (isset($_POST['NOTES'])) { foreach ($_POST['NOTES'] as $value) { echo "$value\n"; }}?>-- This piece of code allows for the arrayCan any one assist with this problem please?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/ Share on other sites More sharing options...
HuggieBear Posted September 7, 2006 Share Posted September 7, 2006 What's the error, can you give us the code?RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88031 Share on other sites More sharing options...
onlyican Posted September 7, 2006 Share Posted September 7, 2006 shouldn't it beforeach($_POST["NOTES"] as $key => $value) Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88044 Share on other sites More sharing options...
recset Posted September 8, 2006 Author Share Posted September 8, 2006 The database record just shows 'Array', but doesn't display what is in the array. Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88161 Share on other sites More sharing options...
wildteen88 Posted September 8, 2006 Share Posted September 8, 2006 Try this:[code]if (isset($_POST['NOTES'])) { /*foreach ($_POST['NOTES'] as $value) { echo "$value\n";*/ // show what ever is in the NOTES array echo '<pre>' . print_r($_POST['NOTES'], true) . '</pre>';}[/code]What does that return? It should return the format and what is in the NOTES array. Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88285 Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 [quote author=recset link=topic=107280.msg430332#msg430332 date=1157688533]The database record just shows 'Array', but doesn't display what is in the array.[/quote]you need to convert the array to a string before inserting to a database. A sloppy way is to implode() the array with a delimeter, then explode() it when you retrieve from the site, a better way is to create a new table for NOTES and use the foreign key of user ID or post ID or whatever it is that is the 'parent' entity of NOTES.For example; the NOTES are child members of a form for a book review..so;[code]<?php$link = mysql_connect('host', 'user', 'pass');mysql_select_db('db', $link);function magic_escape($value, $link){ if (get_magic_quotes_gpc()) $value = stripslashes($value); return mysql_real_escape_string($value, $link);}if (!empty($_POST['NOTES'])){ foreach ($_POST['NOTES'] as $note) { if (!mysql_query( "INSERT INTO `book_reviews` (`notes`) VALUES ('" . magic_escape($note, $link) . "') WHERE `bookid` = '" . magic_escape($_POST['bookid']) . "'" )) { die('Error inserting note: ' . htmlentities($note)); } }}?>[/code]Then when selecting..[code]<?php$link = mysql_connect('host', 'user', 'pass');mysql_select_db('db', $link);function magic_escape($value, $link){ if (get_magic_quotes_gpc()) $value = stripslashes($value); return mysql_real_escape_string($value, $link);}if (!empty($_POST['bookid'])) { $result = mysql_query( "SELECT `notes` FROM `book_reviews` WHERE `bookid` = '" . magic_escape($_POST['bookid'], $link) . "'" ); if (!$result) die('Error selecting notes for bookid: ' . htmlentities($_POST['bookid'])); while ($row = mysql_fetch_assoc($result)) { echo '<div class="note">' . htmlentities($row['note']) . "</div>\n"; }}else{ echo 'No notes';}?>[/code]untested Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88301 Share on other sites More sharing options...
recset Posted September 8, 2006 Author Share Posted September 8, 2006 Jenk,I think for the type of application that I am going for, I don't think it is necessary to create a new table for NOTES, so I was wondering how would the 'implode' and 'explode' function work? I would prefer to keep any additional codes for the array on the current page that I have now.[u][b]Add'tl Info[/b][/u]Also, I have the connections scripts in a seperate php file, so could you just guide me on how to do the array. Also the values for the array that I went to send into the database comes from a recordset from another table. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88432 Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 It's always worth it, and it is the exact type of design pattern Relational Databases such as MySQL are designed for. It is actually much less work to create the table and use a JOIN than it is to implode/explode an array (or serialize()) Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88492 Share on other sites More sharing options...
recset Posted September 8, 2006 Author Share Posted September 8, 2006 Hi Jenk,I've tried to follow your method, but it conflicts with what I already have. I am confused, because the values of the array have to be selected through a form (they are not predefined). Thus, each record will have a different set of array values. I've been doing some research, and I believe that I just need to insert a piece of code in [b]here[/b]:[code]<select name="NOTES[HERE]" size="9" multiple="multiple" id="NOTES"><?php do { ?><option value="<?php echo $row_literarydevices['LDEVICE']?>"<?php if (!(strcmp($row_literarydevices['LDEVICE'], $row_Recordset1['NOTES']))) {echo "selected=\"selected\"";} ?>><?php echo $row_literarydevices['LDEVICE']?></option><?php} while ($row_literarydevices = mysql_fetch_assoc($literarydevices)); $rows = mysql_num_rows($literarydevices); if($rows > 0) { mysql_data_seek($literarydevices, 0); $row_literarydevices = mysql_fetch_assoc($literarydevices); }?></select>[/code]What variable should I put in 'here' so that it can store the values that the user selects?I do appreciate your help though, its just I always look for efficient ways to code.Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88631 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.