How do I put names from table into dropdown menu? - php

Im trying to figure out why the dropdown doesnt show up of the list of teachers in my tables
include"teacher.php"
<body onload="displayDate()">
<img src="Raw Pictures/Header.jpg" style="width:100% ; height:15%">
<img src="Raw Pictures/green.png" style="width:8%; height:11%; position:absolute; top:4% ;left: 3%">
<br><br><br><div>
Teacher:
<select>
<option>echo $row</option>
</select>
</body>
</html>
then the teacher.php
<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('teacher_account');
$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {}
?>
I cant figure out how to show the teachers from database into dropdown box? how do I fix it?

You are probably looking for something like that:
<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('teacher_account');
$sql = "SELECT teachername,facultyname FROM subj_eva";
$result = mysql_query($sql);
?>
<html>
<body onload="displayDate()">
<img src="Raw Pictures/Header.jpg"
style="width:100% ; height:15%">
<img src="Raw Pictures/green.png"
style="width:8%; height:11%; position:absolute; top:4% ;left: 3%">
<div>
Teacher:
<select>
<?php while ($row = mysql_fetch_array($result)) { ?>
<option value="<?= $row['teachername'] ?>">
<?= $row['teachername'] ?> (<?= $row['facultyname'] ?>)
</option>
<?php } ?>
</select>
</body>
</html>
Of course the code can be separated into files, I bundled it for the sake of readability here. It probably makes sense to move those styling rules into a separate css file instead of using those ugly inline styles.
A side note: you are using the outdated and deprecated mysql extension. You should switch to either mysqli or PDO and learn about the security advantages of "prepared statements". Also you should not use the root account for normal database use. Create a less privileged account and only use the root account for administrative tasks.

Try this
$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);
echo "<select>";
while ($row = mysql_fetch_array($result))
{
echo "<option>$row['column']</option>";}
echo "</select>";
?>

<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('teacher_account');
$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);
while ($result_array = mysql_fetch_array($result, MYSQL_ASSOC) ) {
$row = $row .'<option>'. $result_array["facultyname"] .'</option>';
}
?>
and php for generate html file
<?php
include"teacher.php";
?>
<body onload="displayDate()">
<img src="Raw Pictures/Header.jpg" style="width:100% ; height:15%">
<img src="Raw Pictures/green.png" style="width:8%; height:11%; position:absolute; top:4% ;left: 3%">
<br><br><br><div>
Teacher:
<select>
<?php
echo $row;
?>
</select>
</body>
</html>

Related

Can't print certain values from database in PHP

I'm using this database and I have created a table in my PHP file where I list all of these values. I have link my "subject" values to a different file which is supposed to display "description", but every time when I click on "Subject" I get the description from the first input in the table.
This is my code for displaying the table
<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
?>
<?php
ob_start();
include 'organize_meeting.php';
ob_end_clean();
$mysqli = new mysqli("localhost", "root", "", "users");
$curr_user = $loggedInUser->username;
mysql_connect('localhost', 'root', '');
mysql_select_db('users');
$query = "SELECT * FROM uc_messages WHERE `to` = '$curr_user'";
$records = mysql_query($query);
echo "
<body>
<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h2>My Meetings</h2>
<div id='left-nav'>";
include("left-nav.php");
echo "
</div>
<div id='main'>
</div>
<div id='bottom'></div>
</div>
</body>
</html>";
?>
<table width = "600" border = "1" cellpadding = "1" cellspacing = "1">
<tr>
<th>From</th>
<th>Subject</th>
<th>Beginning of meeting</th>
<th>End of meeting</th>
</tr>
<?php
while($msgs = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$msgs['from']."</td>";
echo "
<td>
<a href = 'info.php' target = '_blank_'> ".$msgs['subject']." </a>
</td>
</html>";
echo "<td>".$msgs['TimeFrom']."</td>";
echo "<td>".$msgs['TimeTo']."</td>";
echo "<tr>";
}
?>
And this is my code that is suppoused to display the description
<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
?>
<?php
ob_start();
include 'organize_meeting.php';
ob_end_clean();
$mysqli = new mysqli("localhost", "root", "", "users");
$curr_user = $loggedInUser->username;
mysql_connect('localhost', 'root', '');
mysql_select_db('users');
$query = "SELECT * FROM uc_messages WHERE `to` = '$curr_user'";
$records = mysql_query($query);
echo "
<body>
<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h2>About Meeting</h2>
<div id='left-nav'>";
echo "
</div>
<div id='main'>
</div>
<div id='bottom'></div>
</div>
</body>
</html>";
?>
<?php
$msgs = mysql_fetch_assoc($records);
echo "<p>".$msgs['from']."</p>";
echo "<p>".$msgs['description']."</p>";
echo "<p>".$msgs['TimeFrom']."</p>";
echo "<p>".$msgs['TimeTo']."</p>";
?>
I always get the same description from the database, although I click on a different input.
Note: You are mixing up mysqli.* and the mysql together and you have to convert to mysqli.* the entire query that you have. You have to fix all the mix up codes that you have and after that you follow the code that i have given.
And the below example i am giving in the statement of the mysqli.* and you have to make changes to the code.
File One:
You are missing to pass the ID of the subject or the auto increment ID in the <a> tag which you want to display.
Hence the following <a> tag will be as follows.
Replace:
<td>
<a href = 'info.php' target = '_blank_'> ".$msgs['subject']." </a>
</td>
With:
<td>
<a href = 'info.php?subject_id=".$msgs['id']."' target = '_blank_'> ".$msgs['subject']." </a>
</td>
File Two:
Hence you can get the Request that we have passed in the <a> tag over here and we shall manipulate it.
$msgs = mysqli_fetch_assoc($records)
echo "<p>".$msgs['from']."</p>";
echo "<p>".$msgs['description']."</p>";
echo "<p>".$msgs['TimeFrom']."</p>";
echo "<p>".$msgs['TimeTo']."</p>";
Remove these lines since it makes no sense. Already you have connected to the DB using the mysqli connectivity.
mysql_connect('localhost', 'root', '');
mysql_select_db('users');
Change the query like this so that it will be the mysqli.* way.
$query = "SELECT * FROM uc_messages WHERE `id` = '".$_REQUEST['subject_id']."'";
$records = mysqli_query($mysqli,$query);

