Dynamically generate mySQL queries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Nouwens

    Dynamically generate mySQL queries

    Hi All,

    Please accept my appologies in advance for what I expect will be a
    reasonably simple question.

    I have an html form (which is generated in php) which contains a
    number of rows (one row per product in the 'products' table).

    It is generated with the following php

    <code>
    // create a table for the results
    echo('<table border=1 cellpadding=2>' );
    echo('<tr> <th>Product Description</th><th>Sales $</th><th>Sales
    Units</th><th>Credits $</th><th>Credits Units</th> </tr>');

    // add the results to the table
    while ( $row = mysql_fetch_arr ay($result) ) {
    // get columns from the output on the current row
    $ProdDesc = $row['ProductDescrip tion'];
    $ProdID = $row['ID'];
    // write content collected above into table cells
    echo('<tr><td>' );
    echo($ProdDesc) ;
    echo('</td><td>');
    echo('<form action="update-db.php" method="post">< input type="text"
    name="sd');
    echo($ProdID);
    echo('">');
    echo('</td><td>');
    echo('<form action="update-db.php" method="post">< input type="text"
    name="su');
    echo($ProdID);
    echo('">');
    echo('</td><td>');
    echo('<form action="update-db.php" method="post">< input type="text"
    name="cd');
    echo($ProdID);
    echo('">');
    echo('</td><td>');
    echo('<form action="update-db.php" method="post">< input type="text"
    name="cu');
    echo($ProdID);
    echo('">');
    echo('</td></tr>');
    }
    echo('</table>');
    //the table is complete
    </code>

    The values sd1, su1, cd1, cu1,...... cuXX are all available to the
    update-db.php page which is called when this form is submitted so I am
    fairly sure this is working correctly.

    So far, so good, but when it comes to inserting the data into the
    database I have not been able to create the SQL dynamically (in
    testing I know the number of products and have manually written lots
    of SQL INSERTs but this will not work in the real world)

    The SQL needs to look like roughly like this:
    mysql_query ("INSERT INTO sales_dollars (SaleID, ProductID,
    WeekCommencing, Sales, Credits) VALUES ('',
    '$prodID','$Wee kCommencing','$ sd1', '$cd1')");

    My logic was to say something like this (which needless to say doesn't
    work !)
    <?php

    for ($prodID = 1; $prodID <= $prodCount; $prodID++)
    {
    $currentSD = "sd" . $prodID
    $currentSU = "su" . $prodID
    $currentCD = "cd" . $prodID
    $currentCU = "cu" . $prodID


    mysql_query ("INSERT INTO sales_dollars (SaleID, ProductID,
    WeekCommencing, Sales, Credits) VALUES ('',
    '$prodID','$Wee kCommencing','$ currentSD', '$currentCD')") ;
    }

    I may have messed up the for loop here but the real problem was that
    query would insert '$sd1', '$sd2' etc as text strings into the
    database rather than evaluating $sd1 to it's value (passed from the
    original table which would be a number)

    I have tried using eval() with no success (though that may well be due
    to my complete newbieness with php). I have also tried passing the
    values from the original html as arrays and trying to use simillar
    logic as above to read from $array[$currentArrayEl ement] also with no
    success. Again, if I manually echo the results of $array[0] I am
    returned the value entered in the first html field but I can not
    dynamically select the next array element.

    I hope I have made sense of the problem without writing unnecessary
    garbage.

    Many thanks in advance for your assistance.

    Dave Nouwens
    Sydney, Australia.
  • Pedro Graca

    #2
    Re: Dynamically generate mySQL queries

    Dave Nouwens wrote:[color=blue]
    > I have an html form (which is generated in php) which contains a
    > number of rows (one row per product in the 'products' table).
    >
    > It is generated with the following php
    >
    > <code>
    > // create a table for the results
    > echo('<table border=1 cellpadding=2>' );
    > echo('<tr> <th>Product Description</th><th>Sales $</th><th>Sales
    > Units</th><th>Credits $</th><th>Credits Units</th> </tr>');
    >
    > // add the results to the table
    > while ( $row = mysql_fetch_arr ay($result) ) {
    > // get columns from the output on the current row
    > $ProdDesc = $row['ProductDescrip tion'];
    > $ProdID = $row['ID'];
    > // write content collected above into table cells
    > echo('<tr><td>' );
    > echo($ProdDesc) ;
    > echo('</td><td>');
    > echo('<form action="update-db.php" method="post">< input type="text"
    > name="sd');
    > echo($ProdID);
    > echo('">');
    > echo('</td><td>');
    > echo('<form action="update-db.php" method="post">< input type="text"
    > name="su');
    > echo($ProdID);
    > echo('">');
    > echo('</td><td>');
    > echo('<form action="update-db.php" method="post">< input type="text"
    > name="cd');
    > echo($ProdID);
    > echo('">');
    > echo('</td><td>');
    > echo('<form action="update-db.php" method="post">< input type="text"
    > name="cu');
    > echo($ProdID);
    > echo('">');
    > echo('</td></tr>');
    > }
    > echo('</table>');
    > //the table is complete
    > </code>[/color]

    I think your problem is right there, in the HTML.
    <form>s do not nest!
    If you have several <form>s in your page the posted data is the contents
    of just one of them.

    I usually do <form>s and <table>s like this:

    <form>
    <table>
    <tr>
    <td>...</td>
    <td><input ...></td>
    </tr>
    <tr>
    <td>...</td>
    <td><input ...></td>
    </tr>
    </table>
    </form>
    [color=blue]
    > The values sd1, su1, cd1, cu1,...... cuXX are all available to the
    > update-db.php page which is called when this form is submitted so I am
    > fairly sure this is working correctly.[/color]

    Hmmm ... maybe I'm wrong :)

    [color=blue]
    > So far, so good, but when it comes to inserting the data into the
    > database I have not been able to create the SQL dynamically (in
    > testing I know the number of products and have manually written lots
    > of SQL INSERTs but this will not work in the real world)
    >
    > The SQL needs to look like roughly like this:
    > mysql_query ("INSERT INTO sales_dollars (SaleID, ProductID,
    > WeekCommencing, Sales, Credits) VALUES ('',
    > '$prodID','$Wee kCommencing','$ sd1', '$cd1')");
    >
    > My logic was to say something like this (which needless to say doesn't
    > work !)
    ><?php
    >
    > for ($prodID = 1; $prodID <= $prodCount; $prodID++)
    > {
    > $currentSD = "sd" . $prodID
    > $currentSU = "su" . $prodID
    > $currentCD = "cd" . $prodID
    > $currentCU = "cu" . $prodID
    >
    >
    > mysql_query ("INSERT INTO sales_dollars (SaleID, ProductID,
    > WeekCommencing, Sales, Credits) VALUES ('',
    > '$prodID','$Wee kCommencing','$ currentSD', '$currentCD')") ;
    > }
    >
    > I may have messed up the for loop here but the real problem was that
    > query would insert '$sd1', '$sd2' etc as text strings into the
    > database rather than evaluating $sd1 to it's value (passed from the
    > original table which would be a number)[/color]

    Try variable variables


    $currentSD = ${'sd' . $prodID};


    If you turn register_global s off (which might be a good idea) change
    that line to

    $currentSD = $_POST['sd' . $prodID];

    [color=blue]
    > I have tried using eval() with no success (though that may well be due
    > to my complete newbieness with php). I have also tried passing the
    > values from the original html as arrays and trying to use simillar
    > logic as above to read from $array[$currentArrayEl ement] also with no
    > success. Again, if I manually echo the results of $array[0] I am
    > returned the value entered in the first html field but I can not
    > dynamically select the next array element.[/color]



    Happy Coding :)

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Michael S. Clark

      #3
      Re: Dynamically generate mySQL queries

      Dave Nouwens wrote:[color=blue]
      > Hi All,
      >
      > Please accept my appologies in advance for what I expect will be a
      > reasonably simple question.
      >
      > I have an html form (which is generated in php) which contains a
      > number of rows (one row per product in the 'products' table).
      >
      > It is generated with the following php
      >
      > <code>
      > // create a table for the results
      > echo('<table border=1 cellpadding=2>' );
      > echo('<tr> <th>Product Description</th><th>Sales $</th><th>Sales
      > Units</th><th>Credits $</th><th>Credits Units</th> </tr>');
      >
      > // add the results to the table
      > while ( $row = mysql_fetch_arr ay($result) ) {
      > // get columns from the output on the current row
      > $ProdDesc = $row['ProductDescrip tion'];
      > $ProdID = $row['ID'];
      > // write content collected above into table cells
      > echo('<tr><td>' );
      > echo($ProdDesc) ;
      > echo('</td><td>');
      > echo('<form action="update-db.php" method="post">< input type="text"
      > name="sd');
      > echo($ProdID);
      > echo('">');
      > echo('</td><td>');
      > echo('<form action="update-db.php" method="post">< input type="text"
      > name="su');
      > echo($ProdID);
      > echo('">');
      > echo('</td><td>');
      > echo('<form action="update-db.php" method="post">< input type="text"
      > name="cd');
      > echo($ProdID);
      > echo('">');
      > echo('</td><td>');
      > echo('<form action="update-db.php" method="post">< input type="text"
      > name="cu');
      > echo($ProdID);
      > echo('">');
      > echo('</td></tr>');
      > }
      > echo('</table>');
      > //the table is complete
      > </code>
      >
      > The values sd1, su1, cd1, cu1,...... cuXX are all available to the
      > update-db.php page which is called when this form is submitted so I am
      > fairly sure this is working correctly.
      >
      > So far, so good, but when it comes to inserting the data into the
      > database I have not been able to create the SQL dynamically (in
      > testing I know the number of products and have manually written lots
      > of SQL INSERTs but this will not work in the real world)
      >
      > The SQL needs to look like roughly like this:
      > mysql_query ("INSERT INTO sales_dollars (SaleID, ProductID,
      > WeekCommencing, Sales, Credits) VALUES ('',
      > '$prodID','$Wee kCommencing','$ sd1', '$cd1')");
      >
      > My logic was to say something like this (which needless to say doesn't
      > work !)
      > <?php
      >
      > for ($prodID = 1; $prodID <= $prodCount; $prodID++)
      > {
      > $currentSD = "sd" . $prodID
      > $currentSU = "su" . $prodID
      > $currentCD = "cd" . $prodID
      > $currentCU = "cu" . $prodID
      >
      >
      > mysql_query ("INSERT INTO sales_dollars (SaleID, ProductID,
      > WeekCommencing, Sales, Credits) VALUES ('',
      > '$prodID','$Wee kCommencing','$ currentSD', '$currentCD')") ;
      > }
      >
      > I may have messed up the for loop here but the real problem was that
      > query would insert '$sd1', '$sd2' etc as text strings into the
      > database rather than evaluating $sd1 to it's value (passed from the
      > original table which would be a number)
      >
      > I have tried using eval() with no success (though that may well be due
      > to my complete newbieness with php). I have also tried passing the
      > values from the original html as arrays and trying to use simillar
      > logic as above to read from $array[$currentArrayEl ement] also with no
      > success. Again, if I manually echo the results of $array[0] I am
      > returned the value entered in the first html field but I can not
      > dynamically select the next array element.
      >
      > I hope I have made sense of the problem without writing unnecessary
      > garbage.
      >
      > Many thanks in advance for your assistance.
      >
      > Dave Nouwens
      > Sydney, Australia.[/color]

      Variable variables can be evaluated thus:

      $currentSD = ${"sd".$prodID} ;
      $currentSU = ${"su".$prodID} ;
      $currentCD = ${"cd".$prodID} ;
      $currentCU = ${"cu".$prodID} ;

      It may also be a case of evaluating the SQL string before passing to
      mysql_query():

      $sqlInsert = "INSERT INTO sales_dollars (SaleID, ProductID,
      WeekCommencing, Sales, Credits) VALUES ('',
      '".$prodID."',' ".$WeekCommenci ng."','".$curre ntSD."', '".$currentCD." ')";

      mysql_query($sq lInsert);

      Hope this helps.

      ===
      Michael S. Clark
      Web Applications Developer
      Scotland On Line

      Comment

      • Dave Nouwens

        #4
        Re: Dynamically generate mySQL queries

        "Michael S. Clark" <mclark@scotlan donline.co.uk> wrote in message news:<c948e9$6b 2$1@phys-pa.scotland.net >...

        --8<--8<--8<
        [color=blue]
        > Hope this helps.
        >
        > ===
        > Michael S. Clark
        > Web Applications Developer
        > Scotland On Line[/color]

        It helped tremendously, thankyou (and thanks also to Pedro for getting
        it started in the right direction)

        Dave

        Comment

        Working...