I have a small data entry form which works well, but someone asked me if I can have the 'name' field as a drop down box of users as opposed to having to type in a name and risk a spelling mistake - yep makes sense.
This is pretty new to me and following some information on here and other sites I have tried to accomplish the first part.. populating the drop down box.. nope. No errors, just nothing in the box.
To power this I have tblStaffNames (userID, txtName)
The code I am using looks like this;
<?php
include("connect-db.php");
$queryNames = "SELECT txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
?>
<select name="personname">
<?php
while ($rowNames = $resultNames->fetch_assoc()) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
?>
</select>
The $conn is all good as on another page I can display data in a table from the database, including tblStaffNames - so I can rule out any sort of connection issues.
When I run the page, the little drop down box appears, very simple like but it's there, just no values.
I will end up using the value like this as part of the data entry form;
<td><select name="personname" style="width:100px" ><?php echo $RowNames; ?></select></td>
But I can't actually get to the point of displaying data.
Can anyone help me out with what I am doing wrong here?
I tested your code with slight changes. It works for me. Please check your DB connection is OK as I have done in my code.
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "staff_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$queryNames = "SELECT txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
//Generating the Dropdown
echo "<select name=\"personname\">";
while ($rowNames = $resultNames->fetch_assoc()) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
echo "</select>";
?>
I dont know what your error but for better understanding i change your code like this
$queryNames = "SELECT userID,txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
?>
<select name="personname">
<?php
while ($rowNames = $resultNames->fetch_assoc()) {
?>
<option value="<?php echo $rowNames['userID']; ?>">
<?php echo $rowNames['txtName']; ?>
</option>
<?php
}
?>
</select>
Following code snippets works for me.
<?php
$servername = "localhost";
$username = "user";
$password = "password";
//DB Server Connection
$conn = mysql_connect($servername, $username, $password) or die("Connection establishment failed");
//DB Selection
$selected = mysql_select_db("staff_db", $conn) or die("Could not select DB");
//Query String
$queryNames = "SELECT txtName FROM tblStaffName";
//Query the DB
$resultNames = mysql_query($queryNames);
//Generating the Dropdown
echo "<select name=\"personname\">";
while ($rowNames = mysql_fetch_array($resultNames)) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
echo "</select>";
?>
Related
I have a notification page. Here's my notification code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM notification ORDER BY date desc";
$result = $conn->query($sql);
?>
<table>
<thead>
<tr style="background-color: #eee">
<th width="20%">Activity</th>
<th width="40%">Description</th>
<th width="20%">Date</th>
<th width="20%">Action</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td width="20%">'.$row['activity'].'</td>';
echo '<td width="40%">'.$row['description'].'</td>';
echo '<td width="20%">'.$row['date'].'</td>';
?>
<td><a href="/test/admin/requests.php?view_id=<?php echo $row['id']; ?>" >VIEW</a></td>
<?php echo '</tr>';
}
// echo '<td width="20%">'.$row['date'].'</td>';
// echo '</tr>';
}
else {
echo "You have no notifications yet";
}
$conn->close();
$conn=mysql_connect("localhost", "root");
mysql_select_db("testdb", $conn);
if (! $conn){
DIE('Could not connect: ' . mysql_error());
}
$query="UPDATE notification set status ='read'";
$retval = mysql_query( $query, $conn );
?>
</tbody>
</table>
Output:
The code behind that VIEW link is:
<td><a href="/test/admin/requests.php?view_id=<?php echo $row['id']; ?>" >VIEW</a></td>
Now, for instance I click the VIEW link in of the "NEW MEMBERSHIP REQUEST" (refer to the photo please), the URL will lead me to that link and it should the information that I need to view or display for that certain member. And that's where my problem is because whenever I click the click, the information don't show up on the page, but the URL gives the correct ID number of that member..
This code is the page where the information of that certain member should appear when ever I click the link. But it display nothing but the correct id number. Help me please.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['id']))
{
$id=$_GET['id'];
$sql = ("SELECT * FROM requests where id = ".$id);
$result = $conn->query($sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
?>
<?php echo $id ?>
<br/>Name: <?php echo $row['name'] ?>
<br/>Age: <?php echo $row['age'] ?>
<br/>Date of Birth: <?php echo $row['dob'] ?>
<br/>Occupation: <?php echo $row['occupation'] ?>
<?php
}
?>
The name, age, date of birth and occupation are examples of the data that I'm saying which doesn't show up. But there was no error. Please please I wish someone could help me out.
ADDITIONAL:
My notification table has 6 fields:
id, user, activity, desc, status, date
Requests table has 5 fields:
id, name, occupation, dob, age
When the user submits his membership form, the details he had input like name, occupation, dob, and age will be inserted requests table and at the same time it will notify the admin though the notification table/page. Now when I click that link, the ID that shows is the ID from the notification and not from the requests where his datas were stored. So I guess that's where my mistake is. And I just realized that. Hope you can still help me figure out how to get that id from the requests table. I hope I explained my problem well. I understand if you guys didn't understand it. Thank you for those who helped and for those who will help me. :)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['id']))
{
$id=$_GET['id'];
$sql = "SELECT * FROM requests where id ='$id' ";
$result = $conn->query($sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
?>
<?php echo $id ?>
<br/>Name: <?php echo $row['name'] ?>
<br/>Age: <?php echo $row['age'] ?>
<br/>Date of Birth: <?php echo $row['dob'] ?>
<br/>Occupation: <?php echo $row['occupation'] ?>
<?php
}
?>
You are mixing OOP with procedural method. Use either one of them. If you are using OOP then don't use this:
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
It should be
$row=$result->fetch_array(MYSQLI_ASSOC);
I am trying to populate a dropdown list with PHP from a MySQL database. Right now nothing is populating in my dropdown. I have made sure I have the correct database name, server name, username, and password.
Here is my PHP file queryfunction.php:
<?php
function connect(){
$servername = "localhost";
$username = "root";
$password = "******";
$database = "lab4";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
mysqli_select_db($database);
}
function close(){
mysqli_close();
}
function query(){
$mydata = mysqli_query("Select * from country");
while($record = mysqli_fetch_array($mydata)){
echo '<option value = "' . $record['CountryAbbreviation'] . '">' .$record['CountryAbbreviation'] . '"</option>';
}
}
?>
Here is is my PHP file where I call this:
<?php
include_once "queryfunction.php";
connect();
?>
<!DOCTYPE html/>
<html>
Further down in the doc within a table:
<tr>
<th>Country</th>
<td>
<select name="CountryDropDown">
<?php query() ?>
</select>
<?php close()?>
</td>
</tr>
Try with change this line while($record = mysqli_fetch_array($mydata)){ to this while($record = mysqli_fetch_array($mydata,MYSQLI_ASSOC)){
or show us your $record variable data
I ve been stuck on this problem for about 3 days now. I would appreciate any help i can with this problem how ever please can you explain where and how i have gone wrong as i would like to learn and understand!
Basically what i am trying to achieve is the following.
A drop down menu, that will display the results of the selected column in my database.
My database has 3 columns "project_name","stage" and "project_details"
If the user selects "stage" it must only display / echo the results form the "stage" column on the screen.
The code below is what i have so far! I know i am subject to an SQL injection but i am trying to get the filter to work first then i will sort that out.
At the moment i keep getting an error on line 42 and 56, i would appreciate any help or input for anyone!
<form action='filter2.php' method='post' name='value' >
<select name="value">
<option value="project_name">project_name</option>
<option value="stage">stage</option>
<option value="project_details">project_details</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
<?php
// Authentication Variables
$servername = "localhost";
$username = "basic";
$password = "redrobinX123";
$dbname = "basic_forms1";
// Make connection
$con = mysql_connect($servername, $username, $password, $dbname);
// If error connecting
if (!$con) {
die(mysql_error("could not connect"));
}
// If post value isset
if(isset($_POST['value'])) {
$column = $_POST['value']; // Set Column
$query = "SELECT". $column . "FROM photo"; // Create Query
$sql = mysql_query($query); // Run Query
// Loop through results
while ($row = mysql_fetch_array ($sql)){
echo "<br>". $row[$column] . "<br>";
}
// Close connection
mysql_close($con);
}
?>
Set it up like this....
// Authentication Variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Make connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// If error connecting
if (!$con) {
die(mysqli_error("could not connect"));
}
// If post value isset
if(isset($_POST['value'])) {
$column = $_POST['value']; // Set Column
$query = "SELECT $column FROM photo"; // Create Query
$sql = mysqli_query($con, $query); // Run Query
// Loop through results
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
echo "<br>". $row[$column] . "<br>";
}
// Close connection
mysqli_close($con);
}
I think you mixing mysql and mysqli functionalities. Try this
<form action='filter2.php' method='post' name='value' >
<select name="value">
<option value="project_name">project_name</option>
<option value="stage">stage</option>
<option value="project_details">project_details</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
<?php
// Authentication Variables
$servername = "localhost";
$username = "basic";
$password = "redrobinX123";
$dbname = "basic_forms1";
// Make connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// If error connecting
if (!$con) {
die(mysqli_error("could not connect"));
}
// If post value isset
if(isset($_POST['value'])) {
$column = $_POST['value']; // Set Column
$query = "SELECT ". $column . " FROM photo"; // Create Query
$sql = mysqli_query($con,$query); // Run Query
// Loop through results
while ($row = mysqli_fetch_array ($sql)){
echo "<br>". $row[$column] . "<br>";
}
// Close connection
mysqli_close($con);
}
?>
PHP newbie here. I have struggling with this code for the past few days-
I have a dropdown menu. The options are coming from a table in my database-
<?php
include('Macintosh HD/Applications/MAMP/htdocs/Deals/processform3.php');
$host = 'localhost';
$username = '';
$password = '';
$database = 'database';
$conn = mysqli_connect($host, $username, $password, $database);
$query = mysqli_query($conn,"SELECT * FROM DealCat");
echo "<form action='processform.php' method='POST'>
<select name = 'dealcat'>/n";
while ($row = mysqli_fetch_assoc($query))
{
echo "<option value='{". $row['dealcat']."}'>" .$row['dealcat']."</option>";
}
echo "</select>\n";
?>
The navigation menu shows up fine on the webpage. However, I am not able to process user-input. I want the user to click on one of the options on my dropdown and PHP runs a script to get the results. I know this could be done with Javascript but I don't know that so trying to use only PHP.
Here is the form process script-
<?php
$host = 'localhost';
$username = '';
$password = '';
$database = 'database';
$conn = mysqli_connect($host, $username, $password, $database);
$dealcat=$_POST["dealcat"];
$query = "SELECT * FROM Deals WHERE dealcategory=\"{$_POST['$dealcat']"");
$result=mysqli_query($conn,$query) or die ("Couldn’t execute query.");
while($row = mysqli_fetch_assoc($result))
{
echo "<p>" . $row['description'] ."</p>";
echo "<br>";
echo "<a href =' {$row['weblink']}'> {$row['Header']}</a>";
echo "<br>";
echo "<br>";
echo "<button >Get Deal</button>";
echo "<hr>";
}
?>
Is there a way that PHP shows results based on user clicking on a dropdown option? Thanks a lot!
Try this
<select name="fieldname">
while ($row = mysqli_fetch_assoc($query))
{
echo "<option value=".$row['dealcat'].">".$row['dealcat']."</option>";
}
</select>
How can I get this to echo or print out the results of a select statement via php?
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
<?php
$query="SELECT * FROM mydb.product";
$result=mysql_query($query);
echo "$result";
echo $result;
?>
I've gotten this far now, but it still returns nothing. Does the problem lie within my mysql table? Or does it fall somewhere else
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$sth = $db->prepare("SELECT * FROM mydb.test;");
$sth->execute();
$result = $sth->fetchAll();
?>
<h1> Im here</h1>
<?php
foreach ($result as $row) {
echo "<tr>";
echo "<td>{$row['name']}</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
?>
<h1> Im still here</h1>
Thanks for all the help
You need to look through the results. If there are, say, 3 columns in each row then your code would look something like this:
for($i=0;$i<mysql_num_rows($result);$i++) {
$row_array=mysql_fetch_row($result);
echo($row_array[0]." ".$row_array[1]." ".$row_array[2]."<br />");
}
As suggested in the comments, you'll have to loop through the fetched results:
$query="SELECT * FROM mydb.product";
$result=mysql_query($query);
// Add these lines:
while ($row = mysql_fetch_assoc($result)) {
var_dump($row);
// each item from the row could be used like this:
// echo $row['field'];
}
More information can be found here: http://php.net/manual/en/function.mysql-query.php
Please note that mysql_query is depricated, and you should consider switching to mysqli (http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/ref.pdo-mysql.php)