insert data on mysql thru php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    insert data on mysql thru php

    I don’t know if I posted my question on the right forum if not my bad, but I’m having trouble how to create a php script that will insert data on mysql

    Scenario:
    I have 3 checkbox on my form and users select all
    Checkbox1
    Checkbox2
    Checbox3

    Table structure:
    Date, checknames


    Goal:
    Every time I hit the update button with query behind I want to insert the current date and checkboxnames (see below table)
    ------------------------------
    |Created | checkboxname|
    ---------------------------------
    |10-07-08 |checkbox1 |
    ---------------------------------
    |10-07-08 |checkbox2 |
    --------------------------------
    |10-07-08 |checkbox3 |
    ---------------------------------

    Problem:
    Help to construct my insert query which i know is wrong :(
    INSERT INTO dmtable (Created, checkboxname)
    VALUES (NOW(),'{checkb ox1}', '{checkbox2}',' {checkbox3}');
  • chelvan
    New Member
    • Aug 2008
    • 90

    #2
    hi
    i don't know why you put this { bracket, on your inserting value. remove that.

    Code:
    INSERT INTO dmtable (Created, checkboxname)
    VALUES (NOW(),'{checkbox1}', '{checkbox2}','{checkbox3}');
    change your query like this
    Code:
    INSERT INTO dmtable (Created, checkboxname)
    VALUES (NOW(),'checkbox1', 'checkbox2','checkbox3');

    regards
    chel-1

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by chelvan
      hi
      i don't know why you put this { bracket, on your inserting value. remove that.

      Code:
      INSERT INTO dmtable (Created, checkboxname)
      VALUES (NOW(),'{checkbox1}', '{checkbox2}','{checkbox3}');
      That wouldn't affect anything..

      OP, you pass more values to your query than columns. You give 2 columns (created, checkboxname) and 4 values. This will not work.

      Comment

      • ddtpmyra
        Contributor
        • Jun 2008
        • 333

        #4
        Markus that's the thing how can I insert all the 3 information in one column in one query

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by ddtpmyra
          Markus that's the thing how can I insert all the 3 information in one column in one query
          Why can't you have a column for each checkbox? That makes sense.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Why don't you just add a row for each checked box?

            Arrays of checkboxes can be sent using the same <input> name, each one containing a unique value.
            You can simply send such an an array, loop through it once you get it and construct a query based on that.

            For example:
            [code=php]
            <form action="?" method="post">
            <input type="checkbox" name="boxes[]" value="box1" /><br />
            <input type="checkbox" name="boxes[]" value="box2" /><br />
            <input type="checkbox" name="boxes[]" value="box3" /><br />
            <input type="submit" />
            </form>
            <?php
            if(isset($_POST['boxes']))
            {
            $rows = array();
            foreach($_POST['boxes'] as $_box)
            {
            $rows[] = "(NOW(), '{$_box}')";
            }

            if(count($rows) > 0)
            {
            $sql = "INSERT INTO `tbl`(`Date`, `Box`) VALUES";
            $sql .= implode(", ", $rows);

            echo "<pre>", $sql, "</pre>";
            }
            }
            ?>
            [/code]
            You would want to sanitize the box value before using it tho. I skipped that part to keep things simple.

            Comment

            Working...