Issue with bootstrap grid and mysql response

I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections

Php Variable Data Allocation

I wonder why the 2nd output $devNO gives me bool(false) which is empty. i have no idea about it. Am I wrong at the mysql_query() part?
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
var_dump($developer);
echo "<br>";
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
Here's my output:
Here I will show my full code for "games.php":
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<html>
<head lang="en">
<meta charset="utf-8" />
<title>Game List</title>
<link rel="stylesheet" href="css.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<style>
legend {font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif}
</style>
</head>
<body>
<div id="big_wrapper">
<header id="top_header">
<h1>Dandy's Game Library</h1>
</header>
<nav id="top_menu">
<ul>
<li><strong></stron>Home</strong></li>
<li><strong>Game List</strong></li>
</ul>
</nav>
<div id="game_wrapper">
<section id="filter">
<form action="games.php" method="post" name="search_form">
<fieldset>
<legend><h3><strong>Search</strong></h3></legend>
<strong>Developer</strong><br>
<select name="dev">
<option value="">--Select--</option>
<?php
include("dbConnection.php");
mysql_connect("localhost","root","");
mysql_select_db("games");
$sql = mysql_query("SELECT Publisher FROM publisher");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Publisher']."'>" .$row[Publisher]. "</option>";
}
?>
</select>
<br/><br/><strong>Game Platform</strong><br>
<select name="plat">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Platform FROM platform");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Platform']."'>" .$row[Platform]. "</option>";
}
?>
</select>
<br/><br/><strong>Genre</strong><br>
<select name="gen">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Genre FROM genre");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Genre']."'>" .$row[Genre]. "</option>";
}
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
?>
</select>
<br><br><input type="submit" name="search" value="Search"></input>
</fieldset>
</form>
</section>
<aside id="items">
<fieldset>
<legend><h3><strong>Game List</strong></h3></legend>
<?php
var_dump($developer);
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>
<div class="row">
<div class="cell"><?php echo $row[$game_title]?></div>
<div class="cell"><?php echo "Year : ".$row[$game_year]?></div>
<div class="cell"><?php echo "Language : ".$row[$game_lan]?></div>
<div class="cell"><?php echo "Price : RM".$row[$game_price]?></div>
</div>
<?php
}
?>
</div>
</fieldset>
</aside>
</div>
</div>
<div id="btm_wrapper">
<footer id="foot">
<strong></strong>
</footer>
</div>
</body>
</html>
The full output will be like this after i search from dropdown:
The method mysql_query() , if successful, returns a resultset, which has to be made an array even if only a single value is being returned from the query.
Ideally there should be a for-each iteration on this array to get result, however if you're sure you'll get at least one value, the first row with desired index('No' in your case).
$dataset= mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$row = mysql_fetch_assoc($dataset);
$devNO = $row['No'];
Also note the quote I've used for varchar. It was correctly pointed in the other answer. Try Now.
If $developer is a string use quotes. You need to first fetch results from result set and than use it in code:
<?php
$devNOResult = mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$platNOResult = mysql_query("SELECT No FROM platform WHERE Platform = '$platform'");
$genNOResult = mysql_query("SELECT No FROM genre WHERE Genre = '$genre'");
$devNO = mysql_fetch_row($devNOResult);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>

MySQL/PHP Only Returning One Result

I have seen a few topics like this so i would like to say sorry first but none of the solutions given in those topics fixed my issue.
I am trying to make a dynamic navigation menu using php and mysql, i have multiple inputs in the database as dummy text but for some reason it only wants to show one result..
Here is my code:
<?php
require_once("../includes/connect.php");
include("../includes/header.php");
include("../includes/functions.php");
?>
<div id="content">
<table id="table">
<tr>
<td id="nav">
<ul class="info">
<?php
$result = mysql_query("SELECT * FROM information LIMIT 10", $connection);
while ($row = mysql_fetch_assoc($result)){
echo "<li>{$row["menu"]}</li>";
$result = mysql_query("SELECT * FROM pages WHERE information_id ={$row["id"]} LIMIT 10", $connection);
echo "<ul class=\"pages\">";
while ($row = mysql_fetch_assoc($result)){
echo "<li>{$row["menu"]}</li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="main">
<h2>Main Content</h2>
</td>
</tr>
</table>
</div>
<?php
include("../includes/footer.php");
?>
</body>
</html>
Dont hate because there are tables in here lol, its not for public use and i will be converting it to grids when i get it all set up and functioning. I am not a 'pro' php programmer so be nice!
I guess the problem is here..
your are pushing datas to the same variables twice....
make first query and push to $result. again make another query and push datas to same var $result.
try pushing it to seperate variable
$result1 = mysql_query("SELECT * FROM pages WHERE information_id ={$row['id']} LIMIT 10", $connection);
echo "<ul class=\"pages\">";
while ($row1 = mysql_fetch_assoc($result)){
echo "<li>{$row1["menu"]}</li>";
}

update value from '0' to '1' on image click?

I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());

Categories