Storing form array data to MySQL using PHP

Discussion in 'PHP' started by osemollie, Feb 2, 2006.

  1. #1
    I am new to PHP and am working on my first project. I have a form that requires a user to pick at least two of his favourite colours (checkbox options) see html code below, how can I save these checkbox options into mysql using php? And how can I display these information from the database?

    _____________________________________________________________-

    <html>

    <head>

    <title>FAVOURITE COLOURS</title>
    </head>

    <body>

    <form method="POST" action="specify_file.php">

    <table border="0" width="60%" id="table1" cellspacing="0" cellpadding="0">
    <tr>
    <td colspan="2">
    <p align="center"><b>FAVOURITE COLOURS</b></td>
    </tr>
    <tr>
    <td width="25%">Name</td>
    <td width="75%"><input type="text" name="T1" size="20"></td>
    </tr>
    <tr>
    <td width="25%">Company:</td>
    <td width="75%"><input type="text" name="T2" size="20"></td>
    </tr>
    <tr>
    <td width="25%">E-mail:</td>
    <td width="75%"><input type="text" name="T3" size="20"></td>
    </tr>
    <tr>
    <td width="100%" colspan="2"><b>Choose at least 2 of your best
    colours</b></td>
    </tr>
    <tr>
    <td width="25%">Red</td>
    <td width="75%">
    <input type="checkbox" name="chkColours[]" value="red"></td>
    </tr>
    <tr>
    <td width="25%">Blue</td>
    <td width="75%">
    <input type="checkbox" name="chkColours[]" value="Blue"></td>
    </tr>
    <tr>
    <td width="25%">Green</td>
    <td width="75%">
    <input type="checkbox" name="chkColours[]" value="Green"></td>
    </tr>
    <tr>
    <td width="25%">Yellow</td>
    <td width="75%">
    <input type="checkbox" name="chkColours[]" value="Yellow"></td>
    </tr>
    </table>
    <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>

    </body>

    </html>
    __________________________________________

    Thanks
     
    osemollie, Feb 2, 2006 IP
  2. hdogan

    hdogan Peon

    Messages:
    316
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    $colors = serialize($chkColours);
    # insert $colors variable into mysql
    
    # select colors field from mysql
    $colors = unserialize($row['colors']);
    echo "Selected colors:<br />\n";
    foreach ($colors as $val) {
        echo "<li>$val</li>\n";
    }
    ?>
    PHP:
     
    hdogan, Feb 3, 2006 IP
  3. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    smo, Feb 3, 2006 IP