Using POST method to reflect the records - php

I am trying to reflect the records from MySQL database using PHP. The code goes like this
(Database is connected and selected)
Query.php -> This file reflect the distinct category(since I have multiple values of category) in select box from the database
<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from tbl_1 order by Category asc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
echo "<option value=$row[Name]>$row[Category]</option>";
}
?>
</select>
<input name="" type="submit" />
</form>
process.php-> This file take the option selected by user in query.php and reflect Name and district accordingly.
<?php
$myValue =$_POST['cat'];
echo $myValue;
$mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()");
if(mysql_num_rows($result2) == 0)
{
echo("<br/>No Records Found");
}
ELSE
{
echo "<table border='1'>";
//ECHO THE RECORDS FETCHED
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['Station'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "</tr>";
}
echo "</table>"; }
?>
PROBLEM-> On running the query.php I think the process.php does not recieve the selected option from query.php and hence I get "No Records Found". My database have the data.
Can anyone tell me the mistake here...

You are placing <option value=$row[Name]>$row[Category]</option>
here value is $row[Name] not $row[Category] and selecting category
$mySqlStm = "SELECT Station, Name FROM tbl_1 WHERE Category = '.$myValue.'";
try to place $row[Category] in option value like this
echo "<option value=".$row[Category].">".$row[Category]."</option>";

Related

How to edit drop-down selected value?

