pull the data from mysqli to dropdown list php - php

I have been trying to pull the data from database mysqli in the drop down menu. Here is my code for postad.html. I want to pull categories data from categories table with a row name CatName. I also have CatId that shows ctegory id. Thank you in advance.Please help:
<html>
<head>
<title>Post AD</title>
</head>
<body>
<form method="POST" action="postad.php">
<table>
<tr>
<td>AdName:</td>
<td><input type="text" name="adname"></td>
</tr>
<tr>
<td>AdCategory</td>
<td>
//dropdown list
<select name="Categories">
<?php include = connect.php
$sql = "SELECT CatName FROM categories order by CatName";
$result = $db->query($sql);
while($row = $result->fetch_array()) {
echo "<option value='". $row['CatName']."'>."</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td>Contact Number</td>
<td><input type="text" name="contactnumber"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="text" name="image"></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td>Expiration Date</td>
<td><input type="text" name="expirationdate"></td>
</tr>
<tr>
<td id="btn"><input type="submit" value='submit' name="submit"></td>
</tr>
</table>
</form>
</body>
</html>

Looks like you have an error with your menu code. Should be this
echo "<option value=". $row['CatName'] ."></option>';
Opposed to
echo "<option value='". $row['CatName']."'>."</option>';
The result from the table is going to be a string regardless so there is no need to worry about value=''. The real problem was how you muffled up your quotes.
EDIT: Just noticed include = connect.php should be include 'connect.php';
<?php
$mysql_host = '';
$mysql_user = '';
$mysql_pass = '';
$mysql_dbase = '';
$mysql_table = '';
$pdo = new PDO('mysql:host=' . $mysql_host . ';dbname=' . $mysql_dbase, $mysql_user, $mysql_pass);
$sth = $pdo->prepare('SELECT * FROM `' . $mysql_table . '` ORDER BY `id` CatName');
$sth->execute();
$result = $sth->fetchAll();
foreach ($result as $row) {?>
<option value=<?php echo $row['CatName']; ?>></option>;
<?}?>

echo "<option value='". $row['CatName']."'>."</option>';
contains error and also there is no inner html for options
You can use this way for simplicity.
$CatName=$row["CatName"];
echo '<option value="'.$CatName.'">'.$CatName.'</option>';

Ok. Now I have created a new php file called getcategories.php. and this page works well. Here is the code for it:
$sql = "SELECT CatName FROM categories order by CatName";
$result = $db->query($sql);
$options="";
//echo "<select value='categories'>";
while($row = $result->fetch_assoc()) {
$categoryname=$row["CatName"];
// echo '<option value="'.$categoryname.'">'.$categoryname.'</option>';
// echo "</select>";
//echo "$categoryname\n";
$options .= '<option value="'.$categoryname.'">'.$categoryname.'</option>';
}
?>
How should I pull the data of options to the dropdown of html file

Related

Insert multiple row from php page into mysql

This problem's driving me crazy since yesterday. I have a table consist of 5 columns: kode_barang (item ID), nama_barang (name of item), qty (quantity), harga_beli (price), jumlah (total). User can input 2 items. This is the code of the form:
<HTML>
<?php include "koneksi.php"; ?>
<form action="insert3.php" method="POST">
<table id="theTable" border="1">
<thead>
<tr>
<th> Kode Barang </th>
<th> Nama Barang </th>
<th> Qty </th>
<th> Harga Beli </th>
<th> Jumlah </th>
<tr>
</thead>
<tbody>
<tr>
<td type="text" name="kode_barang" id="kode_barang1"/readonly>
<?php
mysql_connect("localhost","root","");
mysql_select_db("skripsi_1");
$result = mysql_query("select * from input_data_barang");
$met = "var kode_barang = new Array();\n";
echo '<select name="kode_barang" onchange="changeValue1(this.value)">';
echo '<option></option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';
$met .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};\n";
}
echo '</select>';
?>
</td>
<td><input type="text" name="nama_barang" id="nama_barang1"/readonly>
<script type="text/javascript">
<?php echo $met; ?>
function changeValue1(id){
document.getElementById('kode_barang1').value = kode_barang[id].name;
document.getElementById('nama_barang1').value = kode_barang[id].desc;
};
</script>
</td>
<td><input class="valOne" type="text" name="qty"></td>
<td><input class="valTwo" type="text" name="harga_beli"></td>
<td><input class="result" type="text" name="jumlah"></td>
</tr>
<tr>
<td type="text" name="kode_barang" id="kode_barang2"/readonly>
<?php
mysql_connect("localhost","root","");
mysql_select_db("skripsi_1");
$result = mysql_query("select * from input_data_barang");
$jsArray = "var kode_barang = new Array();\n";
echo '<select name="kode_barang" onchange="changeValue2(this.value)">';
echo '<option></option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';
$jsArray .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};\n";
}
echo '</select>';
?>
</td>
<td><input type="text" name="nama_barang" id="nama_barang2"/readonly>
<script type="text/javascript">
<?php echo $jsArray; ?>
function changeValue2(id){
document.getElementById('kode_barang2').value = kode_barang[id].name;
document.getElementById('nama_barang2').value = kode_barang[id].desc;
};
</script>
</td>
<td><input class="valOne" type="text" name="qty"></td>
<td><input class="valTwo" type="text" name="harga_beli"></td>
<td><input class="result" type="text" name="jumlah"></td>
</tr>
<script>
document.getElementById("theTable").addEventListener("input", function(e) {
var row = e.target.parentNode.parentNode
var val1 = row.querySelector(".valOne").value
var val2 = row.querySelector(".valTwo").value
row.querySelector(".result").value = val1 * val2
})
</script>
</tbody>
<td><input type="submit" value="OK"></a>
<input type="reset" value="Reset"></td>
</table>
</form>
</HTML>
And this is the connection to my database:
<?php
include "koneksi.php";
$kode_barang=$_POST['kode_barang'];
$nama_barang=$_POST['nama_barang'];
$qty=$_POST['qty'];
$harga_beli=$_POST['harga_beli'];
$jumlah=$_POST['jumlah'];
$query ="INSERT INTO faktur (kode_barang, nama_barang, qty, harga_beli, jumlah) VALUES ('".$kode_barang."', '".$nama_barang."', '".$qty."', '".$harga_beli."', '".$jumlah."'), ('".$kode_barang."', '".$nama_barang."', '".$qty."', '".$harga_beli."', '".$jumlah."')";
$sql =mysqli_query($connect, $query);
if ($sql){
header ("location: faktur.php");
}else{
echo "Error.";
echo "<br><a href='input_faktur.php'>Back</a>";
}
?>
Notice that my 'Kode Barang' is a dropdown option, and everytime a user click an item ID, name of the item will be shown automatically in 'Nama Barang' column. Everything in this page works perfectly.
But, when I saved it to database, it didn't save both of the items (item in the first row and the second row). Database only saved the second item but saved it twice. When I add a row to the table become 3 rows, database only save the third item and save it three times. When I add [] to the name, like name=kode_barang[], database didn't save the item ID, but only show "Array" text.
Would anybody please help me with this? Thanks.
You are using the same name for both your fields
use a array
name="nama_barang[]" and name="kode_barang[]"
do a loop in the php to save each field individually

