need some help here. It's a very simple web app i'm developing but just needed some help with something.
Here's what's setup. I have a html form with one combo box. All I need is to update this combo box with the entries from a mysql table named 'supplier'. The input to this table 'supplier' is via another form on my website which i've already setup. I need help in auto updating this combo box from the table 'supplier'. Please let me know the php code for it. I've included my code as well. Thanks in advance! I have included the html form as well.
replace your code
$result = mysql_query("SELECT supplier FROM supplier");
while($row = mysql_fetch_array($result))
{
/*echo '<form action="">';*/
echo "<select name='supplier'>";
echo "<option value = '$row[supplier]'>""</option>";
echo "</select>";
with
$result = mysql_query("SELECT supplier FROM supplier");
echo "<select name='supplier'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row[supplier]."'>".$row[supplier]."</option>";
}
echo "</select>";
So,
what is the problem? Is your script not working or do you want a active Combobox on one screen to be updated, when on another screen a new entry for supplier is entered?
Ok, some hints:
The "select"-Tag should be placed outside the loop.
Put the column name in "
Add a display-Value for the options
So, without checking it:
/*echo '<form action="">';*/
echo "<select name='supplier'>";
while($row = mysql_fetch_array($result))
{
echo "<option value = '".$row["supplier"]."'>".$row["supplier"]."</option>";
}
echo "</select>";
Your problem is here:
$result = mysql_query("SELECT supplier FROM supplier");
while($row = mysql_fetch_array($result))
{
/*echo '<form action="">';*/
echo "<select name='supplier'>";
echo "<option value = '$row[supplier]'>""</option>";
echo "</select>";
You're creating the drop down box (the Select) inside of the mysql data loop. As #Hitesh has explained. You need to create this outside of the loop and only echo out the data results within. For example:
$result = mysql_query("SELECT supplier FROM supplier");
echo "<select name='supplier'>";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row['supplier'].">".$row['supplier']."</option>";
}
echo "</select>";
This will output your drop down box, with all your supplier names as the value and the displayed text options.
If you attempted to do the following:
$result = mysql_query("SELECT supplier FROM supplier");
while($row = mysql_fetch_array($result))
{
/*echo '<form action="">';*/
echo "<select name='supplier'>";
echo "<option value = '$row[supplier]'>""</option>";
echo "</select>";
}
You would just end up with as many drop down boxes as you had suppliers in your database. This is because you're creating a new Select box for each record found.
Also, without specifying the second $row['supplier'] between the option tags, you'd just end up with a blank (empty) drop down box.
Hope this helps.
Here is complete code:
PHP Code:
<?php
// select box open tag
$selectBoxOpen = "<select name='supplier'>";
// select box close tag
$selectBoxClose = "</select>";
// select box option tag
$selectBoxOption = '';
// connect mysql server
$con = mysql_connect("localhost","user","pass");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
// select database
mysql_select_db("rtgs", $con);
// fire mysql query
$result = mysql_query("SELECT supplier FROM supplier");
// play with return result array
while($row = mysql_fetch_array($result)){
$selectBoxOption .="<option value = '".$row['supplier']."'>".$row['supplier']."</option>";
}
// create select box tag with mysql result
$selectBox = $selectBoxOpen.$selectBoxOption.$selectBoxClose;
?>
HTML Code:
<form action="ntxn.php" method="post">
<table>
<tr>
<td>Supplier Name:</td>
<td>
<?php echo $selectBox;?>
</td>
</tr>
<tr>
<td>Bill No. :</td>
<td><input type ="text" name="billno"/></td>
</tr>
<tr>
<td>Bill Date : </td>
<td><input type="date" name="billdate"/></td>
</tr>
<tr>
<td>Bill Amount : </td>
<td><input type="text" name="billamt"/></td>
</tr>
<tr>
<td>NEFT / RTGS No. :</td>
<td><input type="text" name="rtgs"/></td>
</tr>
<tr>
<td><input type="submit" name="Save"/></td>
</tr>
</table>
</form>
Related
I want to be able to select a product from two select dropdown lists, e.g. productcode and product name. If I select the productcode I want the corresponding product name to show up in the other select dropdown list and vice versa. So I want to couple every productcode and product name that are on the same row in my MySQL database. I prefer to do this with only HTML and PHP but any other solutions are welcome to.
code snippet of what i am working with:
<tr>
<td>
<select class="productChoice">
<?php
if ($resultCheck > 0){
while($row = mysqli_fetch_assoc($result)) {
$ordercode = $row['productcode'];
echo "<option> $productcode </option>";
}
}
?>
</select>
</td>
<td>
<select class="productChoice">
<?php
mysqli_data_seek($result, 0);
if ($resultCheck > 0){
while($row = mysqli_fetch_assoc($result)) {
$productName = $row['name'];
echo "<option> $productName </option>";
}
}
?>
</select>
</td>
I have a simple table with staff names stored in the column f_operator_name.
I have a drop down menu in a php form with these staff names available for selection. Here is a snippet of the relevant the code:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
When someone selects the staff name I want it to be stored in a variable. I'm testing the submit form at the bottom which is intended to print out the name that has been selected ( in the variable operator), but it's not printing anything out. Can anyone see any issues?
EDIT *** Here's the updated code after some advice from Barmar with the variable information also, for some reason the echo statement still isn't working:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
You can't put <select> inside <h2> and then put the <option>s and </select> outside it. HTML elements have to be nested properly, and <option> has to be inside <select>.
Change it to:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
And the index in $_POST has to match the name of the <select>, so $_POST['operator'] should be $_POST['f_operator_id'].
I am trying to add a 'delete' button on my item table and have a delete button to delete the item and the information about the both item and the seller. I have the delete button on the table but I cannot figure out how to process that button when it is clicked. Please help! Thank you in advance!!
<?php
require 'authentication.inc';
// connect to the server
$connection = sqlsrv_connect( $hostName, $connectionInfo )
or die("ERROR: selecting database server failed");
// prepare SQL query
$UserID = $_SESSION['userID'];
$query = "SELECT * FROM ITEM WHERE userID= '$UserID'";
// Execute SQL query
$query_result = sqlsrv_query($connection, $query)
or die( "ERROR: Query is wrong");
// Output query results: HTML table
echo "<table border=1>";
echo "<tr>";
// fetch attribute names
foreach( sqlsrv_field_metadata($query_result) as $fieldMetadata)
echo "<th>".$fieldMetadata['Name']."</th>";
echo "</tr>";
// fetch table records
while ($line = sqlsrv_fetch_array($query_result, SQLSRV_FETCH_ASSOC)) {
echo "<tr>\n";
foreach ($line as $cell) {
echo "<td> $cell </td>";
}
echo "<td></td>";
echo "</tr>\n";
}
echo "</table>";
// close the connection with database
sqlsrv_close($connection);
?>
To add a delete feature you need two things, a button and secondly the processing of said button. I am not sure what your unique value is in the table, so I am using this fictitious key: itemID. Update with whatever your unique column name is.
Replace your table loop with:
<table border=1>
<tr>
<?php
// fetch attribute names
foreach( sqlsrv_field_metadata($query_result) as $fieldMetadata)
echo "<th>".$fieldMetadata['Name']."</th>"; ?>
</tr>
<?php
// fetch table records
while ($line = sqlsrv_fetch_array($query_result, SQLSRV_FETCH_ASSOC)) { ?>
<tr>
<?php foreach ($line as $cell) {
echo "<td> $cell </td>";
} ?>
<td>
<form method="post">
<input type="hidden" name="itemID" value="<?php echo $line['itemID']; ?>" />
<input type="submit" name="action" value="DELETE" />
</form>
</td>
</tr>
<?php } ?>
</table>
In the processing portion at the top, add processing:
if(!empty($_POST['action']) && ($_POST['action'] == 'DELETE')) {
// Do some sort of validation here
if(is_numeric($_POST['itemID']))
sqlsrv_query($connection, "delete from ITEM where itemID = '".$_POST['itemID']."'");
}
So I have a selector which gets its information from a database.
When I select something from the selector and press: Add to list, it generates a table with the selected information.
Now this what it should do. But now, when I select another result and press Add to list. It removes the old one and replaces it with the new one.
But I actually don't want it to remove the old one, but make a new row under it. So that table gets bigger. How do I do this?
Code for selector:
<!--Selector-->
<?php
//Get name and id data from the db. In an assoc array
$results = $database->Selector();
echo "<form name='form' method='POST' id='selector'>";
echo "<select name='train_name' id='train_name' multiple='multiple'>";
// Loop trough the results and make an option of every train_name
foreach($results as $res){
echo "<option value=" . $res['train_name'] . ">" . $res['train_name'] . "</option>";
}
echo "</select>";
echo "<br />" . "<td>" . "<input type='submit' name='Add' value='Add to list'/>" . "</td>";
echo "</form>";
if(isset($_POST["train_name"])){
//Get all data from database, in an assoc array
$results = $database->getAllAssoc();
//Make table headers
?>
<div id="train_select_table">
<form name="selector" method="post" action="customer_list.php?user_id=<?php echo $_SESSION['user_id']?>">
<table>
<tr>
<th>Train name</th>
<th>Number of bogies</th>
<th>Number of axles</th>
<th>Delete</th>
<th>More info</th>
<th>Check</th>
<!--Only for admins (later!)-->
<!--<th>XML</th>
<th>SQL</th> -->
</tr>
<div id="looprow">
<?php
foreach($results as $res){
//Loop trough results, generate a tablerow every time
?>
<tr>
<td name="train_name"><?php echo $res['train_name']?></td>
<td><?php echo $res['number_of_bogies']?></td>
<td><?php echo $res['number_of_axles']?></td>
<td>Delete</td>
<td>More Information</td>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>
<!--Only for admins (later!)-->
<!--<td>XML</td>
<td>SQL</td>-->
</tr>
<?php
}
?>
</div>
</table><br />
<input name="Add to list" type="submit" id="add_to_list" value="add_to_list">
</form>
</div>
<?php
}
?>
Function:
function getAllAssoc() {
$sql = "SELECT * FROM train_information WHERE train_name = :train_name";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":train_name", $_POST["train_name"]);
$sth->execute();
return $sth->fetchAll();
}
function selector() {
$sql = "SELECT train_name, train_id FROM train_information";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
I understand why it keeps replacing the old row. this is because I send a new query. but I don't know how to keep the old one.
as each time user will chose a single value while not mutiple value, so if you want to use the new value and the old value too, you should store the old value using cookie or session or hidden input in that form. It depends on you.
E.g. at the beginning, do this:
<?php
session_start();//make it be before your output if you want to use session or cookie
if(!$_SESSION['train_name']) $_SESSION['train_name'] = array();
if(isset($_POST["train_name"])) $_SESSION['train_name'] = array_merge ($_SESSION['train_name'], $_POST["train_name"]);
.......
//next step, inquiry with $_SESSION['train_name']
Simply change the name of your multiple select list from train_name to train_name[] as follows:
...
echo "<select name='train_name[]' id='train_name' multiple='multiple'>";
...
By this way your $_POST['train_name'] will be an array passed to bindParam
Below code !Gives me check boxes and a delete button, In input tag all check box have same name (check)!! There check box can be retreive from database with id .
Problem Is:: when I am selecting multiple checkbox for deletion...only last one checkbox is deleted ! means it can delete 1 data from a database.
url like -> http://localhost/demo/delete.php?check=10&check=13&check=14&submit=Delete
I need while I am selecting a checkbox more than 1 checkbox ,check box datas is deleted from database ! Any one help me to overcome this problem thanks
index.php
<?php
$sql = mysql_connect('localhost', 'root', '');
mysql_select_db('database_section', $sql);
?>
<form name="checkbox" method="get" action="delete.php">
<table>
<tr>
<?php
$sql = "select * from data";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
?>
<td><input type="checkbox" name="check" value="<?php echo $row['id']?>"><?php echo $row['data'];?>
</td>
<?php
}
?>
<tr>
<td><input type="submit" name="submit" value="Delete"></td>
</tr>
</table>
</form>
Now, In delete.php..code below...
<?php
$sql = mysql_connect('localhost', 'root', '');
mysql_select_db('database_section', $sql);
if ($_REQUEST['submit']) {
$abc = $_GET['check'];
$sql = "Delete from data where id=$abc";
$result = mysql_query($sql) or die(mysql_error());
if (isset($result)) {
echo "data deleted";
}
else
{
echo "not possible";
}
}
?>
Use check box as an array holder. name it as check[] to hold all selected values. And on post you will get the selected array list.
Now your $abc will be a array, use foreach in delete.php to get the checked ids.
Change name="check" to name="check[]"
See more here: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
[...]
while ($row = mysql_fetch_array($result))
{
?>
<td>
<input type="checkbox" name="check[]" value="<?php echo $row['id']?>"><?php echo $row['data']; ?>
</td>
<?php
}
?>
[...]