I have created a drop-down menu and many other text fields on my html page which fetches the option values from database. It is inserting the selected values in my database table. I want to edit the selected values from the drop-down menu and update them in database. When I open the edit page, the form shows the previous saved values to edit them. But the problem is; When I don't edit the drop-down values and submit the form, It shows me error of undefined index and when I edit or select different value of drop-down then it works fine and don't show any error. Here is my code:
HTML
<label>Courses</label><select name="courses" id="courses" class="dropdownclass"><option selected="selected" value="" disabled selected hidden>-- Select an option --</option><?php
mysql_connect('localhost', 'root', '');
mysql_select_db('db');
$sql = "SELECT courses FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
?>
</select>
<!-- begin snippet: js hide: false console: true babel: false -->
EDITRECORD
<?php
include("connection.php");
$Sno = (int)$_GET['Sno'];
$query = mysql_query("SELECT * FROM table WHERE Sno = '$Sno'") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
if (isset($_POST)) {
echo "";
$id=$row['id'];
$courses=$row['courses'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<form id="form" action="update.php" method="post" enctype="multipart/form-data">
<fieldset>
<input type="hidden" name="new" id="Sno" value="<?=$Sno;?>" />
<label>Courses</label><select name="courses" id="courses" class="dropdownclass" ><option selected="selected" value="" disabled selected hidden><?php echo $courses; ?></option><?php
mysql_connect('localhost', 'root', '');
mysql_select_db('db');
$sql = "SELECT courses FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
?>
</select>
</fieldset>
</form>
</body>
</html>
UPDATE
<?php
include("connection.php");
$Sno ='';
if( isset( $_POST['new'])) {
$Sno = (int)$_POST['new'];
}
$id = mysql_real_escape_string($_POST['id']);
if(isset($_POST['courses'])){
$courses = mysql_real_escape_string($_POST['courses']);
}else{
$courses=$_POST['courses'];
}
query="UPDATE technicalsol
SET id= '$id',
courses = '$courses'
WHERE Sno=$Sno";
$res= mysql_query($query);
if($res){
echo "<div style ='font-size:20px; margin-left:140px;'>Records updated Successfully</div>";
include("search.php");
}else{
echo "Problem updating record. MY SQL Error: " . mysql_error();
}
?>
I want that; If I don't edit the drop-down selected value, It just takes the previous value and saves it.
What you are doing right now is listing the options in dropdown list.
You are not setting up any answer
In your HTML:
while ($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
make sure you make the old value selected.
may be by using an IF statement

Create a table from and SQL database containing a drop down menu with a list of names from another SQL table

I need to create a table with a drop down menu and submit button in each row.
The drop down menu contains a list of advisers from an SQL table. When i select and adviser and i press the submit button the id of the item in the current row along with then selected adviser id or name must be sent to another page. In my case it is sent to delete.php.
My code bellow displays a drop down menu and a submit button for each row of the table, however when you press the submit button it will only work correctly if you press the submit button located at the bottom of the table, if i press any other it appears to not send the info from the drop down menu.
( i know my code appear messy, i am experimenting if something is unclear ask me and i will clarify. )
Thank you very much!
<!DOCTYPE html>
<html>
<body>
<?php
//this is he code for the qeue
// connect to the database udinh sqli
$con = get_sqli();
// get results from database
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
//select whole list of students from walk_in
mysqli_select_db($con,"login");
$sql="SELECT * FROM walk_in";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
mysqli_close($con);
//Table to dispaly qeueu of students
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th><th>Advisor Student wants to see</th><th>P ID</th><th>Select Advisor to notify on send</th><th>Send Student</th><th> </tr>";
echo "<tr>";
//create a table of students by displaying all the data from result and adding a button
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Advisor'] . "</td>";
echo "<td>" . $row['pid'] . "</td>";
// echo '<form action="delete.php?id2=' . $row['id'] . '" method="post">';
// drop down menu for selecting advisor as a form submission
// used to name each submit button with the id from walk_in
$formId = $row['id'] ;
echo "<td>" ;
//create a form to submit the sleected advisor and the seelcted student to be removed from the queue
echo '<form action="delete.php?id=' . $row['id'] . '" method="post">';
//another query used to retreive the list of advisors to pupulate the drop down menu
//create a drop down menu with advisors resulting from the queue
echo '<select name="formStatus">';
$con = get_sqli();
mysqli_select_db($con,"login");
$sql="SELECT * FROM login_details WHERE level = 0 AND logged = 1";
$result2 = mysqli_query($con,$sql);
if (!$result2) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//loops through all advisors for drop down menu creation
while ($row2 = mysqli_fetch_array($result2)) {
$id = $row2['id'];
echo '<option value="'.$id.'">'.$id.'</option>';
}
echo'<option selected="selected"></option>';
echo '</select>';
echo '<td><input type="submit" name="formSubmit" value= "'.$formId.'" /><td>';
//echo '<td><input type="submit" name="formSubmit" value= /><td>';
//echo '<td>Send</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Here are the tables i am using:
login_details table containing ADVISER details
I forgot to close the form, the issue has been fixed. Thank you all!

Mapping values in dropdown 2 dependent on value selected in dropdwon 1 via php mysql

I am fresher in php & mysql.
I have a from where there are 2 dropdwon's. The value in dropdwon are coming via mysql. But now i am not able to get the value in 2nd dropdown dependent on value selected in 1st dropdown.
Here is the code i have tried so far.
Please Help!
Thank You
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn=mysqli_connect('localhost','root','');
$db="sample";
mysqli_select_db($conn,$db);
$sql="SELECT DISTINCT(Site) FROM `bom`";
$result=mysqli_query($conn,$sql);
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while($row=mysqli_fetch_array($result))
{
echo "<option value='".$row['Site']."'>".$row['Site']."</option>";
}
echo "</select>";
$sql1="SELECT BOM Desc FROM `bom` where Site= ";
$result1=mysqli_query($conn,$sql1);
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while($row1=mysqli_fetch_array($result1))
{
echo "<option value='".$row1['BOM Desc']."'>".$row1['BOM Desc']."</option>";
}
echo "</select>";
?>
</body>
</html>
Hi Your fixed code here:
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$db = "sample";
mysqli_select_db($conn, $db);
$sql = "SELECT DISTINCT(Site) FROM `bom`";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); // add error show info
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Site'] . "'>" . $row['Site'] . "</option>";
}
echo "</select>";
$sql1 = "SELECT BOM Desc FROM `bom` where Site IS NULL "; // change the for null site
$result1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn)); // add error show info
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while ($row1 = mysqli_fetch_array($result1)) {
echo "<option value='" . $row1['BOM Desc'] . "'>" . $row1['BOM Desc'] . "</option>";
}
echo "</select>";
?>
</body>
</html>
And if you need load on second select php only cant handle this because you must give time to user for check first select.
I think the better way is using Ajax request for second:
Auto Load Second Dropdown using AJAX
I suggest to use in both <select> the clause selected to show the selected <option>.
Then, in the first <select> add the on change event to launch a page refresh so that:
The selected entry is recognised as selected
The SQL for the second drop-down can be filtered on the selected entry in first dropdown
The first select should appear then like this:
<select onChange='if(options[selectedIndex].value) { location=options[selectedIndex].value;}' size='1'>
<option value='?site=Plant1'>Plant ONE</option>
<option value='?site=Plant2' selected>Plant 2</option>
</select>
When Plant 2 is selected, the page will be refreshed with the URL containing the parameter &site=Plant2 which you can read in the variable $_REQUEST['site'] to be used in the 2nd SQL query

