pls can any one show me how i can add more array here

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bakbai
    New Member
    • Oct 2008
    • 7

    pls can any one show me how i can add more array here

    already i've an array id, i want to add addional into the row.
    pls help!
    [PHP]$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');[/PHP]
    Last edited by Atli; Oct 19 '08, 06:13 PM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    What errors are you getting?
    How does the query look like when you execute it? (Try echoing it before you call mysql_query)

    Comment

    • Bakbai
      New Member
      • Oct 2008
      • 7

      #3
      Thank you.
      with this code, it works ok.
      [PHP]ref = $_POST['referer'];

      $insertSQL = 'INSERT INTO aco (pin, test) VALUES (' .
      implode(", $ref), (", $_POST['id']) . ", $ref)";




      mysql_select_db ($database_scho ols, $schools);
      $Result1 = mysql_query($in sertSQL, $schools) or die(mysql_error ());
      if (!$Result1) {
      echo 'Sorry';
      }else {
      echo 'Thank you';} [/PHP]

      but if i should add any additional array say;


      [PHP]ref = $_POST['referer'];

      $insertSQL = 'INSERT INTO aco (pin, test, adm) VALUES (' .
      implode(", $ref), (", $_POST['id'], $_POST['class']) . ", $ref)";




      mysql_select_db ($database_scho ols, $schools);
      $Result1 = mysql_query($in sertSQL, $schools) or die(mysql_error ());
      if (!$Result1) {
      echo 'Sorry';
      }else {
      echo 'Thank you';} [/PHP]

      it say:
      Warning: Wrong parameter count for implode() in c:\inetpub\wwwr oot\phcn\test\t est5.php on line 8
      You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 123)' at line 1
      Last edited by Atli; Oct 19 '08, 07:14 PM. Reason: Added [code] and [quote] tags.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        That would be because implode only takes two parameters. The string you want to use to glue each element together, and a array to be imploded.

        If you need to add a second array, you can't just throw it in there as a third parameter.
        You are going to have to merge the two arrays using array_merge first, and use the result from that as the array in you implode call.

        Like, for example:
        [code=php]
        echo implode(", ", array_merge($ar ray1, $array2));
        [/code]

        P.S.
        Please use [code] tags when posting your code examples. (See How to ask a question)

        [code=ph p] ...PHP code goes here... [/code]

        Thank you.
        Moderator

        Comment

        • Bakbai
          New Member
          • Oct 2008
          • 7

          #5
          Once more, thank you. i'll work on it and get back to you.
          You're trull helpful.

          Comment

          • Bakbai
            New Member
            • Oct 2008
            • 7

            #6
            I've tried over the night but cant reach further, pls, what i need to understan is this;

            [code=php]
            $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);

            [/code]

            the code above i got from the forum, it says;

            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:

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

            [CODE=mysql]INSERT INTO related_article s (id, referer)
            VALUES (32, 123), (59, 123);[/CODE]

            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.

            Mine, what i need to understand is how to add additional array say;

            [CODE=mysql]
            INSERT INTO related_article s (id, referer)
            VALUES (32, 35, 123), (59, 34, 123);[/CODE]
            if say one linking article #32, #35 to article #123 in that order.
            turlly appreciating your help
            Last edited by Atli; Oct 20 '08, 11:07 AM. Reason: Added [code] tags to your queries as well.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by Bakbai
              I've tried over the night but cant reach further, pls, what i need to understan is this;
              [...]
              Mine, what i need to understand is how to add additional array say;

              [CODE=mysql]
              INSERT INTO related_article s (id, referer)
              VALUES (32, 35, 123), (59, 34, 123);[/CODE]
              if say one linking article #32, #35 to article #123 in that order.
              turlly appreciating your help
              This is an invalid query, obviously. The number of values per row don't match the columns they are meant to be inserted into.

              I don't really understand what you are asking.
              Are you simply trying to sort the array before using it, so the integers get inserted sequentially, lowest to highest?

              If so, you could try the sort function.
              It can be a little confusing, because it alters the input data rather then return the new array.
              You can use it like so:
              [code=php]
              <?php
              $arr1 = array(1, 3);
              $arr2 = array(2, 4);
              $merged = array_merge($ar r1, $arr2);

              // $merged is now (1, 3, 2, 4)

              sort($merged);

              // $merged is now (1, 2, 3, 4)

              ?>
              [/code]
              If this isn't what you are looking for, please try to explain what it is you are after a bit clearer.

              Comment

              • Bakbai
                New Member
                • Oct 2008
                • 7

                #8
                I will stop say thank you, b'cos i'm really learning more, so, thank you again.
                let me explain what i want to do.
                i've a form which post to a php file which will handle the insert process. the form is as below

                [code=html]
                <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">
                <input type="text" name="class[]" value="3" />
                <input type="text" name="class[]" value="4" />
                <input type="text" name="class[]" value="6" />
                <input type="text" name="class[]" value="7" />
                <!-- This is the ID of the "referring" article -->
                <input type="Submit">
                </form>
                [/code]

                now the code below handles the processs
                [code=php]
                $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');

                [/code]

                this codes does it perfectly since the other array of calss is not included in the insert line.
                the code above executed the following

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

                now what i want to do is to add the class array in the same row to have the code do the following when
                executed;


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

                i hope this will enable you help me. thanks

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Ahh ok. Then it gets a little bit more complicated.

                  You will somehow have to associate a specific 'class' to a specific 'id' in your Form, which is hard to do with the dynamically created arrays you are currently using.

                  One thing tho, do you want each article ID to be associated with a specific class number, or do you want to be able to select a class for each checked article ID?

                  That is, will article #32 always be associated with class number 6, or do you want to give the user the ability to choose a class number for it?

                  Comment

                  • Bakbai
                    New Member
                    • Oct 2008
                    • 7

                    #10
                    Atli, thank you very mucg for your effort. you made me read well.
                    i finaly got my solution. here it is for ref;
                    [code=php]
                    foreach ($_POST['code'] as $row=>$code)
                    {

                    $username = mysql_real_esca pe_string($code );
                    $op = mysql_real_esca pe_string($_POS T['op'][$row]);
                    $cl = mysql_real_esca pe_string($_POS T['cl'][$row]);
                    $time = ($_POST['time']);

                    $insertSQL =('INSERT INTO `trans` (code, op, cl, date) VALUES("' . $username . '", "' . $op . '", "' . $cl . '", "' . $time . '")') ;


                    mysql_select_db ($database_drin , $drin);
                    $Result1 = mysql_query($in sertSQL, $drin) or die(mysql_error ());
                    [/code]

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      I'm glad you found a solution.
                      Thanks for sharing it with us.

                      Comment

                      Working...