ttocs Posted June 9, 2009 Share Posted June 9, 2009 Hi guys, I have a question about using PHP with SQL. Let me first explain that I'm new to PHP. We took a 3 day class at my job and are just starting to get into the benefits PHP can perform. So, don't assume I know much when replying Ok, so I'm making a tool that allows users to select from a drop down. When they select their product, a query is run in the background that pulls in information based off of the product they selected. I have this part down. It is shown on the next page and allows them to make changes to it. What I'm trying to figure out is how to get this back into the database. Multiple rows are being shown (one row for each month for 2 years, so 24 rows) and they are able to update as many as they like. If this were one row I would just use a variable in a SQL insert statement, but for some reason, all of these rows are just throwing me through a loop. I thought of an array, but we can't figure out the syntax if it would even work. Does anyone have any ideas or maybe a snippet of code of this being done in the past. Not sure if it matters, but we are using Oracle. Thanks in advance for any help. Link to comment https://forums.phpfreaks.com/topic/161520-solved-help-with-inserting-sql-back-into-db-with-php/ Share on other sites More sharing options...
ttocs Posted June 9, 2009 Author Share Posted June 9, 2009 Here is the code that I have so far. This is the php script that runs when you hit the button on the main page (after you pick the product). for($i=0;$i<12;$i++){ $name = "Year_Mon$i"; $name2 = "Product$i"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Goal Tool</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <link type="text/css" rel="stylesheet" href="/css/search_redirect.css" /> </head> <body> <div class="intro"> <p class="bi_motto"><br/>"Header"</p> <a href="/html/index.html"> <img src="Image.jpg"/> </a> </div> <p><br/></p> <?php $product = $_GET['product']; if (!$db = @ocinlogon("DB","user", "pw")) { $error = ocierror(); printf("CONNECT error: %s", $error["message"]); die(); } $query = "select * from db.table where product in ('$product')"; $stmt = ociparse($db,$query); if (!@ociexecute($stmt)) { $error = ocierror($stmt); printf("OCIEXECUTE Failure. Error was: %s\n", $error["message"]); } echo "<form action=\"goal_tool.php\" method=\"get\">"; echo "<table>"; echo "<tr>"; echo "<td><b>Year_Month</b></td>"; echo "<td><b>Product</b></td>"; echo "<td><b>Revenue</b></td>"; echo "<td><b>IC</b></td>"; echo "<td><b>Impressions</b></td>"; echo "</tr>"; $i = 0; while($nrows = ocifetchinto($stmt, &$results, OCI_ASSOC)) { echo "<tr>"; echo "<td><input type=text name=\"Year_Mon$i\" value=".$results['YEAR_MON']."></td>"; echo "<td><input type=text name=\"Product$i\" value=".$results['PRODUCT']."></td>"; echo "<td><input type=text name=\"Revenue$i\" value=".$results['REVENUE']."></td>"; echo "<td><input type=text name=\"IC$i\" value=".$results['IC']."></td>"; echo "<td><input type=text name=\"Imps$i\" value=".$results['IMPS']."></td>"; echo "</tr>"; $i++; } echo "</table>"; echo "<input type=\"submit\" value=\"Submit\"/>"; This just returns the 24 rows in editable boxes. Now I have to get this back in the database. Link to comment https://forums.phpfreaks.com/topic/161520-solved-help-with-inserting-sql-back-into-db-with-php/#findComment-852354 Share on other sites More sharing options...
gevans Posted June 9, 2009 Share Posted June 9, 2009 Instead of numbering every input field, you can use an array; your code; echo "<td><input type=text name=\"Year_Mon$i\" value=".$results['YEAR_MON']."></td>"; changed code; echo '<td><input type=text name="Year_Mon[]" value='.$results['YEAR_MON'].'></td>'; If you do that for each field you'll have an array on the next html page ready made for you. Without and security or fancy stuff, this would print an array of 20 fields; foreach($_GET['Year_Mon'] as $key => $value) { echo "$key: $value<br />"; } That should get you in the right direction Link to comment https://forums.phpfreaks.com/topic/161520-solved-help-with-inserting-sql-back-into-db-with-php/#findComment-852360 Share on other sites More sharing options...
ttocs Posted June 9, 2009 Author Share Posted June 9, 2009 Really? Thanks. Let me try to put that into play with this. I think that might be what I'm looking for. Thank you. Link to comment https://forums.phpfreaks.com/topic/161520-solved-help-with-inserting-sql-back-into-db-with-php/#findComment-852369 Share on other sites More sharing options...
ttocs Posted June 9, 2009 Author Share Posted June 9, 2009 This did the trick. Thank you so much! This is exactly what we were missing! Link to comment https://forums.phpfreaks.com/topic/161520-solved-help-with-inserting-sql-back-into-db-with-php/#findComment-852389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.