I am trying to populate the options in my html website with information from a database but it does not populate.
All my other entries such as name, last name and address work but this is not working for me. It does not output the firstName values.
<select name="text" id="firstName">
<?php
$sql = "SELECT firstName from Members";
$results = mysql_query($sql);
while ($rows = mysql_fetch_assoc($results)){
?>
<option value="<?php echo $rows['firstName'];?>"><?php echo $rows['firstName'];?></option>
<?php
}
?>
</select>
Related
I have created one IMS in that I am trying to fetch data from one table from the database and display it in drop-down form to another page.
In the database, I have created one table named a party in that table one column named party_name is available and I have to fetch that column data to my order page. Below is my Code. If anyone knows a solution than please help.
<select name="party[]" style="width:100%;" required>
<?php
$result=mysql_query("SELECT * FROM partys");
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['party_name'];?></option>
<?php
}
?>
</select>
Firstly: mysql extension is deprecated, you should use at least mysqli_*:
Secondly: try the below example, replacing the database connection string variables with your database credentials:
<select name="party[]" style="width:100%;" required>
<option value="">-- Please select --</option>
<?php
$dbc = mysqli_connect('localhost', 'userName', 'password', 'databaseName')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['party_name'];?></option>
<?php } ?>
</select>
I have been trying to populate a drop down menu by pulling the needed information from a database through php and mysql however I haven't been able to achieve this and so far when I run it I just get a drop down box with nothing inside of it. Any help on this would be great as I am really stuck as to where I am going wrong.
<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<?php
//Database Query
$query = "SELECT Name FROM dungeon";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
var_dump($result);
while($row = mysqli_fetch_assoc($result)) {
?>
<option value="<?= $row['Name']; ?>"><?= $row['Name']; ?></option>;
<?php
}
?>
</select>
I have connected to the database fine and can pull information down from it to populate a table for example however the drop down menu just isn't working.
NB: I need the value to be set to the name that I am also populating the drop down menu with.
your code works for me, i tried something different hope it works for you.
<select id="dung-name-select" name="dung-select">
<option name="" disabled selected hidden>Name</option>
<option value="*">All</option>
<?php
$result = $connection->query("SELECT Name FROM dungeon");
if ($result) {
while($row = $result->fetch_assoc()) {
?>
<option value="<?= $row['Name']; ?>"> <?= $row['Name']; ?></option>;
<?php
}
}
else {
echo "No dungeons found";
}
?>
</select>
I have 2 PHP files.
File1: A form to select skills from an existing table (Skills table with column name: skill & skill_id)
<select name="skill1">
<?php
$self=$_SESSION['subuser_id'];
$sql = "SELECT * FROM skills";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$skill = $row["skill"];
$skill_id= $row["skill_id"];
?>
<option value='<?php $skill_id?>'><?php echo $skill;?></option>
<?php
}
?>
</select>
File2: I am recalling value through $skill1 =$_POST['skill1']; which is returning null.
I want to pass skill_id of the selected skill in code 1 to $skill1 variable on file2
Change
<option value='<?php $skill_id?>'><?php echo $skill;?></option>
to
<option value='<?php ECHO $skill_id;?>'><?php echo $skill;?></o
I have a form with fields and one dropdown list. The data from the dropdown list comes out the table: categorie. (fields are: catID, catName)
When I select a categorie from the drop down list and fill all the other input fields, it saves all the input fields and only the catName from the categorie table into the tabel: event.
How can I also save the selected catID from the categorie table into the event table?
Can someone helm me out here?
Regards, Benny
<?php require '../Connections/localhost.php'; ?>
<?php
if(isset($_POST['newEvent'])) {
session_start();
$eventName = $_POST['SelCatName'];
$eventSDate = $_POST['Event-StartDate'];
$eventSTime = $_POST['Event-StartTime'];
$eventEDate = $_POST['Event-EndDate'];
$eventETime = $_POST['Event-EndTime'];
$eventDescription = $_POST['Event-Description'];
$catID = $_POST["catID"];
$sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$eventName}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID}')");
header('Location: eventOK.php');
}
?>
<form action="" method="post" name="RegisterForm" id="RegisterForm">
<div class="FormElement">
<select name="SelCatName" class="TField" id="SelCatName">
<option selected="selected" id="0">--Selecteer een categorie--</option>
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option id="<?php echo $ViewAllCategories['catID']; ?>"><?php echo $ViewAllCategories ['catName']; ?> </option>
<?php } ?>
</select>
You made a mistake in <option id="id"></option>
if you wanna take value from select child.
you must change id to value="" or you must add value="id" proper.
change your select box block completely like below..
<select name="SelCatName" class="TField" id="SelCatName">
<option selected="selected" id="0">--Selecteer een categorie--</option>
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option value="<?PHP echo $ViewAllCategories['catID'].'-'.$ViewAllCategories['catName']; ?>"> <?PHP echo $ViewAllCategories['catName']; ?></option>
<?php } ?>
</select>
and a tip for mysql query... to the best of one's ability don't use "select * from" use it if you dont need to all colums better than "*"
$con->query("SELECT catID, catName FROM categorie");
PHP file
<?php require '../Connections/localhost.php'; ?>
<?php
if(isset($_POST['newEvent'])) {
session_start();
//$eventName = $_POST['SelCatName'];
$eventSDate = $_POST['Event-StartDate'];
$eventSTime = $_POST['Event-StartTime'];
$eventEDate = $_POST['Event-EndDate'];
$eventETime = $_POST['Event-EndTime'];
$eventDescription = $_POST['Event-Description'];
$catIDs = $_POST["SelCatName"];
$catID = explode("-", $catIDs);
$sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$catID[1]}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID[0]}')");
header('Location: eventOK.php');
}
?>
to me you are missusing the attribute of ID, Replace the ID for value: your option tag already have the id and name
<select name="SelCatName" class="TField" id="SelCatName">
your code will be:
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option value="<?php echo $ViewAllCategories['catID']; ?>"> <?php echo $ViewAllCategories ['catName']; ?> </option>
<?php } ?>
Therefore you will find your post as only SelCatName = (numerical ID)
Now, if you need that the list of the catID and catName stores to the next page you can re-run your query in the next page and store them in an array.
I have this select box that populates from MySQL Data using PHP
<select>
<?php
$stmt = $pdo_conn->prepare("SELECT * from prices ");
$stmt->execute(array());
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($records as $result2) {
echo '<option value="'.$result2["product"].'">'.$result2["product"].'</option>';
}
?>
</select>
in the same table in MySQL (prices) there is a retail column, how can i put the value of retail into a text input
<input type="text" name="unitprice" />
based on the selected row in the select box?
I'd recommend using JavaScript.
Option 1
I would convert your results into a JSON Array and echo it to JavaScript. This way all information gets given the user.
<select id="mySelectBox" onchange="changeValue(this.value);">
<?php
$json_array = array();
$stmt = $pdo_conn->prepare("SELECT * from prices ");
$stmt->execute(array());
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($records as $result2) {
$product = $result2["product"];
$json_array[$product] = $result2['unitprice'];
echo '<option value="'.$result2["product"].'">'.$result2["product"].'</option>';
}
json_encode($json_array, JSON_FORCE_OBJECT);
?>
</select>
<input type="text" id="unitPrice" name="unitprice" />
<script>
var json = <?php echo $json_array; ?>;
function changeValue(myValue) {
document.getElementById("unitPrice").value = json[myValue];
}
</script>