populating my dropdown menu using procedural prepared statement - php

I would like to ask on how do I populate my dropdown/<select> via retrieving my data in MySQL database. I am using procedural prepared statement to add a security or avoid SQL injection.
Problem: It's only retrieving one data from my database which is I do have a two data stored in my table and how do I insert it through my <option> tag? I'm currently doing right now is first retrieve all research_title.
index.php
<form action="#" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="LabelTitle">Research Title</label>
<?php
include 'includes/includes_getOptionList.php';
?>
</div>
includes_getOptionList.php
<?php
// Connection in DB is stored here
include 'connection_operation.php';
$query = "SELECT research_title FROM tbl_topicresearch";
$smtmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($smtmt, $query)) {
echo "SQL Statement" . mysqli_stmt_error($smtmt);
} else {
mysqli_stmt_execute($smtmt);
$getResult = mysqli_stmt_get_result($smtmt);
if ($getResult) {
?>
<select class="form-control" name="research_title" id="research_title">
<?php
while ($row = mysqli_fetch_assoc($getResult)) {
$title_research = $row['research_title'];
echo $title_research;
echo '<option value="<?php echo $row[research_title]; ?>"> <?php echo $row[research_title]; ?> </option>';
?>
</select>
<?php
}
} else {
}
}
?>

The </select> close tag should be outside the while loop.
You must not include PHP inside PHP (ex: echo '<?php ... ?>';).
You have an extra call (echo $title_research;).
Code:
if ($getResult)
{
echo '<select class="form-control" name="research_title" id="research_title">';
while ($row = mysqli_fetch_assoc($getResult))
{
$title_research = $row['research_title'];
echo '<option value="' . $title_research . '">' . $title_research. '</option>';
}
echo '</select>';
}
else
{
echo '<p>no results</p>';
}
Or:
<?php if ($getResult) : ?>
<select class="form-control" name="research_title" id="research_title">
<?php while ($row = mysqli_fetch_assoc($getResult)): ?>
<option value="<?php echo $row['research_title'] ?>">
<?php $row['research_title'] ?>
</option>
<?php endwhile; ?>
</select>
<?php else: ?>
<p>no results</p>
<?php endif; ?>

Related

confused in echo mysql result inside echo

after sign in and redirect to next page, i check the user with $_SESSION.
Then if username valid, i want to show dropdown list which the list is get from database. I am confuse how to echo.
<?php
session_start(); //Start the session
if(!isset($_SESSION['pic']))
{
header('Location:index.php?p=signin');
}
else {
echo '<div class="form-group">
<label for="symcat" class="control-label">Symptom Category</label>
<select id="symcat" name="symcat" class="selectlist form-control">
<option value=""></option>
$sql='SELECT category FROM sym_category';
if ($sql) {
$res=mysqli_query($dbc,$sql) or die(_ERROR26.': '.mysqli_connect_error());
}
while ($dat = mysqli_fetch_array($res, MYSQLI_NUM)) {
echo '\t<option value="'.$dat[0].'">'.$dat[0].'</option>\n';
}
mysqli_free_result($res);
</select>
</div> <!-- /form-group -->';
}
?>
This would probably be better:
<?php
session_start(); //Start the session
if(!isset($_SESSION['pic']))
{
header('Location:index.php?p=signin');
}
else {
$sql = 'SELECT category FROM sym_category';
if ($sql) {
$res = mysqli_query($dbc,$sql) or die(_ERROR26.': '.mysqli_connect_error());
}
?>
<div class="form-group">
<label for="symcat" class="control-label">Symptom Category</label>
<select id="symcat" name="symcat" class="selectlist form-control">
<option value=""></option>
<?php if (isset($res)): ?>
<?php while ($dat = mysqli_fetch_array($res, MYSQLI_NUM)): ?>
<option value="<?php echo $dat[0] ?>"><?php echo $dat[0] ?></option>
<?php endwhile ?>
<?php mysqli_free_result($res); ?>
<?php endif ?>
</select>
</div> <!-- /form-group -->
<?php } ?>
Honestly I still don't think this is a very nice way. If possible, please use view files.
I think it's reasonable to say that you shouldn't use an echo within another echo. Echo is used to print something to your screen, so it shouldn't be necessary to use an echo within that same echo.
Check out this sample code. Maybe it'll help you.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Fetch the option list details from DB
$optionList = array();
$sql = "SELECT category FROM sym_category";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$optionList[] = $row['category']
}
} else {
echo "0 results";
}
// Create the dropdown list
echo "<div class='form-group'>";
echo '<label for="symcat" class="control-label">Symptom Category</label>';
echo '<select id="symcat" name="symcat" class="selectlist form-control">';
echo '<option value=""></option>';
foreach ($optionList as $key => $value)
{
echo "<option value='$value'>$value</option>";
}
echo '</select>';
echo '</label>';
echo "</div>"
As Akintunde & SheperOfFire said the nice and tidy way is like :
<?php
session_start(); //Start the session
if(!isset($_SESSION['pic']))
{
header('Location:index.php?p=signin');
}
else {
header('Location:index.php?p=forminputcar');
}
?>
Because, inside of forminputcar has php tag and echo also. Or you can also put above script at the top of forminputcar page. So, if session match we stay on current page.
<?php
session_start();
if(isset($_SESSION['pic']))
{
header('charset=UTF-8');
}
else {
header('Location:index.php?p=signin');
}
?>

