Code implementation in php-mysql - php

I am trying to understand the code and I am confused to understand the code at this point below. in the first example I want to fetch data from the database and display them as drop-down list but unfortunately it is not displaying anything, and in the second example everything is working fine (fetching data displaying as the drop-down list) bu the problem is that I cant understand the code in the second example it looks much more complicated than the first example. So I want to correct my first example and after that I can rebuilt the second example based on the first one.
First table is department (ID, persondepartment, Description)
Second table board ( personboard, description)
ps: this is for my project so I dont care about Security
First Example - Not Working
<td>Department</td>
<td>
<select name='persondepartment' ID="dept">
<option ID="0">-- Select Department --</option>
<?php
mysql_connect('localhost', 'data_datab', 'password');
mysql_select_db ("data");
$getalldepartments = "SELECT * FROM department";
WHILE ($viewdepartments = mysql_fetch_array(
$getalldepartments)){
?>
<option ID="<?php echo $viewdepartments['ID'];?>
"> <?php echo $viewdepartments ['persondepartment'] ?></option>
<?php } ?>
</select>
</td>
Second Example - Working
<td>Board</td>
<td>
<?php
mysql_connect('localhost', 'data_datab', 'password');
mysql_select_db ("data");
$sql = "SELECT * FROM board";
$result = mysql_query($sql);
echo "<select name='personboard'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['personboard'] . "'>" . $row['personboard'] . "</option>";
}
?>
</td>

Try to change
$getalldepartments = "SELECT * FROM department";
to:
$getalldepartments = mysql_query("SELECT * FROM department");
and consider to use mysqli instead of deprecated mysql extension.
Correct version with PDO
<td>Department</td>
<td>
<select name="persondepartment" id="dept">
<option value="0">-- Select Department --</option>
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=data', 'data_datab', 'password');
} catch (PDOException $e) {
echo '<option value="">' . $e->getMessage() . '</option>';
}
$getalldepartments = $pdo->query("SELECT * FROM department");
while ($viewdepartments = $getalldepartments->fetch(PDO::FETCH_ASSOC)) {
?>
<option value="<?= $viewdepartments['ID']; ?>"><?= $viewdepartments['persondepartment']; ?></option>
<?php
}
?>
</select>
</td>

Related

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

code for fetching value to select option

I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.

edit select option values

I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";

How to get option list from database and select it?

I want a dynamic option list which reads from database table lets say table of : Students,
It must show to user the list of student_name and when it is selected by user it must send student_id of that student to the database. For example:
Students table :
student_id student_name
----------------------------
1 John
2 Edward
In users option list must be included only John, Edward.. but when user selects John, the option picker must send only student_id('1') to database.
My current code , but is not fetching list from db :S :
yes, this is my code but for some reasons it ain't work :
<select Name='student_id'>
<option value="">--- Select ---</option>
<?
mysql_connect ("localhost","root","");
mysql_select_db ("mydb");
$select="student_name";
if (isset ($select)&&$select!=""){
$select=$_POST ['student_name'];
}
?>
<?
$list=mysql_query("select * from students order by student_name asc");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['student_id']; ?>"<? if($row_list['student_name']==$select){ echo "selected!"; } ?>>
<?echo $row_list['student_name'];?>
</option>
<?
}
?>
</select>
may this one help you
$result = mysqli_query($con,"SELECT * FROM Students");
echo "<select>";
while($row = mysqli_fetch_array($result)) {
echo "<option id=".$row['student_id'].">" . $row['student_name'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
You should fetch your database from your PHP, then build your list out of it
//YOU MUST ADD YOUR DATABASE CONNECTION
mysql_connect('localhost','username','password');
mysql_select_db("dbname");
//HERE IS YOUR SQL REQUEST
$SQL_request = "SELECT * FROM `student_table`";
$req = mysql_query($SQL_request) or die(mysql_error().'<br/>'.$SQL_request);
echo '<select name = "students">';
while($result = mysql_fetch_assoc($req)){
echo '<option value="'.$result['student_id'].'">'.$result['student_name'].'</option>';
}
echo '</select>';
?>
Try below code.
$con = mysqli_connect('localhost','root','','mydb');
$sql="SELECT student_id ,student_name FROM students";
$result = mysqli_query($con,$sql);
?>
<select name = "student">
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['student_id']; ?>"><?php echo $row['student_name']; ?></option>
<?php
}
?>
</select>

Populate html <select> with array data from mysql in PHP

I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular

Categories