This is my code but I don't seem to get anything in the dropdown list. Is there something else I'm supposed to do besides this? Or is there something wrong with my code?
<div class="span10 offset1">
<div class="row">
<h3> Add catagory</h3>
</div>
<select class="selectpicker" data-style="btn-success" >';
<?php
include('database.php');
$query = "SELECT cat_name FROM catagory";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
echo "</select>";
?>
</div>
You're referencing $row but assigning the result to $r. Just change the variable:
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $r['cat_name'] . " '>" . $r['cat_name'] . " </option>";
}
Your variables names look wrong
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
You loop through the results using $r but use $row[] within the loop. It should probably read
while($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
Related
I have a form that queries my server for data using MySQL. I am using a form that sends get requests. It doesn't show anything and I don't know why. I is so strange because my query is valid and I tested it on PHPmyadmin..I am not striving for answers only, I want to know why this happened and what is the reason behind it.
Here is my code:
<form name="get" action="Roster.php" method="get">
<select name="course" id="course">
<?php
$get = mysqli_query($con, "SELECT teaching.Course_ID FROM `teaching` WHERE teaching.F_ID=213000000 ");
while ($row = mysqli_fetch_assoc($get)) {
echo '<option value ="' . $row["Course_ID"] . '"> ' . $row["Course_ID"] . ' </option>';
}
?>
</select>
<select name="group">
<?php
$get = mysqli_query($con, "SELECT `Group_ID` FROM `teaching` WHERE `teaching`.F_ID= 213000000");
while ($row = mysqli_fetch_array($get)) {
echo '<option value ="' . $row["Group_ID"] . '"> ' . $row["Group_ID"] . ' </option>';
}
?>
</select>
<date-util format="yyyy-mm-dd">
<label for="Date" > Date </label><input id="meeting" name="date" type="date" />
</date-util>
<input type="submit" name="Send" value="Get"/>
</form>
<?php
if ($_GET['submit']) {
$sql = " SELECT enrollment.S_ID,student.ID,student.F_Name,student.L_name,attendance.Status,attendance.Date
From enrollment
INNER JOIN student On enrollment.S_ID
INNER JOIN attendance On enrollment.S_ID
where enrollment.Course_ID =" . $_GET["course"] . "and enrollment.Group_ID =" . $_GET["group"] . "and attendance.date =" . $_GET["date"] . " ";
$result = mysqli_query($con, $sql);
$message = "Please Choose Course_ID and Group_ID ";
if ($result > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Hello";
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['F_Name'] . " " . $row['L.name'] . '</td>';
echo '<td>' . $row['Date'] . '</td>';
echo '<td>' . $row['Status'] . '</td>';
echo "</tr>";
}
} else {
echo "<tr>";
echo '<td>' . $message . '</td>';
}
}
?>
$_GET['submit'] does not exist. You need to add submit as your name attribute to your button like so
<input type="submit" name="submit" value="Get"/>
Also you should use prepared statements to prevent SQL injection attacks.
Though I have researched, I couldn't find any solution for this. I need to get the database value ("Default") as the pre-selected value of the drop down list.
<select name="listCustomer" id="listCustomer">
<?php
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";}
?>
</select>
Can you please help me on this?
Just create a variable before the echo, something like:
$selected = ((strtolower($row['customer_name']) == 'default') ? 'selected' : '');
then change the echo to this:
echo '<option '.$selected.' value="'.$row['customer_name'].'">'.$row['customer_name'].'</option>';
This can be accomplished using an if statement on the customer_name
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
if($row["customer_name"] === "Default"){
echo "<option value=\"" . $row['customer_name'] . "\" selected>" . $row['customer_name'] . "</option>";
} else {
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";
}
}
?>
Note the selected tag on the first echo.
$query="SELECT customer_name FROM customers";
$result = #mysql_query ($query);
echo "<select name=customer_name value=' '>";
while($drop=#mysql_fetch_array($result)){
echo "<option value=$drop[customer_name]>$drop[customer_name]</option>";
}
echo "</select>";
I have a dropdown in php the value is coming from database. In the edit mode i need to show selected value in to drop down. My php is below:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
Try this code...
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>
I have got this table in my Database:
**train_information**
train_id
country_id
user_id
train_name
tare_weight
number_of_bogies
number_of_axles
wheel_diameter_min
wheel_diameter_max
And then i have 2 .php pages (1 is only for classes)
Selector.php
<!--Selector-->
<form name='form' id='selector'>
<?php
$selector = $database->selector();
?>
<select onchange= "mySelection()" id ="selector" name="selector">
<option selected="true" disabled="disabled">Selecteer een trein</option>
<?php
foreach ($selector as $selector) {
print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
if ($selector['train_id'] == $selector_id) {
echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
} else {
echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
} }
?>
</select>
</form>
And then the class.php
function selector() {
$sql = "SELECT train_id, train_name FROM train_information";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
The selector shows the train names in the selection. so that is good.
But i want that when i select it, and press a button like: Select. That it select the information of that selected train and shows it on the page.
And i don't only want to show 1, but if i would like to select a other train after that, it shows both trains information on the page.
How do i do this?
You can do following
Add this Method to class.php
function select_train_details($trainId) {
$sql = "SELECT * FROM train_information where train_id = " . $trainId;
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
Add Following to selector.php
<form name='form' id='selector'>
<?php
$selector = $database->selector();
if (isset($_POST['selector'])) {
$selectorDetail = $database->select_train_details();
}
?>
<select onchange= "this.form.submit" id ="selector" name="selector">
<option selected="true" disabled="disabled">Selecteer een trein</option>
<?php
foreach ($selector as $selector) {
print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
if ($selector['train_id'] == $selector_id) {
echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
} else {
echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
}
}
?>
</select> </form> <?php if (isset($selectorDetail)) {
echo "<pre>";
$selectorDetail; } ?>
Ive got a property website im working on for a school project http://www.holidayaviemore.com
I have three dropdown lists which have all been generated from a mysql database.
I have added a submit button so that when the options have been selected the database info will display..this is where I am really stuck!
Im not sure what code of functions are needed to diplay the content on the page or on another page..any ideas? This is the code for the dropdownn boxes and form I have so far..
EDIT
Basically I want the options selected to be displayed on a webpage http://www.holidayaviemore.com shows the dropdown lists
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>
I think you need this. You need to see if form already submitted and add selected in option tag where it needed.
<p>Select Options From Below to find property</p>
<form action="#" name="form" id="form" method="post">
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='availability'><option value=''>- Availability?--</option>";
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['availability']) and $row['availability'] == $_POST['availability']){
echo "<option value='". $row['availability']. "' selected>" . $row['availability'] ."</option>";
}
else{
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
}
echo "</select>";
?>
<?php
$sql = "SELECT bedrooms FROM properties LIMIT 10";
$result = mysql_query($sql);
echo "<select name='bedrooms'><option value=''>--Please Choose Bedrooms Bedrooms--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['bedrooms']) and $row['bedrooms'] == $_POST['bedrooms']){
echo "<option value='" . $row['bedrooms'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['bedrooms'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<?php
$sql = "SELECT sleeps_min FROM properties LIMIT 12";
$result = mysql_query($sql);
echo "<select name='sleeps_min'><option value=''>--Please Choose Guests--</option>";
$count = 1;
while ($row = mysql_fetch_array($result)) {
if(isset($_POST['sleeps_min']) and $row['sleeps_min'] == $_POST['sleeps_min']){
echo "<option value='" . $row['sleeps_min'] . "' selected> " . $count . "</option>";
}
else{
echo "<option value='" . $row['sleeps_min'] . "'> " . $count . "</option>";
}
$count++;
}
echo "</select>";
?>
<input type="submit" name="" value="GO" />
</form>