php select drop down choice to session variable

I have two dropdown boxes and I want the user to choose values from both.
The dropdown boxes are filled with results of a query on page load. I want these values to then be stored as a session variable for use in subsequent queries.
First file = choose.php
<html>
<form action="choose2.php" method="post">
<?//db connection stuff taken out from here
}
// fill classes
$result = mysqli_query($con,"SELECT * FROM classes");
echo "<select id='classlist'>";
while($row = mysqli_fetch_array($result))
{
echo " <option value=" . $row['id'] . ">" . $row['classname'] . "</option>";
}
echo "</select>";
//fill schemes of work
$result = mysqli_query($con,"SELECT * FROM schemes_of_work");
echo "<select id='sowlist'>";
while($row = mysqli_fetch_array($result))
{
echo " <option value=" . $row['id'] . ">" . $row['name'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit">
</form>
</html>
second file = choose2.php
<?php session_start();
$_SESSION['classname'] = $_POST["classlist"];
$_SESSION['sowname'] = $_POST["sowlist"];
?>
<?php echo $_SESSION['classname'];?>
<?php echo $_SESSION['sowname'];?>
I cant get this to work though - I am getting an empty page on choose2 and the following error in apache log:
"Undefined index: sowlist in /var/www/assessment/choose2.php on line 3,
The select tag should have a name attribute to be able to use it in php POST.
Try
<select id='classlist' name="classlist">
The same for the other one.

PHP populate the data from database when District is selected

please follow this post instead Check my following post please
Chained multiple selects
I have this code where a user selects the particular category of doctor and then the District in which he wants to look it. After state he needs to click the station(in the selected district)
Doctor >> District >> Station
Here is the code
<html>
<body>
<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from doctors order by Category asc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
echo "<option value='{$row[Category]}'>{$row[Category]}</option>";
}
?>
</select>
<select name="station">
<?php
$sql_1="select distinct Station from doctors order by Station asc";
$query_1=mysql_query($sql_1);
while($row=mysql_fetch_array($query_1))
{
echo "<option value=".$row[Station].">".$row[Station]."</option>";
}
?>
</select>
<input name="C" type="submit" />
</form>
</body>
</html>
Process.php
$mySqlStm ="SELECT Name FROM doctors WHERE Category='$myValue' and Station='$myStation'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()");
if(mysql_num_rows($result2) == 0){
echo("<br/>no records found");
}
ELSE
{
echo "<table border='1'>";
//ECHO THE RECORDS FETCHED
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
//echo "<td>" . $row['Station'] . "</td>";
echo "<td>" . $row['Name'] . "</td>" ;
//echo "<td>" . $row['Phone 1'] . "</td>";
//echo "<td>" . $row['Mobile'] . "</td>";
}
I want the station category to be auto filled when district is selected and When a category of doctor is selected, the district has to be auto filled.
How can I do it? I would prefer Javascript because I know a little bit about it.
Also, i have all the data in one database it looks like this
Category | Station | District | Name | Qualification and so on
Ajax + jQuery will do that for you in a few lines of code.
Tutorial here: Ajax+jQuery Tutorial
Ajax for the state and city
From this you can get the idea for the doctor list
Please check it once this is good example
Demo for Country, State and City is here
Code is Here
Customize it your way..
You can change the code in findCity.php to Process.php and in js/location.js
you have:
$.ajax({
type: "GET",
url: "findCity.php",
data: strURL,
cache: false,
success: function(d){
$('#citydiv').show().html(d);
}
});
#citydiv is the part which will be changed to the result you got in Process.php

Categories