teamshultz Posted February 20, 2010 Share Posted February 20, 2010 I'm trying to create an easy way for my salespeople to create quotes. My goal: Have a basic form with one input field (Part Number - correlates to partnum column in item table). I can handle this part. I'd like to have a page that begins with just one input field and has a button underneath (Add Another) that adds another element (duplicate input field) if necesarry. I have no idea how many items will be included in the price quote so I can't code in a predetermined amount of input fields. Once all part numbers are entered, I need to submit the query eg: select * from items where partnum = partnum1, partnum2, partnum3..... and have the results returned in a table. This has me stumped at the moment. Can you guys please give me some direction? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/ Share on other sites More sharing options...
trq Posted February 20, 2010 Share Posted February 20, 2010 You would simply name all your part number inputs as an array. eg partnumber[], then youq eury would look something like..... $sql = "SELECT * FROM items WHERE partnum IN('" . implode("','", $_POST['partnumber']) . "')"; Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015108 Share on other sites More sharing options...
teamshultz Posted February 20, 2010 Author Share Posted February 20, 2010 Here's a snippet of the element adder with your changes added. Is this close to what you're talking about? el.innerHTML = 'Part Number: <input type="text" name="partnum[' + ++i + ']" />'; The count (+ ++i +) starts at one, so it looks as though partnum[1], partnum[2], partnum[3], etc... will be passed on to the query. Sound right? Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015118 Share on other sites More sharing options...
trq Posted February 20, 2010 Share Posted February 20, 2010 You don't need to define any keys. el.innerHTML = 'Part Number: <input type="text" name="partnum[]" />'; Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015133 Share on other sites More sharing options...
teamshultz Posted February 20, 2010 Author Share Posted February 20, 2010 Ok, so this will be the final result. Say the salesperson adds two more elements with the first one: <form action="itemq.php" method="get"> Part Number: <input type="text" name="partnum[]" /> Part Number: <input type="text" name="partnum[]" /> Part Number: <input type="text" name="partnum[]" /> </form> will get passed on to the query, which is as follows: $sql = "SELECT * FROM items WHERE partnum IN('" . implode("','", $_GET['partnumber']) . "')"; I'll give it a shot in the morning. Thanks a lot for your help. Much appreciated. Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015145 Share on other sites More sharing options...
teamshultz Posted February 21, 2010 Author Share Posted February 21, 2010 Here's the error message I get when I try to do that: Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\quotes\itemq.php on line 10 Here's itemq.php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("pricebook", $con); $sql = "SELECT * FROM items WHERE partnum IN('" . implode("','", $_POST['partnumber']) . "')"; $result = mysql_query($sql); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; echo "<table>"; $i=0; while ($i < $num) { $partnum=mysql_result($result,$i,"partnum"); $desc=mysql_result($result,$i,"desc"); $cost=mysql_result($result,$i,"cost"); $price09=mysql_result($result,$i,"price09"); echo "<tr><td>$partnum</td><td>$desc</td><td>$cost</td><td>$price09</td></tr>"; $i++; } echo "</table>"; ?> And here's the URL that gets passed on to itemq.php http://localhost/quotes/itemq.php?partnum%5B%5D=2011&partnum%5B%5D=2012&partnum%5B%5D=2013 Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015447 Share on other sites More sharing options...
trq Posted February 21, 2010 Share Posted February 21, 2010 You have named your form elements partnum[] yet you refer to them within your query as $_POST['partnumber']. Sould be... $_POST['partnum'] Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015497 Share on other sites More sharing options...
teamshultz Posted February 21, 2010 Author Share Posted February 21, 2010 Excellent! Thanks so much. It's finally working, so you can close this. I heart you. Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015533 Share on other sites More sharing options...
trq Posted February 21, 2010 Share Posted February 21, 2010 You can mark your own topics as resolved. There is a button in the bottom left. Link to comment https://forums.phpfreaks.com/topic/192692-dynamic-sql-query-based-on-singlemultiple-form-inputs/#findComment-1015542 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.