Jump to content

Dynamic SQL Query Based on Single/Multiple Form Inputs?


teamshultz

Recommended Posts

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.

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?

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.

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.