Jump to content

Recommended Posts

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


<?php
if (isset($_POST['NOTES'])) {
  foreach ($_POST['NOTES'] as $value) {
    echo "$value
\n";
  }
}
?>

-- This piece of code allows for the array

Can any one assist with this problem please?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/20057-list-menu-problem/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88285
Share on other sites

[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
Link to comment
https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88301
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88432
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/20057-list-menu-problem/#findComment-88631
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.