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.
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.
Comment