Failing to update the new data entered by administrator

Look like everything is working fine with this code but in fact fails to update the database, Data are displayed correctly while fetching data but when i press update Button the data disappear but no update has been executed. It look fine to me but seems i am wrong.
This is a project for my professor so i don't care for the SQL injection and others.
<html>
<head>
<link rel="stylesheet" type="text/css" href="btnstyle.css">
<title>Managament System</title>
</head>
<body>
<h1>TU Chemnitz Student managament system</h1>
<br>
ADD Person
Edit Person
Manage Boards
Manage Departments
Search N&S
Triple Search
Membership
<br>
<br>
<?php
// set database server access variables:
$host = "localhost";
$user = "";
$pass = "";
$db = "";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$querys = "SELECT * FROM tblperson";
// execute query
$result = mysql_query($querys) or die ("Error in query: $query. ".mysql_error());
echo "<table border=1 align=center>
<tr>
<th>Personal ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Deparment</th>
<th>Board</th>
<th>Marticulation Number</th>
<th>Reg Date</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
?>
<?php
echo '<tr>';
echo '<td>'. $row['personid'].'</td>';
echo '<td>'. $row['personname'].'</td>';
echo '<td>'. $row['personsurname'].'</td>';
echo '<td>'. $row['persondepartment'].'</td>';
echo '<td>'. $row['personboard'].'</td>';
echo '<td>'. $row['martinumber'].'</td>';
echo '<td>'. $row['personregdate'].'</td>';
echo '<td>'.' EDIT '.'</td>';
}
?>
</body>
</html>
and this is the edit file which seems to problematic.
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid'];
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
<form action="edit20.php" method="POST">
<table border="0">
<tr>
<td>First Name</td>
<td><input type="text" name="newpersonname" value="<?php echo $row[1];?>" maxlength="30" size="13"></td>
</tr>
<tr>
<td>Last Name</td>
<td> <input type="text" name="personsurname" value="<?php echo $row[2];?>" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Department</td>
<td>
<select name='persondepartment'>
<option>Production</option>
<option>Sales</option>
</select>
</td>
</tr>
<tr>
<td>Board</td>
<td>
<select name='personboard'>
<option>Evaluation</option>
<option>Executive</option>
<option>Research</option>
</select>
</td>
</tr>
<tr>
<td>Marticulation Number</td>
<td> <input type="text" name="martinumber" maxlength="60" size="30"></td>
</tr>
<tr>
<td>Date of Registration</td>
<td><input type="date" name="personregdate" maxlength="7" size="7"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value=" Update"></td>
</tr>
</table>
</form>
You are looking for personid when the Update button is pressed on the form in edit20.php but that value has never been set so it will be empty and the update will fail.
After
<form action="edit20.php" method="POST">
add:
<input type="hidden" name="personid" value="<?php echo $personid; ?>">
On edit page seem your confusing the same variable with different values. If you state $personid variable to contain the edit value from get, then just re-use the variable don't assign new value. On this line you assign new value :
$personid = $_POST['personid'];
Don't assign new value since it has the initial value already to use just set the variable global for usage
$personid = $_GET['edit'];
Or else create a hidden element and pass edit value into it.
Please add name attribute for your update button
<td colspan="2"><input type="submit" name="update" value=" Update"></td>
and chk whether the update button set or reset as in the place of
if(isset($_POST['newpersonname'])) // change text 'newpersonname' as 'update'
You use a variable that doesn't excist:
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid']; // this doesn't excist
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
$personid = $_POST['personid']; doesn't excist in your code. Its simply a piece of code you put in there to probably proces, but forgot to define the variable in the code. Place the following in your form.
<input type="hidden" name="personid" value="<?php echo $_GET['edit']; ?>">
You only use this just once because you send the form back after proces to your home, hence it wont be used anymore. You can also use the avariable you defined as $personid; on that position.
If that fails, something maybe wrong in your query. Try to echo out the query (remove qucikly the meta command) by simply just do echo $sql after you do the sql query. 9 out of 10 times, it's a typo.

