insert multiple records into one table (one form)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GJPeacock@gmail.com

    insert multiple records into one table (one form)

    Hi all,

    Please excuse my limited technical knowledge of PHP, so far i have only
    created PHP sites using Dreamweaver, i find know that i need features
    outside of it's capabilities.

    I am trying to create a 'related articles' selection page. The
    administrator, after creating an article, can select older articles to
    include on the final displayed page.

    As the number of older articles increases, so will the number of
    possible relataed articles, so i am making a dynamic checkbox as the
    selection. This is all covered in Dreamweavers features, but, what i
    then need to be able to do is create a new record in the
    'related_articl es' table for each article selected when the form is
    submitted.

    I found this post below, which is more or less exactly what i want to
    do, i just wasn't sure that this was php code, looks like asp. Would
    someone be kind enough to show me how to change it to php code, or even
    write the code for me? ANY help at all would be much appreciated.



    Thanks,

    Gavin

  • NC

    #2
    Re: insert multiple records into one table (one form)

    GJPeacock@gmail .com wrote:[color=blue]
    >
    > I am trying to create a 'related articles' selection page. The
    > administrator, after creating an article, can select older articles to
    > include on the final displayed page.[/color]

    Is this the best way to do it? You could generate a list of related
    articles dynamically, based on article's publication date and topic
    (which, of course, means that you need to keep track of topics in your
    database).
    [color=blue]
    > As the number of older articles increases, so will the number of
    > possible relataed articles, so i am making a dynamic checkbox as the
    > selection. This is all covered in Dreamweavers features, but, what i
    > then need to be able to do is create a new record in the
    > 'related_articl es' table for each article selected when the form is
    > submitted.[/color]

    So what seems to be the problem? You receieve several ID numbers or
    URLs via POST, and run one or more INSERT queries with this data.
    [color=blue]
    > I found this post below, which is more or less exactly what i want to
    > do, i just wasn't sure that this was php code, looks like asp. Would
    > someone be kind enough to show me how to change it to php code,
    > or even write the code for me?[/color]

    Well, so far you haven't even mentioned what database you are using...

    Cheers,
    NC

    Comment

    • GJPeacock@gmail.com

      #3
      Re: insert multiple records into one table (one form)

      Hi,

      Maybe i should re-phrase my question. I'm using php with a MySQL
      database, and i would like to know how to insert a new record into a
      table for each checked item on one form.

      The reason i want to create a relation in this way, is because i want
      to be able to choose which article appears as a related link regardless
      of it's category/date/etc.
      [color=blue]
      > So what seems to be the problem? You receieve several ID numbers or
      > URLs via POST, and run one or more INSERT queries with this data.[/color]

      No problem with this, it's just this is what i'm not sure of how to do.

      Thanks, Gavin

      Comment

      • NC

        #4
        Re: insert multiple records into one table (one form)

        GJPeacock@gmail .com wrote:[color=blue]
        >
        > Maybe i should re-phrase my question. I'm using php with a MySQL
        > database, and i would like to know how to insert a new record into a
        > table for each checked item on one form.[/color]

        OK, let's say you have a form:

        <form method="POST" action="insert. php">
        <input type="checkbox" name="id[]" value="32">Arti cle #32<br>
        <input type="checkbox" name="id[]" value="38">Arti cle #38<br>
        <input type="checkbox" name="id[]" value="45">Arti cle #45<br>
        <input type="checkbox" name="id[]" value="59">Arti cle #59<br>
        <input type="Submit">
        </form>

        Now, let's assume the user checked articles #32 and #59. These values
        will be available to insert.php as fields in $_POST['id'], which in
        this case will be an array. So in insert.php you can write:

        $query = 'INSERT INTO related_article s (id) VALUES (' .
        implode('), (', $_POST['id']) . ')';
        $result = mysql_query($qu ery)
        or die('Could not execute INSERT query');

        That's it, really...

        Cheers,
        NC

        Comment

        • GJPeacock@gmail.com

          #5
          Re: insert multiple records into one table (one form)

          Thanks for the reply NC, the trouble is i don't want the field to be an
          array, i want each item that is selected to be inserted into a new row.
          So if #32 and #59 were selected, #32 would be inserted into a row, then
          a new row would be created to insert #59 into... and so on if there
          were more articles selected.

          Comment

          • Jerry Stuckle

            #6
            Re: insert multiple records into one table (one form)

            NC wrote:[color=blue]
            > GJPeacock@gmail .com wrote:
            >[color=green]
            >>Maybe i should re-phrase my question. I'm using php with a MySQL
            >>database, and i would like to know how to insert a new record into a
            >>table for each checked item on one form.[/color]
            >
            >
            > OK, let's say you have a form:
            >
            > <form method="POST" action="insert. php">
            > <input type="checkbox" name="id[]" value="32">Arti cle #32<br>
            > <input type="checkbox" name="id[]" value="38">Arti cle #38<br>
            > <input type="checkbox" name="id[]" value="45">Arti cle #45<br>
            > <input type="checkbox" name="id[]" value="59">Arti cle #59<br>
            > <input type="Submit">
            > </form>
            >
            > Now, let's assume the user checked articles #32 and #59. These values
            > will be available to insert.php as fields in $_POST['id'], which in
            > this case will be an array. So in insert.php you can write:
            >
            > $query = 'INSERT INTO related_article s (id) VALUES (' .
            > implode('), (', $_POST['id']) . ')';
            > $result = mysql_query($qu ery)
            > or die('Could not execute INSERT query');
            >
            > That's it, really...
            >
            > Cheers,
            > NC
            >[/color]

            It also violates first normal form for relational databases.

            Rather, something like (for simplicity, no error checking shown):

            foreach ($_POST['id']) as $id) {
            mysql_query("IN SERT INTO related_table (article_id, related_id) " .
            "VALUES ($origId, $id)");

            $origId is the original article id, $_POST['id'] is the id(s) of the related
            article(s).

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • NC

              #7
              Re: insert multiple records into one table (one form)

              GJPeacock@gmail .com wrote:[color=blue]
              >
              > Thanks for the reply NC, the trouble is i don't want the field to be an
              > array,[/color]

              Then tell us a little more about your form, so we can give you advice
              appropriate to your situation... Also, why don't you want the field to
              be an array?
              [color=blue]
              > i want each item that is selected to be inserted into a new row.[/color]

              This is exactly what this piece of code does. It creates one record
              for each selected item. Obviously, the data in the example are
              incomplete (the ID of the article to which selected articles are
              related is missing, which is probably what prompted Jerry Stuckle's
              comment about it violating first normal form for relational databases),
              but it was only meant to show you the sequence of steps... Well, let's
              try it again, this time with more data...

              Let's say you have a form:

              <form method="POST" action="insert. php">
              <input type="checkbox" name="id[]" value="32">Arti cle #32<br>
              <input type="checkbox" name="id[]" value="38">Arti cle #38<br>
              <input type="checkbox" name="id[]" value="45">Arti cle #45<br>
              <input type="checkbox" name="id[]" value="59">Arti cle #59<br>
              <input type="hidden" name="referer" value="123">
              <!-- This is the ID of the "referring" article -->
              <input type="Submit">
              </form>

              Then, let's assume the user checked articles #32 and #59. These
              values will be available to insert.php as fields in $_POST['id'], which

              in this case will be an array. So in insert.php you can write:

              $ref = $_POST['referer'];
              $query = 'INSERT INTO related_article s (id, referer) VALUES (' .
              implode(", $ref), (", $_POST['id']) . ", $ref)";
              $result = mysql_query($qu ery)
              or die('Could not execute INSERT query');

              Here's the query that is going to be executed:

              INSERT INTO related_article s (id, referer)
              VALUES (32, 123), (59, 123);

              This query will create two records in related_article s, one linking
              article #32 to article #123, the other linking article #59 to article
              #123.

              The advantage over Jerry Stuckle's solution is that all necessary
              records are created in one query, although I must admit that this
              advantage is probably trivial.

              Cheers,
              NC

              Comment

              • GJPeacock@gmail.com

                #8
                Re: insert multiple records into one table (one form)

                Thanks NC

                This is exactly what i wanted, sorry if it took a few posts to get
                here. Much appreciated.

                Gavin

                Comment

                Working...