I am trying to create a dropdown menu which has values from the database and when selected it should show all the information that are connected to the selected value.
In MySQL I have three tables, person, address and resume. I created two drop-down menu's(see code below) which are showing the values from address > address_state and address > address_city. What needs to happen is when I selected a state it will show only the persons living in the selected state. And when selecting city it has to show all the persons living in the same city.
My drop-down menu select state only:(got the same code city)
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT address_state FROM address ORDER BY address_state asc";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=address value=''>Student Name</option>"; // list box select command
foreach ($conn->query($sql) as $row){//Array or records stored in $row
echo "<option value=>$row[address_state]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
I know you cannot provide any code because I don't have any code to show which actually "selects" the persons. But I have searched on Google for tutorials and I did not find one which did the thing I needed. So I hope you guys can help me or can provide any tutorials.
Replace this line:
echo "<option value=>$row[address_state]</option>";
With
echo "<option value='".$row['address_state']."'>".$row['address_state']."</option>";
First replace:
echo "<select name=address value=''>Student Name</option>";
To:
echo '<select name="address">';
Next replace this:
echo "<option value=>$row[address_state]</option>";
With:
echo '<option value="'.$row[address_state].'">'.$row[address_state].'</option>';
Related
I'm trying to recreate a make a booking form. They have a drop down select option with 3 values from the room table in the database. In the room table I have the values roomID, roomname, roomtype and beds. I need to have roomname, roomtype and beds displayed as one option in the drop down. So the 3 values will be on the same line if that makes sense. So for example it will say Brown, Double, 2beds and then I can select the options and will view another set of data. I currently know how to make a drop down with only one value in the line but how would I make a drop down menu that presents 3 values from a table?
Below is my code so far:
<?php
include "config.php";
$db = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE);
$query = $db->query("select roomname, roomtype, beds from room");
echo '<select room="roomname">';
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['roomname'].'">'.$row['roomname'].'</option>';
}
echo '</select>';
?>
I think this is what you are expecting.If you want to show roomname, roomtype, beds in one line just concatenate it.
<?php
include "config.php";
$db = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE);
$query = $db->query("select id,roomname, roomtype, beds from room");
echo '<select name="roomname">';
while ($row = $query->fetch_assoc()) {
echo '<option value="'.$row['id'].'">'.$row['roomname'].'-'.$row['roomtype'].'-'.$row['beds from room'].'</option>';
}
echo '</select>';
?>
I used the below code to get the datas or options in my dropdown list from mysql db. also it works fine, but my problem is when i select a particular option from this dropdown list, on submission no value or empty value is saving for that for field. simply i can see the option name, but value is seems like this
value=" ";
actually, value is what i see as option name.
<strong> Select Data </strong>
<select name="data1">
<option value=""> NONE </option>
<?php
//Mysql db connection
$con = mysqli_connect("localhost", "my_user", "my_password", "my_db");
//Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Perform queries
$rs = mysqli_query($con, "SELECT DISTINCT relation FROM relation_names");
if ($rs && mysqli_num_rows($rs)) {
while ($rd = mysqli_fetch_object($rs)) {
echo("<option value='$rd->id'>$rd->relation</option>");
}
}
mysqli_close($con);
?>
</select>
add id in query too
$rs = mysqli_query($con,"SELECT DISTINCT relation,id FROM relation_names");
Then only
$rd->id
will populate correct value
If you want relation as value then do like below:
echo("<option value='$rd->relation'>$rd->relation</option>");
I have two tables, SpeciesHuntBoats contains details of Boats, the Make, Skipper etc. and SpeciesHunt which is generated by the skipper submitting a catch report form with details of their fishing catches. showboats.php displays 5 columns, Year, Boat Name, Make, Skipper, Number Of Species Caught.
For the final column I want a display of the number of rows in SpeciesHunt where that BoatName appears (to give a leaderboard of who has entered the most catches). This is as far as I've been able to get unfortunately, any help much appreciated!
<body>
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Count Rows
$result = mysqli_query($conn, "SELECT * FROM SpeciesHunt");
$num_rows = mysqli_num_rows($result);
// Check row count of entire table works
echo "$num_rows Rows\n";
$sql = "SELECT Year, BoatName, BoatMake, Skipper FROM SpeciesHuntBoats";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Hunt Year</th><th>Boat Name</th><th>Boat Make</th><th>Skipper</th><th>Number Of Species</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Year"]. "</td><td>" . $row["BoatName"]. "</td><td>" . $row["BoatMake"]. "</td><td>" . $row["Skipper"]. "</td><td>
$num_rows</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
Table structures:
SpeciesHunt
SpeciesHuntBoats
For your tables, you should rather make use of a relationship between boats and hunts
You should research a bit into sql relationships.
Currently you are basically saving the boat name, make and skipper twice.
This is a better suggestion for the structure:
Boats:
Year
Name
Make
Skipper
Photo
Hunts:
DateCaught
Angler
Species
Notes
BoatId (This will be a reference to a certain boat record)
In this case you can do a sql query that joins the two tables
select Year, Name, Make, Skipper, count(hunts.id) from boats
join hunts on boats.id = hunts.BoatId
group by Year, Name, Make, Skipper;
What this will return is a certain boat with a total count of hunt records.
Things you can research into:
MySQL relationships
MySQL joins
MySQL aggregates
I'm trying to display more details of the data when the table row <tr> is clicked. But I have no idea how to implement it. Here's my code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hotel_dir";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT hotel_img, hotel_name, hotel_address, hotel_phone FROM hotel";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td><img src=".$row["hotel_img"].
"></td><td>".$row["hotel_name"].
"<br />".$row["hotel_address"].
"</td><td>".$row["hotel_phone"].
"</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Here's the image.
hotel list
The problem is, I'm trying to pass it to hotel.php showing more details of the hotel when the table row is clicked.
You must have an Id field in your hotel table you can include it and have an anchor tag to link to other page with this id,
actually some thing like this:
For SQL statement:
for SQL statement you select id and the previous fields that you had selected already, as usually id is a unique column in tables you can get detail of one row and show all detail of specied id.
Here in this page your sql statement should change to :
$sql = "SELECT id,hotel_img, hotel_name, hotel_address, hotel_phone FROM hotel";
and in the detail.php page your SQL statement must be something like this(of course I didn't consider security stuff):
$sql = "SELECT * FROM hotel where id=".$_GET['id'];
for your table :
Here I just added a tag call detail.php file with parameter id that you can get it in details.php for show details
echo "<tr>
<td><a href="http://yoursite.dev/detail.php?id=".$row["id"]>See Details</a></td>
<td><img src=".$row["hotel_img"]."></td>
<td>".$row["hotel_name"].
"<br />".$row["hotel_address"].
"
</td>
<td>".$row["hotel_phone"].
"
</td>
</tr>";
And in your detail.php file you get parameter id with $_GET['id'] and show more detail for specified id
I have successfully got my php code to connect to my database and retreieve data and display it. I am now trying to take this data and display it in a dropdown menu, however when I do this the dropdown menu displays the correct number of options that correlates with the data in the database(ie. it gives three options if there is 3 values in the db). But it doesn't display the text all the options are blank. Any ideas as to why it is not displaying the text?
<?php
$dbhost= 'Host IP';
$dbuser ='My Username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql='SELECT event_id FROM events';
mysql_select_db('db_name');
$retval= mysql_query($sql, $conn);
echo'<select name=dropdown value=', '>Dropdown</option>';
while($r= mysql_fetch_array($retval))
{
echo "<option value={$r["event_id"]}>{$r["events"]}</option>";
}
echo "</select>";
?>
http://i.imgur.com/30Uq0Ok.png
The link is what the menu currently returns
Thanks
You did not select events from database. You need to select all(*) or required columns by its name in the query .
Change your query:
$sql='SELECT event_id FROM events';
to :
$sql='SELECT * FROM events';
You also need to take option value inside quotes.
Change this line inside loop:
echo "<option value={$r["event_id"]}>{$r["events"]}</option>";
to:
echo "<option value=\"{$r['event_id']}\">{$r['events']}</option>";
try it like this (change your query to select *)
echo '<option value='.$r["event_id"].'>'.$r["events"].'</option>';
You are just selecting event_id from the table. So, $r["events"] is always empty.
Select the title field, too.
<?php
$dbhost= 'Host IP';
$dbuser ='My Username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql='SELECT event_id,event_title FROM events';
mysql_select_db('db_name');
$retval= mysql_query($sql, $conn);
echo'<select name=dropdown value=', '>Dropdown</option>';
while($r= mysql_fetch_array($retval))
{
echo "<option value={$r["event_id"]}>{$r["event_title"]}</option>";
}
echo "</select>";
?>
I assumed, that the field name is "event_title".