Displays the current value taken from ENUM in form - php

I have a field in the mysql database of type "ENUM",
the field is called "career_status" and these values are stored,
Active,
Retired,
Dead
I created a form for update, the one I would like and display the current value in the form.
Example:
if now it is stored Active displaying Active in the form, if nothing is empty etc ..
How to get these values from the field?
this is the code
<?php
require_once("connetti.php");
$actor_id = $_GET["id"];
$query = mysql_query("SELECT * FROM actor WHERE actor_id=" . (int)$actor_id) or die ("Error in query: " . mysql_error());
$row = mysql_fetch_array ($query);
$nome=$row['nome'];
$performer_aka=$row['performer_aka'];
$website=$row['website'];
$career_status=$row['career_status'];
$birthday=$row['birthday'];
$died=$row['died'];
$status=$row['status'];
?>
<div id="BioData" style="overflow: hidden;">
<center><h1><?php echo $nome;?> <span class="infoblock-pagetype">modifica dati</span></h1></center>
<form action="<?php echo $_SERVER['PHP_SELF'] . '?id=' . $actor_id; ?>" method="post">
<tr>
<td class="paramname">
<b>Career Status: </b> (<b><font color="red">selezionare il valore</font></b>)
</td>
<td class="paramvalue">
<select name="career_status" id="career_status"><?php echo $career_status;?>
<option value="1">Active</option>
<option value="2">Retired</option>
<option value="3">Dead</option>
<option value="0"> </option>
</select>
</td>
</tr>
<?php
if (isset($_POST['modifica']))
{
//include "connetti.php";
//$query = mysql_select_db("xxx", $db);
if ($query)
{
$nome=$_POST['nome'];
$performer_aka=$_POST['performer_aka'];
$website=$_POST['website'];
$career_status=$_POST['career_status'];
$birthday=$_POST['birthday'];
$died=$_POST['died'];
$status=$_POST['status'];
$query=mysql_query("UPDATE actor SET
nome='".$_POST['nome']."',
performer_aka='".$_POST['performer_aka']."',
website='".$_POST['website']."',
career_status='".$_POST['career_status']."',
birthday='".$_POST['birthday']."',
died='".$_POST['died']."',
status='".$_POST['status']."'
WHERE actor_id=". $actor_id);
if($query) echo "<h2>Congratulazioni! Dati inseriti.</h2>";
else echo "<h2>Attenzione! Dati non inseriti!</h2>";
} else echo "<h2>Errore! Database non selezionato.</h2>";
}
?>

If I understood correctly that you wish to indicate which career_status option is currently selected for the record then perhaps the following might help
<select name='career_status'>
<?php
/* possible options for career status */
$status=array(
0=>'',
1=>'Active',
2=>'Retired',
3=>'Dead'
);
for( $i=0; $i < count( $status ); $i++ ){
/* is this item selected? */
$selected = $i==$career_status ? ' selected' : '';
printf('
<option value="%d"%s>%s',
$i,
$selected,
$status[ $i ]
);
}
?>
</select>

Related

PHP dropdown search result selecting wrong input

I have made a dropdown search form that is auto populated by my database content. The voices in the table would be for example types of woods with varing dimensions.
So there are repeatable wood names with diverse data.
To avoid repetition the dropdown is populated with wood types combined to be selected then displayed with all their variants.
The problem is, upon selecting an input, the results are of the item listed above and not the one selected.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
echo "<option value=\"\">Selezione Materiale</option>";
while($row = $info->fetch_assoc()){
if($row['finit'] != $finit) {
echo "<option value=\"$finit\">" . $row['finit'] . "</option>";
$finit = $row['finit'];
}
}
?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Since there are many variants(dimensions) associated with a single wood type, you have to first take the wood type as input from user(via dropdown list), and probably then you may want to display all possible variants(dimensions) of that particular wood type.
So, change the SQL query in the following way,
$query = "SELECT DISTINCT finit FROM prime";
and the while loop in the following way,
while($row = $info->fetch_assoc()){
$output = "<option value='" . $row['finit'] . "'";
if($row['finit'] == $_POST['finit']){
$output .= " selected='selected'";
}
$output .= ">" . $row['finit'] . "</option>";
echo $output;
}
Try this but change if condition according your default value and sql value should be match.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
?>
<option value="">Selezione Materiale</option>;
<?php
while($row = $info->fetch_assoc()){
if($row['finit'] == $finit) {
$selected = 'selected';
}else{
$selected = '';
$finit = $row['finit'];
}
?>
<option value="<?php echo $finit ?>" <?php echo $selected ?>><?php echo $row['finit']?></option>
<?php } ?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

Dynamically updating db with SELECT menu

Thanks for all advice! I am pulling a list of months that are not set to "current month" into a drop down/select menu in order to set some other month to be available for action. I get that list but and it populates the dropdown but then doesn't pass that value to the update. I'm either super confused or missing something very small.
Here are the queries:
if (isset($_POST['addMonth'])) {
$addmonth = $_POST['addmonth'];
//open additional months
$query_open_additional = "UPDATE reimbmonths SET reimb_open = 1 WHERE monthname = '$addmonth' ";
$query_current = "SELECT * FROM reimbmonths WHERE reimb_open = 1";
$result_current = mysql_query($query_current) or die ('Query failed: ' . mysql_error() . "<br />\n$sql");
$num_current = mysql_num_rows($result_current);
echo "Added another month: " . $addmonth;
}
And this is the HTML/PHP:
<h3>Open additional months</h3>
<form id="add-months" action="" method="post" onsubmit="window.location.reload() onchange="addMonth();"">
<select name="addmonth" id="addmonth">
<option <? if (empty($_POST["addmonth"])) { echo 'selected';} ?> disabled> --- Choose a Month --- </option>
<?
$i = 0;
while ($i < $num_months) {
$month_to_add = mysql_result($result_select_closed,$i,"monthname");
?>
<option value="<? $month_to_add ?>" <? if($_POST["addmonth"] === '$month_to_add') { echo 'selected';} ?>><? echo ucwords($month_to_add) ?></option>
<?
$i++;
}
?>
</select>
<input class="submit_btn" type="submit" name="addMonth" value="Set Month"/>
</form>

Database driven select box being empty on submit

I am trying to submit a form value in a database with php. In form a select box value comes from database.
<?php include_once 'header.php';
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
//form validion
if(isset($_POST['submit']))
{
$eid =$_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
$miatm =trim($_POST["miatm"]);
if(empty($miatm) || !preg_match("/^[a-zA-Z0-9 ]*$/",$miatm)) {
$flag=1;
$miErr="Please Enter Valid Id";
}
.............like this
if($flag==0)
{
$sqll="insert into **********";
}
//my form is
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
Its seems Ok.but when i tried to submit the form if some of one field remain empty then its show blank value in select box.
how can i remain the same selected value in select box even if textbox remain empty.
You need to retain the value of drop down after form submit.
User selected attribute of select option.
<?php
if (isset($_POST['submit'])) {
$eid =$_POST["eid"];
if ($eid=="blank") {
$flag=1;
$idErr="please Select E-MITRA";
}
}
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result)) {
$selected = (isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected="selected"' : '';
?>
<option value="<?php echo $row['uid']; ?>" <?php echo $selected;?>><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
You need to use selected="" or selected="selected" after submission in your select tag as a attribute as:
<?
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
$selected = ((isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected=""' : '');
?>
<option <?=$selected?> value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
if(isset($_POST['submit']))
{
$eid = $_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
?>
</select>
Side Note:
In your question ist two lines are not inside the php, i hope this is type error.

PHP - Display message from MySQL directly after selecting a dropdown with values of Mysql

Oopss sorry, i forgot to change it. in this code it will display the list of cakename.
<?php
$sql="SELECT * FROM $tbl_name ORDER BY c_id ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$c_id=$rows['c_id']; // idnumber of cake
$cakename=$rows['cakename']; //this is the name of cake
$options.="<OPTION VALUE=\"$c_id\">$cakename</OPTION>"; //it will list all the cake
// close while loop
}
?>
<select name="order_id">
<option value=0>Select Cake:<?php echo $options; ?>
</select>
and this one, when the user selected the cake1.it will show the details,price and the picture.thats my problem.i want to combine all the code.and i dont know how to show the picture.
<html>
<form action="current_file.php" method="POST">
<select name="order_id" id="order_id" onChange="this.form.submit()">
<option value="">Select Cake:</options>
<option value="1">cake1</options>
<option value="2">cake2</options>
<option value="3">cake3</options>
</select>
</form>
<?php
$order_id=$_POST['order_id'];
if (!empty($order_id)){
$sql="SELECT * FROM $tbl_name WHERE c_id = $order_id";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$price=$rows['price'];
$details=$rows['details'];
$picture=$rows['picture'];
// close while loop
}
echo $details;
echo $price;
}//close if statement
?>
yeah, all the codes are working.but all i want is to become one.get it?? like this but not working:
<?php
$order_id=$_POST['order_id'];
if (!empty($order_id)){
$sql="SELECT * FROM $tbl_name WHERE c_id = $order_id";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$price=$rows['price'];
$details=$rows['details'];
$picture=$rows['picture'];
$c_id=$rows['c_id']; // idnumber of cake
$cakename=$rows['cakename']; //this is the name of cake
$options.="<OPTION VALUE=\"$c_id\">$cakename</OPTION>"; //it will list all the cake
// close while loop
}
?>
<form action="current_file.php" method="POST">
<select name="order_id" id="order_id" onChange="this.form.submit()">
<option value=0>Select Cake:<?php echo $options; ?>
</select>
<?php
echo $details;
echo $price;
echo $picture;
?>
</form>
while($rows=mysql_fetch_array($result)){
$c_id=$rows['c_id'];
$options.="<OPTION VALUE=\"$c_id\">$cakename</OPTION>";
// close while loop
}
?>
<select name="order_id">
<option value=0>Select Cake:<?php echo $options; ?>
</select>
You have html error in this statement because you the html will be like this
<option value=0>Select Cake:<option value='1'>what ever</option>
so you have to close the first select option so it should be:
while($rows=mysql_fetch_array($result)){
$c_id=$rows['c_id'];
$options.="<OPTION VALUE=\"$c_id\">$cakename</OPTION>";
// close while loop
}
?>
<select name="order_id">
<option value=0>Select Cake:</option>
<?php echo $options; ?>
</select>
Try this one:
Now there's only one PHP file which will check for post message. If $_POST['pet_id'] is present, it will show the item details, otherwise it will show a select-box to the user.
<?php
if (!empty($_POST['order_id'])) {
$order_id = $_POST['order_id'];
$sql = "SELECT details FROM $tbl_name WHERE c_id = $order_id";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
// $c_id=$rows['c_id'];
$details = $rows['details'];
$price = $rows['price'];
$picname=$rows['picname']; // this is the name of the picture
$picture = '<img src="/images/' . $picname . '.jpg" />'; //i change the $pet_id to $picname the pet_id are not inluded
} // close while loop
echo $details;
echo $price; // What's the price? You haven't selected price from db...
echo $picture;
} else {
$sql = "SELECT * FROM $tbl_name ORDER BY c_id ASC";
$result = mysql_query($sql);
$options = ''; // First Create an empty variable then fill it in a loop
while ($rows = mysql_fetch_array($result)) {
$c_id = $rows['c_id'];
$cakename = $rows['c_cakename'];
$options .= "<option value=\"$c_id\">$cakename</option>\n";
}
?>
<!-- Create a form to send data -->
<form action="yourtarget.php" method="post">
<select name="order_id" onChange="this.form.submit()">
<!-- You are putting all of your options inside the first option tag. Make sure you close the first one and then echo rest of options. -->
<option value=0>Select Cake:</option>
<?php echo $options; ?>
</select>
</form>
<?php } ?>
My idea for images is to store them by their item-id in a folder, you can store image name or path in db. There are too many ways to do that.

How do I select value from DropDown list in PHP??? Problem

I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">

Categories