#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    1
    Rep Power
    0

    Question Inserting checkbox values from an array into MySQL


    Inserting checkbox values from an array into MySQL

    I have searched several similar topics and solutions in several websites but I have not yet found a solution that works for me.

    I have this checkbox list in a form:

    <input type="checkbox" name="topic[]" value="people">Historical Figures and People<br>
    <input type="checkbox" name="topic[]" value="myths">Myths and Legends<br>
    <input type="checkbox" name="topic[]" value="buildings">Buildings and Structures<br>
    <input type="checkbox" name="topic[]" value="environments">Scenic Natural Environments

    Following a POST submit, I echo the form data in a confirmation page:

    PHP Code:
    for ($topic_id=0$topic_id<count($topic); $topic_id++) {
    echo 
    "$topic[$topic_id] ";
    echo 
    "<input type='hidden' name='topic[]' value='$topic[$topic_id]'>";

    But here is where I get stuck. How do I get this data out of a loop and into a single comma separated string that I can insert into MySQL?

    This did not work, leaving the field with a NULL value:

    PHP Code:
    foreach( $topic[] AS $topic_name ) {
    $insert_topic "INSERT INTO plandmark_core (topic) 
    VALUES('
    $topic_name')";
    mysql_query$insert_topic );

    and this did not work, also leaving the field with a NULL value:

    PHP Code:
    for ($topic_id=0$topic_id<5$topic_id++) {
    $insertstring3 "insert into landmark_core (topic)
    values('
    $topic[$topic_id]')";
    $result mysql_query($insertstring3);
    if(!
    resulterror_message(sql_error()); 
    The MySQL field is constructed as follows:


    mysql> desc landmark_core;
    +-------------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------------+--------------+------+-----+---------+-------+
    | topic | varchar(50) | YES | | NULL | |
    +-------------+--------------+------+-----+---------+-------+
    Last edited by outlyr242; February 9th, 2004 at 01:42 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Location
    Tacoma, WA
    Posts
    1,353
    Rep Power
    18
    Here is a thread that really helped out quite a bit. Be sure to change the SQL to suit your query. Ps the "good" stuff was from INXgeek

    http://forums.devshed.com/showthread...278#post494278

IMN logo majestic logo threadwatch logo seochat tools logo