Jump to content


Photo

List Menu problem


  • Please log in to reply
8 replies to this topic

#1 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 07 September 2006 - 10:23 PM

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

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 10:36 PM

What's the error, can you give us the code?

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 07 September 2006 - 11:15 PM

shouldn't it be
foreach($_POST["NOTES"] as $key => $value)
Tell me the problem, I will try tell you the solution

#4 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 08 September 2006 - 04:08 AM

The database record just shows 'Array', but doesn't display what is in the array.



#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 September 2006 - 10:28 AM

Try this:
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>';
}
What does that return? It should return the format and what is in the NOTES array.

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 11:22 AM

The database record just shows 'Array', but doesn't display what is in the array.


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;

<?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));
        }
    }
}

?>

Then when selecting..

<?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';
}

?>
untested

#7 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 08 September 2006 - 03:20 PM

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.

Add'tl Info
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.


#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 04:32 PM

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())

#9 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 08 September 2006 - 07:44 PM

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 here:

<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>

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users