How to select only id instead of print name in select options?

<select id="rightsid" name="rightsid">
<?php
while($row=mysql_fetch_array($list)) {
$admno=$row["adm_no"];
$name=$row["name"];
?>
<option id="<?php echo $admno;?>" name="<?php echo $admno;?>"><?php echo $admno;?> <?php echo $name;?></option>
<?php
}
?>
<input type="submit" value="Show Rights">
</select>
This is my code. When I take the value of select to next page, it is taking both name and admno but I want to take only admno but here in dropdown list. I want to display both but want to carry only one. How to do this?
Within option value, write $admo. Try this:
<?php
while ($row=mysql_fetch_array($list)) {
$admno = $row["adm_no"];
$name = $row["name"];
echo '<option id = "'.$admno.'" name = "'.$admno.'" value = "'.$admno.'" >' . $admno . $name . '</option>';
}
?>
Use value in option as below
<select id="rightsid" name="rightsid">
<?php
while($row=mysql_fetch_array($list)) {
$admno=$row["adm_no"];
$name=$row["name"];
?>
<option value="<?php echo $admno;?>" id="<?php echo $admno;?>" name="<?php echo $admno;?>"><?php echo $admno;?> <?php echo $name;?></option>
<?php
}
?>
</select>
<option id="<?php echo $admno;?>" value="<?php echo $admno;?>"><?php echo $name;?></option>

validate if checkbox has been checked

I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19

HTML Option tag with selected value from PHP

Can you please help me out as to what I am doing wrong with this code:
<option value="">------------ Select ------------</option>
<?php while (($row = mysql_fetch_array($tech))) {
?>
<option value= <?php echo $row['id'] ?>
<?php echo $selectedtechname ?>
<?php echo "selected" ?>
>
<?php echo $row['technician_name'] ?></option>
<?php } ?>
All variables are working fine since I used Var_dump and they echo correctly so no errors is SQL etc. What I need to do is pass a php statement in the option tag of HTML to simply select the value of $selectedtechname from the list. The value to post is $row['id'] but the selected value to show should be $selectedtechname
Thanks
I see a similar post
populate a select box with php mysql
while($row = mysql_fetch_assoc($result)) {
if ($row['technician_name'] == $selectedtechname) {
echo '<option value=\"'.$row['id'].'" selected>'.$row['id'].'</option>';
} else {
echo '<option value=\"'.$row['id'].'">'.$row['id'].'</option>';
}
}
You can try the following:
$selected='';
<option value="">Select a value</option>
<?php
while ($row = mysql_fetch_array($tech)) {
?>
<?php
if($selectedtechname == $row['selectedtechname'])
{
$selected='selected';
}
else
{
$selected='';
}
?>
<option value="<?php echo $row['id']?>" <?php echo $selected;?> ><?php echo $row['value_to_display'];?></option>
<?php
}
?>

Select list move down

I'm developing a webpage with a select list that contains images.
I already have this:
When I select an image name in the list the image will be displayed in the div below.
<?php
// Create connection
$con=mysqli_connect("******","***","***","charts");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<form method="post" action="index.php" id="nano" name="nano">
<p>
<select name="SelectBox" id="SelectBox" onchange="this.form.submit()">
<?php if($_POST['submitted'] == true){ ?>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano WHERE IMAGE_NAME ='". $_POST['SelectBox']."'");
while($row = mysqli_fetch_array($result))
{ ?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php } ?>
<?php echo $_POST['SelectBox']; ?></option>
<?php } else{ ?>
<?php
$result = mysqli_query($con,"SELECT TOP * FROM Nano");
while($row = mysqli_fetch_array($result))
{
?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php
$var1 = $row['IMAGE_NAME']; ?>
<?php
}
?>
<?php } ?>
<option value="" disabled="disabled"> -------- </option>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano");
while($row = mysqli_fetch_array($result))
{ $values[] = $row['IMAGE_NAME'];
?>
<option value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php }?>
</select>
<input type="hidden" name="submitted" id="submitted" value="true" />
</p>
<?php if($_POST['submitted'] == true){ ?>
<p><img src="Images\Nano\<?php echo $_POST['SelectBox']?>" width="953" height="600" /></p>
<?php }else { ?>
<p><img src="Images\Nano\<?php print_r($values[0]) ?>" width="953" height="600" /></p>
<?php } mysqli_close($con);?>
</form>
</div>
I want when I move down in the select list the picture will change and not when I click on it in the select list.
In your case, you have to bind hover event to option, but there is no way to do what you want using native select control. The native one only answers when you click a different option from previous.
However, you can simulate a select control using html&css&js, that way when your cursor move down the simulated option(which might be a div or something), you can bind event handlers to it and display the name.

Categories