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";
Related
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.
thanks for taking a look at this. I have been stuck at this problem for awhile basically i am doing a recruitment agency website for my school project. I am doing the function where i can view all the candidate who applied for any job and i can choose from the dropdown box whether to "approval", "denied", or remain as "pending" which it should update the table in the database to the option i chose and it will reflect at the candidate's page. However with the codes i am using right now, it is able to display all the information i need from the different table on the page but when i try to submit the details, it only works for the last guy that applied and not the rest.
This is the form :
<form method="post" action="doEditStatus.php">
<div align ="center">
<table border='1' width ="500">
<tr>
<td> <b> ID </b></td>
<td> <b> Candidate name </b></td>
<td> <b> Job ID </b></td>
<td> <b> Job title </b></td>
<td> <b> Company </b></td>
<td> <b> Shortlist status </b></td>
</tr>
while ($row = mysqli_fetch_array($result)) {
$jobid = $row['Job_id'];
$canid = $row['Candidate_id'];
?>
<tr>
<td><?php echo $canid; ?></td>
<input type="hidden" name="can_id" value=<?php echo $canid ?>>
<input type="hidden" name="job_id" value=<?php echo $jobid ?>>
<?php
$query2 = "SELECT * FROM candidate WHERE Candidate_id =$canid";
$result2 = mysqli_query($link, $query2) or die(mysqli_error($link));
while ($row2 = mysqli_fetch_array($result2)) {
$canname = $row2['First_name']." ".$row2['Last_name'];
?>
<td><?php echo $canname; ?></td>
<?php
}
$query3 = "SELECT * FROM jobs WHERE Job_id =$jobid";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
while ($row3 = mysqli_fetch_array($result3)) {
$jobname = $row3['Job_title'];
$comid = $row3['Company_id'];
?>
<td><?php echo $jobid; ?></td>
<td><?php echo $jobname; ?></td>
<?php
}
$query4 = "SELECT * FROM company WHERE Company_id =$comid";
$result4 = mysqli_query($link, $query4) or die(mysqli_error($link));
while ($row4 = mysqli_fetch_array($result4)) {
$comname = $row4['Company_name'];
?>
<td><?php echo $comname; ?></td>
<?php
}
?>
<td>
<select id="id_status" name="shortlist_status">
<option value="0">Pending...</option>
<option value="1">Shortlist</option>
<option value="2">Denied</option>
</select>
</td>
</tr>
<?php
}
?>
</table>
</div>
<input type="submit" value="Submit"/>
</form>
This is the dosubmit page:
<?php
include "dbFunctions.php";
session_start();
$candidate_id = $_POST['can_id'];
$job_id = $_POST['job_id'];
$status = $_POST['shortlist_status'];
$insertQuery = "UPDATE application SET Shortlist_status = '$status' WHERE Candidate_id = $candidate_id AND Job_id = $job_id";
$inserted = mysqli_query($link, $insertQuery) or die(mysqli_error($link));
if($inserted)
{
$message = 'Profile edited successfully <br>Home';;
echo $candidate_id;
echo $status;
}
else
{
$message = "Profile edited failed";
}
echo $message;
?>
Give all your inputs names that end with []. PHP will then create an array for each of them in $_POST. E.g.
<input type="hidden" name="can_id[]" value=<?php echo $canid ?>>
<input type="hidden" name="job_id[]" value=<?php echo $jobid ?>>
Then your PHP can do:
$insertQuery = "UPDATE application SET Shortlist_status = ? WHERE Candidate_id = ? AND Job_id = ?";
$insertStmt = mysqli_prepare($link, $insertQuery);
mysqli_stmt_bind_param($insertStmt, "iii", $status, $candidate_id, $job_id);
foreach ($_POST['can_id'] as $i => $candidate_id) {
$job_id = $_POST['job_id'][$i];
$status = $_POST['shortlist_status'][$i];
$inserted = mysqli_execute($insertStmt) or die(mysqli_error($insertStmt));
if ($inserted) {
...
} else {
...
}
}
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
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.
My table has 4 columns. "isbn, author, title, price".
I want to search one of them combining all 4 fields.
Like: if author "kayle" write 4 books with same price ($50) but with different title, for that in search page, if i select author:kayle and hit search, then it shows all books with all different or may be same prices and titles. Now i want to select author:kayle and price:$50 at the same time, and hit enter. For that it will show only $50 prices books wrote by kayle and it will appear in the table with 4 rows.
I try to combine it but stuck with the second step searching query. Here is my code if any one understand what i want to do, please share it. Here is my code:
<form method="post" action="search_form.php">
<input type="hidden" name="submitted" value="true"/>
<table border="1">
<tr>
<td style="padding:3px 10px; font-weight:bold;">ISBN</td>
<td style="padding:3px;">
<input type="hidden" name="category_isbn" id="category_isbn" value="isbn"/>
: <select name='criteria_isbn' id="criteria_isbn" onchange="ajaxFunction()">
<option selected="selected">- Select -</option>
<?php
$order = "SELECT isbn FROM books" or die (mysql_error());
$result = mysql_query($order);
while($data = mysql_fetch_array($result))
{
echo ("<option> $data[isbn] </option>");
}
?>
</select>
</td>
<td style="padding:3px 10px; font-weight:bold;">Author</td>
<td style="padding:3px;">
<input type="hidden" name="category_author" id="category_author" value="author"/>
: <select name='criteria_author' id="criteria_author" onchange="ajaxFunction()">
<option selected="selected">- Select -</option>
<?php
$order = "SELECT author FROM books" or die (mysql_error());
$result = mysql_query($order);
while($data = mysql_fetch_array($result))
{
echo ("<option> $data[author] </option>");
}
?>
</select>
</td>
<td style="padding:3px 10px; font-weight:bold;">Title</td>
<td style="padding:3px;">
<input type="hidden" name="category_title" id="category_title" value="title"/>
: <select name='criteria_title' id="criteria_title" onchange="ajaxFunction()">
<option selected="selected">- Select -</option>
<?php
$order = "SELECT title FROM books" or die (mysql_error());
$result = mysql_query($order);
while($data = mysql_fetch_array($result))
{
echo ("<option> $data[title] </option>");
}
?>
</select>
</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submitted']))
{
$category_isbn = $_POST['category_isbn'];
$criteria_isbn = $_POST['criteria_isbn'];
$query = "SELECT * FROM books WHERE $category_isbn LIKE '%".$criteria_isbn."%'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if(isset($_POST['criteria_isbn']))
{
$category_author = $_POST['category_author'];
$criteria_author = $_POST['criteria_author'];
$query = "SELECT * FROM books WHERE $category_author LIKE '%".$criteria_author."%'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if(isset($_POST['criteria_author']))
{
$category_title = $_POST['category_title'];
$criteria_title = $_POST['criteria_title'];
$query = "SELECT * FROM books WHERE $category_title LIKE '%".$criteria_title."%'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows results found";
echo "<table border= 1>";
echo "<tr> <th>ISBN</th> <th>AUTHOR</th> <th>TITLE</th> <th>PRICE</th> </tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['isbn'];
echo "</td><td>";
echo $row['author'];
echo "</td><td>";
echo $row['title'];
echo "</td><td>";
echo $row['price'];
echo "</td></tr>";
}
echo "</table>";
}
}
}
?>
Thanks in advance.
try this
for first search
select* from table where author="kayle";
for second search
select * from table where author="kayle" and price="50";