I would like to avoid repeating codes

Hello I'm trying to build a web app for a friend of mine to help her in her business. The application is meant to help her monitor the number of Stock Keeping Unit in the storage of her little bakeshop.(or monitor how much sacks of flour, tray of eggs, cups of butter are left in her inventory). So to do this I've made a form for her. look:
<tr> <form method="POST" action="makeprod.php">
<td>Prodname</td>
<td> <input type="text" name="prodname"></td>
</tr>
<tr>
<td>Quantity</td>
<td> <input type="int" name="quantity"></td>
</tr>
<tr>
<td>Recipe 1: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."'>".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec1qty"></td>
</tr>
<tr>
<td>Recipe 2: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe2'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec2qty"></td>
</tr>
<tr>
<td>Recipe 3: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe3'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."'>".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec3qty"></td>
</tr>
<tr>
<td>Recipe 4: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe4'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec4qty"></td>
</tr>
<tr>
<td>Recipe 5: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe5'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>"; ['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec5qty"></td>
</tr>
<tr>
<td><input id="button" type="submit" name="submit" value="submit"></td>
</tr>
</form>
</table>
What I'm trying to do in this form is to prompt her to record a product/pastry she's making, quantity and indicate the recipes/inventories that will cost her to make that product in that quantity so it will reduce those recipes from a database table I named 'inventory'(but this is for another story).
My issue is that I could not determine how many ingredients she'll need to make a single product and repeating a select option for the recipe 5 or 10 times seems to make my code look too dirty and it doesn't seem like efficient. What I want is a code that will help me display only the number of select options she needs in the form. So if a product she's making only has about 6 different ingredients then only 6 select option should be displayed. If 3, then only 3.
It would help if I don't have to repeat this code from above to make my code atleast a bit cleaner:
<td>Recipe 1: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>";
}
echo "</select>";
?>
I'm fairly new to PHP and doesn't know Javascript yet so I would prefer solution in HTML/PHP format but JS would also suffice. Thank you.
As per your title of question you have repeating code of sql connection in your code. Do avoid it by adding single connection file. Do create connection.php and add below code in it.
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
?>
and include this file in your this working page as
include "connection.php";

How to populate a textbox with the appropriate information related to the item selected from a drop down list in PHP?

