Jump to content

going from one input to multiple inputs from user trying to run a query


hmark

Recommended Posts

Hello.

 

I'm wanting to modifymy php code from accepting only one input from a user, to giving them a choice of two.

 

For instance, a user can look a information based on a "comcode" or a "pack name". I'm very new to PHP so, i would appreciate any help i can get. Right now it only works for "comcode". I want to give users options to query by "pack name" too.

 

Here's what I have:

 

?

require 'lib-hollydb.inc';

connect_to_db("$dbname");

 

$comcode = $_GET['comcode'];

$q = "select * from hollydb.experts where comcode=$comcode";

 

connect_to_db("$dbname");

$res = run_query($q);

$rec = mysql_fetch_array($res);

if(!$rec){

errorMsg(" No record found. Please try again.");

}

 

?>

 

<?

cePageHead();

ceLeftMenu();

?>

 

</td>

<td valign="top">

<img src="images/red_vertical_bar.gif" width=1 height=120 border=0 alt="">

</td>

<td valign="top">

</td>

<td>

<b>PRO input:</b> <a href="pred_input.php">add</a> | edit | <a href="pred_delete.php">delete</a> <br>

<BR>

Edit a record<br><br>

 

<form action="pred_edit3.php">

<input type="hidden" name="comcode" value=<?echo "$comcode"?>>

<table>

<tr>

<td>Code:</td>

<td><input type="text" name="code" value="<?echo $rec['code']?>"><td>

</tr>

<tr>

<td>Description:</td>

<td><input type="text" name="descript" value="<?echo $rec['descript']?>"><td>

</tr>

<tr>

<td>Indoor Predicted FIT Rate:</td>

<td><input type="text" name="ind_pred_fit" value="<?echo $rec['ind_pred_fit']?>"><td>

</tr>

<tr>

<td>Outdoor Predicted FIT Rate:</td>

<td><input type="text" name="outd_pred_fit" value="<?echo $rec['outd_pred_fit']?>"><td>

</tr>

<tr>

<td>Prediction Reference:</td>

<td><input type="text" name="pred_ref" value="<?echo $rec['pred_ref']?>"><td>

</tr>

<tr>

<td>Target FIT Rate:</td>

<td><input type="text" name="target_fit" value="<?echo $rec['tareget_fit']?>"><td>

</tr>

<tr>

<td>Target Reference:</td>

<td><input type="text" name="target_ref" value="<?echo $rec['tareget_ref']?>"><td>

</tr>

 

<td>

</tr>

<tr>

<td> </td>

<td><br><input type="submit" value="save"><td>

</tr>

</table>

</form>

</form>

</td>

</tr>

</td>

</tr>

</table>

</div>

 

</body>

</html>

 

 

Then I have the following:

 

 

<?

require 'lib-hollydb.inc';

 

$comcode=$_GET['comcode'];

$ay = array_keys($_GET);

$x = implode(",",$ay);

$y = implode("','", $_GET);

 

$a = array();

 

foreach ($_GET as $key => $value) {

if($value != ''){

$q = "$key='$value'";

array_push($a,$q);

}

}

$q = implode(", ",$a);

$q = "update experts set $q where comcode=$comcode";

connect_to_db("$dbname");

run_query($q);

 

?>

 

<?

cePageHead();

ceLeftMenu();

?>

 

<b>

Thank you.

<br>

Comcode: <?echo $comcode?> has been updated.

</b>

</td>

 

 

</tr>

<br>

 

 

</td>

</tr>

</table>

</div>

 

<div align="center">

 

</body>

</html>

 

I hope i've posted enough info to explain myself. Once again, any help would be appreciated.

 

If you use the code/PHP tags (the # and

 receptively on the toolbar) then your probably more likely to get a reply. 

 

As to answering your question, the exact answer will depend on whether they should always search for two, one of them will always be there with the second optional or if both are optional. You will simply need to use isset($_GET['comcode']) to check if a value has been submitted then construct the MySQL query appropriately using the AND/OR keywords. For example if both are required you can use...

 

[code]$q = "SELECT * FROM hollydb.experts WHERE comcode='$comcode' AND pack_name='$pack_name'";[/code]

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.