I have a table using the variables from the below drop down menu. When the user selects the option in the drop down the table pulls the information based on the variable tied to the selection. How ever when the page first loads it errors out saying the query is missing the variable from the drop down. If I make a selection from the drop down it refreshes the page and resolves the issue. I need the drop down to initially submit the data or whatever is necessary for the query to get its variable on intial page load.
$selected = 'selected = "selected" ';
$Country =$ID_SOCIEDAD;
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
echo " <option ".($row['Fund_Manager_Company_Code'] == $Country? $selected : '')."value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo " </select>
<input type='submit' value='Filter' />";
echo " </form>
Here is the sql query for the drop down menu options:
$result = mysql_query("
SELECT
ID_SOCIEDADADM as Fund_Manager_Company_Code,
DES_SOCIEDAD_CORTO as Fund_Manager_Company_Name
FROM dr_lista_rentabilidad_diaria
GROUP BY ID_SOCIEDADADM
")
or die(mysql_error());
Here is the query for the table:
$result = mysql_query("
SELECT
ID_CARTERA as Fund_ID,
DES_CARTERA_CC as Fund_Name,
DES_CARTERACLASE as Class_Name,
DES_CARTERACLASE_ESP as Special_Class_Name,
FORMAT(POR_RENTCARTERA_C1,2) AS Yield_1month
FROM dr_lista_rentabilidad_diaria
WHERE COD_PAIS = $COD_PAIS
AND ID_SOCIEDADADM = $ID_SOCIEDAD
AND `ID_COLUMNA_C1`= $ID_COLUMNA
ORDER BY DES_CARTERA_CC ASC
")
or die(mysql_error());
You didn't post your query, so it is hard to give an appropriate answer. However, my guess is that you have the variable $_POST['Country'] somewhere in that query. So why don't you do this:
$country = $_POST['Country'];
if (!isset($country)) { $country = 'US'; }
$query = 'SELECT * FROM table WHERE country = ' . $country;
EDIT: As BenM (correctly) pointed out, this code snippet is absolutely not save. You should NOT include this in your production code. It was just meant to give you an idea on how to avoid the SQL error you are getting by always providing a valid value for the country.
Related
I am missing something from my code and I don't know how to make it work. I may have programed it wrong and that could be giving me my troubles. I am new at php and things have been going slowly. please understand that the code my not be organized as it should be. After creating about 12 pages of code I found out that I should be using mysqli or pod. Once I get everything working that will be the next project. Enough said here is my issue. I was able to populate my drop down box and there shows no errors on the page. Also all the data does get inserted into the database except for the section made on the drop down box. Here is my code. I will leave out all of the input fields except the drop down.
<?php
{$userid = $getuser[0]['username'];}
// this is processed when the form is submitted
// back on to this page (POST METHOD)
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
# escape data and set variables
$tank = addslashes($_POST["tank"]);
$date = addslashes($_POST["date"]);
$temperature = addslashes($_POST["temperature"]);
$ph = addslashes($_POST["ph"]);
$ammonia = addslashes($_POST["ammonia"]);
$nitrite = addslashes($_POST["nitrite"]);
$nitrate = addslashes($_POST["nitrate"]);
$phosphate = addslashes($_POST["phosphate"]);
$gh = addslashes($_POST["gh"]);
$kh = addslashes($_POST["kh"]);
$iron = addslashes($_POST["iron"]);
$potassium = addslashes($_POST["potassium"]);
$notes = addslashes($_POST["notes"]);
// build query
// # setup SQL statement
$sql = " INSERT INTO water_parameters ";
$sql .= " (id, userid, tank, date, temperature, ph, ammonia, nitrite, nitrate, phosphate, gh, kh, iron, potassium, notes) VALUES ";
$sql .= " ('', '$userid', '$tank', '$date', '$temperature', '$ph', '$ammonia', '$nitrite', '$nitrate', '$phosphate', '$gh', '$kh', '$iron', '$potassium', '$notes') ";
// #execute SQL statement
$result = mysql_query($sql);
// # check for error
if (mysql_error()) { print "Database ERROR: " . mysql_error(); }
print "<h3><font color=red>New Water Parameters Were Added</font></h3>";
}
?>'
Here is the drop down
<tr><td><div align="left"><b>Tank Name: </b> </div></td><td><div align="left">
<?php
echo "<select>";
$result = mysql_query("SELECT tank FROM tank WHERE userid = '$userid'");
while($row = mysql_fetch_array($result))
{
echo "". $row["tank"] . "";
}
echo "";
?>
</div></td></tr>
You missed some code in while loop.
while($row = mysql_fetch_array($result))
{
echo "<option>".$row['tank']."</option>";
}
echo "</select>";
are you able to build drop down menu or box. if not try this query
$sql="SELECT `tank` FROM `tank` WHERE user_name='$user'";
$result=mysqli_query($dbc,$sql)
//here $dbc is a variable which you use to connect with the database.
Otherwise leave that only read from here why you need to change your code. in the while loop
one one more thing you have to give your select attribute a name, because it will return the value through name so give a name to your select attributes as you are using tank while building your drop down menu so i will give a same name tank. Than you dont have to change anything.
and you have to give value to your option as well, thanks
echo "<select name='age'>";
while($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['tank'] . "' >" . $row['tank'] . "</option>";
}
echo "</select>";
I want to access the value selected by user for further processing.
Hence I am using post method to pass the values of whole form.
But GET to access cust_id so that I can reflect change in
further parts of my form. Hence I had to post the following line:
<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>
outside php code. But now, once I select some option from dropdown menu, URL changes accordingly, but dropdown menu does not reflects the change
<?php
$query = "SELECT Cust_id, Cust_Name, Cust_City FROM Customers";
$result = mysql_query($query);
?>
<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>
<?php
while($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['Cust_id'].'">'.$row['Cust_Name'].','.$row['Cust_City'].'</option>';
}
echo '</select>';
?>
How can I, in the same form, access the address of the particular customer id from database when user selects customer name from this dropdown menu?
I think you mean when you change dropdown, the value is not retained, it obviously won't be because your page is being refresh, you need to GET the value from url and put a selected attribute to have that value selected.
Do it this way:
<?php
$query = "SELECT Cust_id,Cust_Name,Cust_City FROM Customers" ;
$result = mysql_query($query);
//checking if GET variable is set, if yes, get the value
$selected_option = (isset($_GET['product'])) ? $_GET['product'] : '';
//we will store all the dropdown html code in a variable and display it later
$select = "<select id='fullname' onChange=\"window.location='sp_menu.php?product='+this.value\" name='fullname'>";
while($row = mysql_fetch_assoc( $result )) {
//checking if the cust_id matches the GET value,
//if yes, add a selected attribute
$selected = ($selected_option==$row['Cust_id'])?'selected':'';
echo '<option value="'.$row['Cust_id'].'"'. $selected. '>' . $row['Cust_Name'] .' , '.$row['Cust_City']. '</option>';
}
$select .= '</select>';
//display the dropdown
echo $select;
?>
I have a task that I need to retrieve data from the database and set it in the Combo Box. Fortunately, I have done it.
Now, I have a Search Button which retrieves the data relevant in these text and combo boxes. My Issue is, After I click Search Button all my combo box and text box selected values become empty. How can I set those same data after clicking Search button ?
My Code Effort is,
<?php
$sql="select cat_id,cat_name from disease_category group by cat_id ";
foreach ($dbo->query($sql) as $row){
if(isset($_REQUEST['cat_name'])&&$_REQUEST['cat_name']==$row[cat_name])
{
echo "<option value=$row[cat_id] selected='selected' >$row[cat_name]</option>";
}
Else
{
echo "<option value=$row[cat_id]>$row[cat_name]</option>";
}
}
?>
My SEARCH button code,
<?php
include 'config.php';
if(isset($_REQUEST['SUBMIT']))
{
$cat=$_REQUEST['cat'];
$subcat=$REQUEST['subcat']
$sel=mysql_query("SELECT * from table_name where cat_id like '$cat%' AND sub_id like '$sub_cat%'AND survier like '$survier%' ")
}
It should be pretty simple. I still don't fully understand what you're trying to do. But if all you want is to dynamically populate an options list based on the results of a SQL query.
<?php
$sql = '
SELECT
*
FROM
`my_table`
';
$query = mysql_query($sql) OR die(mysql_error());
print '<select name="dropdown">';
while ($row = mysql_fetch_assoc($query)) {
print '<option value="'. $row['cat_id'] .'"';
if (
isset($_REQUEST['cat_name']) &&
$_REQUEST['cat_name'] == $row['cat_name']
) { print ' selected="selected"'; }
print '>'. $row['cat_name'] .'</option>';
}
print '</select>';
?>
You should be able to modify the SELECT query to fit your needs, and modify content within the while() loop, as well. That should get you going if I understand what you're trying to do.
I have a HTML etc.. tags now what I want to achieve is upon a selection of ie. i want to load the related info from database to in a new tag with as many tags.
I am using PHP to do achieve this now at this point if for example i choose option1 then the query behind it retrieves relevant information and stores it in a array, and if I select option2 exactly the same is done.
The next step I made is to create a loop to display the results from array() but I am struggling to come up with the right solution to echo retrieved data into etc. As its not my strongest side.
Hope you understand what I am trying to achieve the below code will clear thing out.
HTML:
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP:
$form = Array();
if(isset($_POST['workshop'])){
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
while($result2 = mysqli_fetch_assoc($query2)){
//The problem I am having is here :/
echo "<select id='Forex' name='Forex' style='display: none'>";
echo "<option value='oko'>.$result[1].</option>";
echo "</select>";
print_r($result2);echo '</br>';
}
}else{
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%'";
$query = mysqli_query($link, $sql);
while($result = mysqli_fetch_assoc($query)){
print_r($result);echo '</br>';
}
}
}
Try this code:
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value='oko'>{$result['course']}</option>";
}
echo "</select>";
echo '</br>';
From the top in your php:
// not seeing uses of the $form I removed it from my answer
if(isset($_POST['workshop'])){
$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?
//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR course LIKE '&$lowerWorkhop%'";
$query = mysqli_query($link, $sql); // here no need to have two results
// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
$select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys.
// to retrieve them, you have to use quotes around the key, hence the change
}
$select.= '</select>';
}
echo $vSelect;
this will output a select containing one option for each row returned by either of the queries. by the way this particular exemple won't echo anything on screen (since your select display's set to none). but see the source code to retrieve the exemple.
I have a select box with three options. When an option is selected a save button is then click which performs a database update on the field. This will involve having two submit buttons in one form. I have created a different action on each submit button, this will be the save button:
if ($_POST['update_status'] == 'Save') {
$keys = array_keys($_POST['order_status']);
//perform the database update to change the option value
}
if(isset($_POST['order_selected'])) {
//send email
}
At the moment my select box for each option is embedded in a table like:
echo '<select name="order_status[] id="id"">';
echo '<option value = "Pending" name="order_status['.$i.']" class = "pending"' . ($row['status'] == 'Pending' ? ' selected=selected' : '') . '>Pending</option>';
echo '<option value = "Approved" name="order_status['.$i.']" class = "approved"' . ($row['status'] == 'Approved' ? ' selected=selected' : '') . '>Approved</option>';
echo '<option value = "Disapproved" name="order_status['.$i.']" class ="disapproved"' . ($row['status'] == 'Disapproved' ? ' selected=selected' : '') . '>Disapproved</option>';
echo '</select>';
now at the moment I have the update query working, except it is outside of the form and works by posting a unique order I.D from the database and then performing he update. however I can only do 'approved'. 'pending' is not an issue as I have already set a flag in the database when an order is created, as default pending.
Instead of have this functionality outside of the form, I would like to be apple to select a dropdown item, hit save, and then the database update query is run changing the value in the select box, within the same form, if this would be possible?.
at the moment I have the query and submit button outside of the form which looks like:
<?php
if (!empty($_POST)) {
$id = intval($_POST['approval']);
$query = "UPDATE Orders SET status = 'Approved' WHERE id = $id";
mysql_query($query);
}
$query = "SELECT ID, Orderno, status FROM Orders WHERE status = '0'";
$result = mysql_query($query);
?>
<select name="approval">
<?php while ($row = mysql_fetch_assoc($result)): ?>
<option value="<?php echo $row['ID']; ?>"><?php echo $row['Orderno']; ?></option>
<?php endwhile; ?>
</select>
<input type="submit" action ="" value="Approve Order" />
Many Thanks, any pointers on how I should be tackling this are much appreciated.
I'm pretty confused by your question, but if you are trying to have the database query run without having the user leave the page, and then update the attributes of some of your HTML elements, you are going to have to use AJAX.