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
Related
This is the code I've got so far:
<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
<?php
$sql="SELECT course_name FROM course";
$result = mysqli_query($conn, $sql) or die(mysql_error());
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
}
?>
</select>
I'm trying to make it so the dropdown appears blank at first instead of showing an option pulled from the database. (all connection etc is above)
Alex answered this question for you. Just add <option>Select...</option> before the loop (Change Select... to white space if you really want it to show a "blank" option).
Here is the code:
<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
<option>Select...</option>
<?php
$sql="SELECT course_name FROM course";
$result = mysqli_query($conn, $sql) or die(mysql_error());
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
}
?>
</select>
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
I created a drop down list that has been populated by the database and now I'm having trouble retrieving the data. Normally, I would know how to retrieve the value of the drop down list if I had to manually name the data, but in this case, I'm not quite sure how I would name it.
Here is my current code:
<h1>Generate Reports</h1>
<form enctype="multipart/form-data" action="http://localhost/yiiFolder/index.php/create" method="post">
<table>
<tr>
<td><strong>Materials</strong></td>
<?php
mysql_connect('host', 'root', 'password');
mysql_select_db ("db");
$sql = "SELECT material_name FROM materials";
$result = mysql_query($sql);
echo "<td><select name='materials'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['material_name'] . "'>" .
$row['material_name'] . "</option>";
}
echo "</select></td></tr> ";
$sql2 = "SELECT location_name From locations";
$result2 = mysql_query($sql2);
?>
<td><strong>Locations</strong></td>
<?php
echo "<td><select name='locations'>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<option value='" . $row2['location_name'] . "'>" .
$row2['location_name'] . "</option>";
}
echo "</select></td></tr>";
?>
<tr>
<td><button name="submit" type=submit>Generate</button></td>
</tr>
</table>
</form>
<?php
$material = $row['material_name'];
$locations = $row2['location_name'];
$generate = $_POST['submit'];
if(isset($generate))
{
echo $material;
echo $locations;
}
?>
You're trying to capture value before the submit button is hit. Also, as Hanky pointed out you're using the wrong names while referring to select data. You should do this instead
if(isset($_POST['submit'])) // this code will run after the button is clicked
{
$material = $_POST['materials']; // and not material_name
$locations = $_POST['locations']; // and not location_name
echo $material;
echo $locations;
}
PS: You're following a very unsecure way of developing a web application. At the very least you need to switch to PDO and always escape the data.
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.
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>";