I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";
Related
I'm trying to create an html form to provide information about the salaries table in my database, the user should be able to pick year between 1986-1996 and choose if she wants to see the total salary of that year or the average salary of that year.
I have no idea how I link up these scripts and I can't find much online.
html file:
<html>
<body>
<fieldset>
<form id="frmName" method=post action="Oppgave4.php" onsubmit="">
<h1>Oppgave 4</h1>
Choose year:
<select id="frmName" onChange="">
<option selected disabled hidden>----</option>
<option name="1986">1986</option>
<option name="1987">1987</option>
<option name="1988">1988</option>
<option name="1989">1989</option>
<option name="1990">1990</option>
<option name="1991">1991</option>
<option name="1992">1992</option>
<option name="1993">1993</option>
<option name="1994">1994</option>
<option name="1995">1995</option>
<option name="1996">1996</option>
</select>
Total or average salary:
<select id="frmName" onChange="">
<option selected disabled hidden>----</option>
<option name="Total">Total salary</option>
<option name="Average">Average salary</option>
</select>
<input type="submit" value="Submit" id="submit">
</p>
</form>
</fieldset>
</body>
</html>
php file:
<?php
$year = ($_POST['1986'], $_POST['1987'], $_POST['1988'], $_POST['1989'], $_POST['1990'],
$_POST['1991'], $_POST['1992'], $_POST['1993'], $_POST['1994'], $_POST['1995'],
$_POST['1996'], $_POST['Total']);
$average = $_POST['Average'];
$conn = mysqli_connect("localhost", "root", "", "employees");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlavg = "SELECT AVG(salaries.salary) AS average FROM salaries
WHERE from_date = '$year'";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["average"] ."</td></tr>";
}
echo "</table>";
$sqlsum = "SELECT SUM(salaries.salary) AS total FROM salaries
WHERE from_date = '$year'";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["total"] ."</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
What you're trying to do is literally PHP/MySQL 101 and there is a lot online on how to do this. Having said that you are making some mistakes in your code. First, you should name the <select>
<select name="year">
Then you should give each option a value:
<option value="1994">1994</option>
...// do each one like this
This way, when the form is submitted to the PHP you can find it in the POST array:
$year = $_POST['year'];
That is just a start. You have a second drop-down that also needs a name and each option should have a value attribute.
<select name="calculation_type">
<option>----</option>
<option value="Total">Total salary</option>
<option value="Average">Average salary</option>
</select>
Which will be found like this in the POST array:
$average = $_POST['calculation_type'];
Your form needs a name and does not need the onsubmit The action should be the name of the PHP script which will perform the calculations:
<form name="form_name" method=post action="Oppgave4.php">
Warning
Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!
Suggestion
You should go work through some basic PHP tutorials like those offered by https://www.learn-php.org/ (a free, interactive website) or other services
I want the dropdown to show the "client_code", "name" in one line. It almost works but not 100%. I am a beginner with php and SQL, can someone help me please?
Code that doesn't work
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$queryKlantCode = "SELECT klant_code FROM klant";
$resultKlant=mysqli_query($mysqli,$queryKlant);
$resultKlantCode=mysqli_query($mysqli,$queryKlantCode);
while($row=mysqli_fetch_array($resultKlant) &&
$row2=mysqli_fetch_array($resultKlantCode) )
{
?>
<option><?php echo $row[0]. ", ". $row2[0];?></option>
<?php
}
?>
</select>
</form>
Code that only works with retrieving name in dropdown from database
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$res=mysqli_query($mysqli,$queryKlant);
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>
</form>
You can select more than one column from a table in the same select, and as both these columns live in the same table it makes producing this result much simpler.
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$sql = "SELECT naam, klant_code FROM klant";
$result = mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result)){
?>
<option><?php echo $row[0]. ", ". $row[1];?></option>
<?php
}
?>
</select>
</form>
You probably want to do this with your <option> tag as well rather than put the name and code in the visible portion
<option value="<?php echo $row[1];?>"><?php echo $row[0];?> </option>
And if you use mysqli_fetch_assoc() you can use the columns names so you know what you are putting where
while($row=mysqli_fetch_assoc($result){
<option value="<?php echo $row['klant_code'];?>"><?php echo $row['naam'];?> </option>
I am looking for a way to search for data from the database using input type box list, I tried make the code but it doesn't display anything:
html code:
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
php code:
if (isset($_POST['users'])) {
$key = trim ($_POST['users']);
$s = "SELECT * FROM users where user_name LIKE '%$key %'";
$res = mysql_query($s) or die('query did not work');
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
when I try the code I didn't get any result and when I remove the while loop and put this instead of it :
<?php echo $key; ?>
it gives me the numbers of the selected value, for example if I select user2 the result will be 2. and I want the result to be user id and user name.
you need to fetch all the user name in your drop down select box
<select name="users">
<option selected="selected" value="">-- select --</option>
<?php $s2 = "SELECT * FROM users";
$q2=mysql_query($s2) or die($s2);
while($rw=mysql_fetch_array($q2))
{
echo '<option value="'.$rw['userid'].'">'.$rw['username'].'</option>';
}</select>
?>
<?php if (isset($_POST['search'])) { // submit button name here
$key = $_POST['users'];
$s = "SELECT * FROM users where user_id='".$key."'";
$res = mysql_query($s) or die($s);
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
edit your html to this,you will get the in $_POST which will be in value='something'
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
Or if value is the id of user then change query to this
$s = "SELECT * FROM users where user_id='".$key."'";
I want my program to list my data from mysql table in a list in a dropdown menu on my page.
Here's my code:
<fieldset>
<legend> Selecteer uw Categorie </legend>
<label for ="Categorie"> Categorie </label>
<select name ="Categorie" id="Categorie">
<datalist id ="Categorie">
<Option Value="Router">Router</option>
<Option Value="Switch">Switch</option>
<Option Value="Toestel">Toestel</option>
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<Option Value= <?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<ul>";
echo "<li>" . $row['Categorieen1'] . "</li>";
echo "</ul>";
}
echo "</table>";
mysqli_close($con);
?>
</option>
</select>
</datalist>
</fieldset>
THis code works perfectly, It looks up the data i need and posts it in the dropdown list But it all gets posted in a single line..
I want it to be Listed underneath each other..
Please help me!
They are all on a single line because you put your result in a single <option> tag.
try this:
<Option Value="Basisstation">Basisstation</option>
<Option Value="Repeaters">Repeaters</option>
<?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "<option>Failed to connect to MySQL: " . mysqli_connect_error()."</option>";
}
$result = mysqli_query($con,"SELECT * FROM Categorien");
while($row = mysqli_fetch_array($result))
{
echo "<option>".$row['Categorieen1'] . "</option>";
}
mysqli_close($con);
?>
</select>
</datalist>
</fieldset>
EDIT:
sorry for the mistake I forgot the second echo in the while loop XP!
I will like to get multiple request after clicking submit. Meaning that after i clicked submit and get the result, i will like to inquire results again and click submit again. I don't want to lead myself back to nothingness.
<?php
error_reporting(0);
if (!$_POST['submit'])
{
<form action="http://localhost/test.php" method="post">
Type of Leave:
<select name="leave">
<option value=""selected="selected"></option>
<option VALUE="Medical Leave"> Medical Leave</option>
<option VALUE="Unpaid Leave"> Unpaid Leave</option>
</select>
<input type="submit" name="submit" value="Check!" />
</form>
<?php
}
else
{
$conn=odbc_connect("employee","","") or die (odbc_errormsg());
if (!$conn)
{
exit
("Connection Failed: " . $conn);
}
else
{
$choice = $_POST['leave'];
$sql="SELECT * FROM balance WHERE ID=$username";
$rs=odbc_exec($conn,$sql);
?>
<?php
while (odbc_fetch_row($rs))
{
$choice=odbc_result($rs,"$choice");
echo "<tr><td>$choice</td>";
}
odbc_close($conn);
echo "</table>";
}
}
Currently the code itself queries out the expected output that I want, but the only downside is it stays there as an output. What my aim is to have the submit and the select choice option stays there so that I can continue to output the result I want, thanks.
The best user experience would be to use AJAX to dynamically change the results table depending on what is chosen in the dropdown. For a simpler solution, you could try moving the form up and out of the if/else loop. Put the form first, then after the form prints, check for if ($_POST['submit']).
In this example, I also added a little function to add selected="selected" to whichever option was chosen for this submit. It's very rough, but you get the idea.
<?php
function matchPost($name,$val) {
if($val == $name)
echo 'selected="selected"';
}
?>
<form action="http://localhost/test.php" method="post">
Type of Leave:
<select name="leave">
<option value=""></option>
<option <?php matchPost('Medical_Leave',$_POST['leave']); ?> VALUE="Medical_Leave"> Medical Leave</option>
<option <?php matchPost('Unpaid_Leave',$_POST['leave']); ?> VALUE="Unpaid_Leave"> Unpaid Leave</option>
</select>
<input type="submit" name="submit" value="Check!" />
</form>
<?php
if ($_POST['submit'])
{
$conn=odbc_connect("employee","","") or die (odbc_errormsg());
if (!$conn)
{
exit
("Connection Failed: " . $conn);
}
else
{
$choice = $_POST['leave'];
$sql="SELECT * FROM balance WHERE ID=$username";
$rs=odbc_exec($conn,$sql);
?>
<?php
while (odbc_fetch_row($rs))
{
$choice=odbc_result($rs,"$choice");
echo "<tr><td>$choice</td>";
}
odbc_close($conn);
echo "</table>";
}
}