Save a dropdown list into database - php

I have a list which is displayed from my database:
<<b>List</b>
<?php
$select = "SELECT * FROM quabits.inregistrare";
$result2 = mysql_query($select);
?>
<form id="raport" name="save">
<table cellspacing='0' cellpadding='0'>
<tr><td>Selectează: </td>
<td><select name="den" id="den" onchange="rulare(this.value)">
<option value="">Selectați un profesor</option>
<?php
while($row=mysql_fetch_array($result2)){
echo "<option value='" . $row['Email'] ."'> " .$row['Nume'] ." ". $row['Prenume'] ."</option>";
}?>
</select>
</td></tr>
</table>
</form>
<input id="save" name="save" type="submit" value="Submit" action="POST"/>
After the drop down menu I put a button to submit.
And after a value is selected and click for submit, I want to save into my database (quabits table inregistrare) the value.
Here I'm lost. Any help ?

If you have a select like the following:
<select name="den" id="den" onchange="rulare(this.value)">
<option value="">Selectați un profesor</option>
.....
</select>
You would retrieve the value of the select using $_REQUEST['den'].
Make sure you add the method type to your form, for example
<form id="raport" name="save" method="POST">
in PHP you would store the POST value:
$den = $_POST['den'];

$den = $_POST['den'];
$link = mysqli_connect("host","user","password","database",3306
$sql = "Insert into table ('fieldname') VALUES ('$den');
mysqli_query($link,$sql);
Very basic solution, but i the question is not very understandable....

Related

Fetch select options from database and pass the selected option to PHP variable

Code excerpt for populating the options in HTML select from the database query:
<?php
// Connect to the database
$mydb = new wpdb('***','***','***','***');
// run the query to fetch the options
$rows = $mydb->get_results("select distinct(Region) from tblusers order by
Region asc;");
?>
<!-- Form -->
<form method='post' action='success.php'>
<b><h1>Stuur SMS aan Streek</h1></b>
<b>Kies Streek:</b>
<select id='txtReg' name='txtReg'></option>
<?php
// populate the options
foreach ($rows as $row) {
echo "<option value=$row->Id>$row->Region ? 'selected="selected"' : ''
</option>";
}
?>
</select>
<br/>
<b>Boodskap:</b>
<textarea id='txtMsg' name='txtMsg' rows=5 cols=65 style='width:50%;'>
</textarea>
<br/>
<input type='reset' value='Kanselleer'> | <input class="button"
type='submit' value='Stuur SMS aan Streek'>
</form>
The success.php page that has to display the option that was selected:
<?php
$txtReg = $_POST['txtReg'];
echo $txtReg;
All working, except when I select a option from the dropdown, how do I pass it to the success page as it is not displaying when I try to echo the variable.
You have some syntax problems associated with your select tag and how you are generated your option tags.
Updated
<?php
// Connect to the database
$mydb = new wpdb('***','***','***','***');
$query = "SELECT distinct(Region)
FROM tblusers
Order By
Region ASC";
$rows = $mydb->get_results($query);
var_dump($rows);
?>
<!-- Form -->
<form method='post' action='success.php'>
<b><h1>Stuur SMS aan Streek</h1></b>
<b>Kies Streek:</b>
<select id='txtReg' name='txtReg'>
<?php
// populate the options
foreach ($rows as $row) {
echo '<option value="' . $row->Region . '">' . $row->Region . '</option>';
}
?>
</select><br/>
<b>Boodskap:</b>
<textarea id='txtMsg' name='txtMsg' rows=5 cols=65 style='width:50%;'></textarea><br/>
<input type='reset' value='Kanselleer'> | <input class="button" type='submit' value='Stuur SMS aan Streek'>
</form>
Another problem you might be having is your query. Looks like you may not have the ID field being returned in your data. If that field is not in your query then the value in your options tag will be empty.
A solution might be to change the $row->Id to $row->Region in the value of the option tag.
That should get you going in the right direction.
You can add hidden field for store selected option. Update that hidden field value when changing option by using jquery or javascript.
Try changing this,
echo "<option value=$row->Id>$row->Region ? 'selected="selected"' : ''
to
$value = 'whatever your value';
echo "<option value='". $value ."' />
What I can see is quotes are missing from your value.
You have a mistake in your code. When you want an option to be selected you just write an attribute "selected" like this: <option value="myValue" selected></option>
in this case what you are doing is, you are assigning "selected" to option's value;
Your code should look like something like this:
<?php
// populate the options
foreach ($rows as $row) {
echo "<option value='".$row->Id."' ";
if($row->id == $row->Region) echo "selected";
echo "> SomeOption"; // Close the option tag and add any text inside
echo "</option>";
}
?>
You have 2 errors in your code, you not passing the value in option and the logic for selection is == not >
<?php
// Connect to the database
$mydb = new wpdb('***','***','***','***');
// run the query to fetch the options
$rows = $mydb->get_results("select distinct(Region) from tblusers order by Region `asc;"); `
?>
<!-- Form -->
<form method='post' action='success.php'>
<b><h1>Stuur SMS aan Streek</h1></b>
<b>Kies Streek:</b>
<select id='txtReg' name='txtReg'></option>
<?php
// populate the options
foreach ($rows as $row) {
echo "<option value='".$row->Id."' ".($row->Id==$row->Region) ? 'selected' : ''."></option>";
}
?>
</select>
<br/>
<b>Boodskap:</b>
<textarea id='txtMsg' name='txtMsg' rows=5 cols=65 style='width:50%;'>
</textarea>
<br/>
<input type='reset' value='Kanselleer'> | <input class="button" type='submit' value='Stuur SMS aan Streek'>
</form>

Can't view records based on selected drop down values from two drop downs

I want to show a record from database based on the selected drop down value. I have a page which shows a drop down having column names and another drop down which shows the rows of that selected column from first drop down. After that there is a search button which shows all the records based on those two drop down menus i.e. the selected column name and selected row of that column. Then the search button shows a table having all the columns and shows that specific result.
Problem is I don't know how to use that drop down ids in the select query. Here is my code:
<form id="searchform" action="view.php" method="post" enctype="multipart/form-data" >
<select id="select1" onChange="check(this);">
<option selected value="" disabled="disabled">Select an Option</option>
<option value="all">Select All</option>
<option value="names" >Names</option>
<option value="courses" >Courses</option>
</select>
<select id="select2" disabled="disabled">
<option>Select an option</option>
<?php
$sql="SELECT DISTINCT names FROM table ";
$result = mysqli_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option class='names' value=' " . $row['names'] ."'>" . $row['names'] ."</option>";
}
?>
<?php
$sql="SELECT DISTINCT courses FROM table ";
$result = mysqli_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option class='courses' value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
?>
</select>
<input type="submit" name="submit" value="Search" />
</form>
Now on view.php page
<body>
<?php
$count=1;
include("connection.php");
$select1=isset($_POST['select1']);
$select2=isset($_POST['select2']);
$result=mysql_query("SELECT names,courses FROM table WHERE names='.$_REQUEST['select2'].'");
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Names</th> <th>Courses</th></tr>";
while ($row=mysql_fetch_array($result)){?>
<tr>
<td align="center" ><?php echo $row["names"]; ?></td>
<td align="center" ><?php echo $row["courses"]; ?></td>
</tr>
<?php $count++; } ?>
</table>
</body>
</html>

using multiple list menu and checkbox php

Am retrieving some data from the DB and am allowing users to make multiple selection via check box and also selecting a level for each selected check box. when saving, i only get to see the selected check boxes in the DB but not the level selected.
Code for making selection
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency WHERE department = '$department'");
while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>";
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[]'value= ".$row['id']." >
<option></option>
<option>level 1</option>
<option>level 2</option>
<option>level 3</option>
<option>level 4</option>
<option>level 5</option> </select> </td> ";
}
echo "</tr>";
?>
<input name="submit" type="submit" value="submit" />
</form>
<?php
echo" </table>";
?>
..
Code for saving into the DB
session_start();
$id = $_SESSION['user_id'];
$hobb = $_POST['comp'];
$level = $_POST['level'];
include ('mysql_connect.php');
$N = count($hobb);
for($i=0; $i < $N; $i++)
{
$var1=$hobb[$i];
$var2 = $level[$i];
//include ('connect.php');
include ('mysql_connect.php');
$table = "INSERT INTO competency_result (user_id,competency_id,level) ".
"VALUES ('$id', '$var1', '$var2')";
mysql_query($table) or die(mysql_error());
$inserted_fid = mysql_insert_id();
mysql_close();
}
Set the key the same for comp[] and level[], ie. $row['id'] -
while($row = mysql_fetch_array($sql))
{
...[your code]...
echo "<input type='checkbox' name='comp[".$row['id']."]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo "<td> <select name='level[".$row['id']."]' >
...[your code]...
}
(note: the <select> does not have a value attribute. That is defined by the <option>)
Then you can easily get the corresponding values using
foreach($hobb as $key=>$val)
{
$var1=$hobb[$key];
$var2=$level[$key];
...[your code]...
}
(also, it looks like you have <tr> inside your loop, but </tr> after. I would assume you would want to move the closing tag inside the loop to properly close each row)

HTML form submit button name tag empty

I have two forms created in PHP as below
echo '<form name="delete" action="?page=deletetable" method="POST" autocomplete="off">';
echo '<input type=submit value="Delete" name="'.$row['TableName'].'">';
echo "</form>";
echo '<form name="assign" action="?page=assign" method="POST" autocomplete="off">';
echo '<select name="user">';
$sql = mysqli_query($con,"SELECT * FROM users WHERE hasCustom = 0");
while ($row = mysqli_fetch_array($sql)){
echo "<option value=\"".$row['user_name']. "\">" . $row['user_name'] . "</option>";
}
echo "</select>";
echo '<input type="submit" value="Assign" name="'.$row['TableName'].'">';
echo "</form>";
This looks like the below in the end html code
<form name="delete" action="?page=deletetable" method="POST" autocomplete="off">
<input type=submit value="Delete" name="rates_test">
</form>
<form name="assign" action="?page=assign" method="POST" autocomplete="off">
<select name="user">
<option value="zac">zac</option>
<option value="testadmin">testadmin</option>
<option value="tonyd">tonyd</option>
</select>
<input type="submit" value="Assign" name="">
</form>
as you can see in the last submit button the name tag is empty yet in the first form the name tag is correct i.e name="rates_test"
Both forms are using the same PHP to get this value so I cant see why one works and the other does not.
You're using a 'while' loop to reset $row. My guess is that ['TableName'] doesn't exist in $row after that loop.
Try using separate variable names for your first $row and the one in the while loop.
With the line:
while ($row = mysqli_fetch_array($sql)){
you overwrite the variable $row, try using a different variable for that while loop unless this is intended.
$row variable is getting the new values again when you have fetched the records again from the query.
Try ($row1 = mysqli_fetch_array($sql)) instead of $row = mysqli_fetch_array($sql))

PHP- Fetch from database and store in drop down menu html

I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";

Categories