I'm creating a form where it allows a user to enter in the number of product rows they would like to have in the form. Then with the corresponding rows, you can select the Product with the Product_ID & Product_Name from a Drop Down List. With the selected item, it should pull the Product_Cost from the table and populate the Unit Price textbox. I can't seem to get the textbox to populate with the correct data. My if statement if (isset($_POST['product' . $i])){ doesn't seem to be working properly, it runs as if the statement were false. I'm trying to say "If a select box has an option selected, take the option selected and find it's corresponding row in the database and take the price found in that row for that product and populate the unit price textbox."
<? require_once("connect_to_DB.php"); //inserts contents of this file here ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Order Form</title>
<meta charset="utf-8">
</head>
<body>
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
//This is for the options in the product list box
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="' . $row[0] . '">' . $row[0] . ' - ' . $row[2] . '</option>';
}
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="example.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>'">
<?
echo $options;
?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>'" ></td>
<? if (isset($_POST['product' . $i])){ ?>
<td class="inputCol2"><input type="text" name="'unit<?= $i ?>'" value="<?= $row[3] ?>" placeholder="$" ></td>
<? } else { ?>
<td class="inputCol2"><input type="text" name="'unit<?= $i ?>'" value="" placeholder="$"></td>
<? } ?>
<td class="inputCol2"><input type="text" name="'total<?= $i ?>'" placeholder="$"></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2"><input type="text" name="totalfinal" placeholder="$"></td>
</tr>
</table>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
</table>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_free_result($result2);
mysqli_free_result($result3);
mysqli_free_result($result4);
mysqli_close($db);
?>
</div>
</body>
There appear to be some syntax errors in there, for instance:
<select name="'product<?= $i ?>'">
<?
echo $options;
?>
</select>
There seems to be several instances where this type of quotation marks are used - I think they ought to be more like:
<select name="product<?= $i ?>">
<?
echo $options;
?>
</select>
Also, in a few places you are not using the correct opening tag for the php blocks though someone might point out different. Generally they ought to be:
<?php
/* statements */
?>
or
<?="print out this string";?>

Select option from dropdown menu with PHP and mySql [duplicate]

This question already has answers here:
Create PHP array from MySQL column
(12 answers)
Closed 22 days ago.
I tried to create a simple select dropdown menu from MySQL database. However, it does not work on my code.
Here is my code:
<?php
mysql_select_db($database_conn, $conn);
$query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
$RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
$totalRows_RsCourse = mysql_num_rows($RsCourse);
$count=0;
while ( $row = mysql_fetch_array($RsCourse, MYSQL_ASSOC)) {
$courseid=$row["courseid"];
$count++;
}
?>
<tr>
<td bgcolor="#CCCCCC"> Course Name</td>
<td bgcolor="#dfdfdf"> <select name="courseid">
<option value="" SELECTED>Selected Course</option>
<option value="<?php echo $courseid; ?>"><?php echo $row_RsCourse['$courseid']; ?></option>
</select>
</td>
</tr>
Any advice will be appreciated!
<?php
echo '<tr>
<td bgcolor="#CCCCCC"> Course Name</td>
<td bgcolor="#dfdfdf"> ';
mysql_select_db($database_conn, $conn);
$query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
$RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
$totalRows_RsCourse = mysql_num_rows($RsCourse);
if($totalRows_RsCourse)
{
echo '<select name="courseid"><option value="" SELECTED>Selected Course</option>';
$count=0;
while ($row = mysql_fetch_array($RsCourse, MYSQL_ASSOC))
{
$count++;
echo '<option value="'.$row['courseid'].'">'.$row['courseid'].'</option>';
}
echo '</select>';
}
else
{
echo 'No courses to show yet.'; // no rows in tbl_course
}
echo '</td>
</tr>';
?>
That was a mess, but hope you can go on from these new codes. Enjoy.
PS: this part >'.$row['courseid'].'</option> u can change to new one according to your table structure which one is not shown here.
<?php
echo '<tr>
<td bgcolor="#CCCCCC"> Course Name</td>
<td bgcolor="#dfdfdf"> ';
mysql_select_db($database_conn, $conn);
$query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
$RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
$totalRows_RsCourse = mysql_num_rows($RsCourse);
echo '<select name="courseid"><option value="" SELECTED>Selected Course</option>';
$count=0;
while ($row = mysql_fetch_array($RsCourse, MYSQL_ASSOC))
{
$count++;
echo '<option value="'.$row['courseid'].'">'.$row['courseid'].'</option>';
}
echo '</select></td>
</tr>';
?>
You can store everything in a buffer and print at once in the select below:
<?php
mysql_select_db($database_conn, $conn);
$query_RsCourse = "SELECT * FROM tbl_course ORDER BY courseid DESC";
$RsCourse = mysql_query($query_RsCourse, $conn) or die(mysql_error());
$coursesHtml = "";
while ( $row = mysql_fetch_array($RsCourse, MYSQL_ASSOC)) {
$coursesHtml .= "<option value='{$row["courseid"]}'>{$row["coursename"]}</option>";
}
?>
<tr>
<td bgcolor="#CCCCCC">Course Name</td>
<td bgcolor="#dfdfdf">
<select name="courseid">
<option value="" SELECTED>Selected Course</option>
<?= $coursesHtml ?>
</select>
</td>
</tr>
PS: Avoid use , style your html well with css using padding-left: 5px; or other features;
PS2: You should not show your page/form with tables structure, use divs with flexbox.

Categories