submit selected data to table php - php

$sql = 'SELECT a.guest_id, g.fname, g.lname, g.ph1, g.email, g.arrpickup, g.arrdropoff, a.approxtime, a.vehicle_id1, a.vehicle_id2, a.vehicle_id3,FROM arrivals a INNER JOIN guest g ON a.guest_id = g.guest_id WHERE g.guest_id = 18';
with this I am getting data from the database using inner join.
$html = null;
$html .= '<select>
<option value="-1"> ----Select Vehicle---- </option>';
$sql1 = 'SELECT * FROM vehicle ORDER BY vehicle_id ASC';
foreach ($pdo->query($sql1) as $row1) {
$html .= '<option value="">'. $row1['model'] . ' - ' . $row1['licplate'] . ' </option>';
}
$html .= '</select>';
this is drop down mechanism for getting data from table
its showing using
echo '<tr><td>Vegicle 1</td><td>'.$html.'</td></tr>';
now I want to store selected value to table so where to put insert query ? all connection stuff again ?

$upd_row['your value'];
change your get table value in inert a database.
and follow this code and some changes your requerment.
i hope help in your code.
$idproduct= "select * from `country_master`";
$str = mysql_query($idproduct);
$select="";
$issel = '';
if(mysql_num_rows($str)>0)
{
while($viewrow=mysql_fetch_array($str))
{
if($upd_row['your value'] == $viewrow['id'])
{
$select = "selected";
$issel = 'yes';
}else
$select ="";
$opts .= "<option value='".$viewrow['id']."'".$select." >".$viewrow['country_name']."</option>";
}
}
if($issel == '')
$opts = '<option value="" selected>--Select--</option>'.$opts;
else
$opts = '<option value="" >--Select--</option>'.$opts;
echo $opts;
?>

Related

how to use php condition in html option tag

i have this function and i want use "selected" attribute in "option" tag when $selected is equal to $row['id'].
please pay attention it's not an usual html tag between php codes, pay attention html option tag add to $output variable every time in while loop.
public function getCategoriesList(&$output = '',
$parent = 0, $seprator = '', $selected=1)
{
$sql = "SELECT * FROM categories WHERE `parent_id` = $parent ";
$stmt = $this->pdoConnection->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$output .= "<option value=".$row['id']." ($row[id]==$selected)? selected :''>
".$seprator.$row['title']."
</option>";
$this->getCategoriesList($output, $row['id'],
$seprator . ' - ');
}
return $output;
}
You can make a separate condition for selected attribute. Example:
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$selected = $row['id'] == $selected ? "selected='selected'" : '';
$output .= "<option value='{$row['id']}' {$selected}>{$seprator}{$row['title']}</option>";
$this->getCategoriesList($output, $row['id'], $seprator . ' - ');
}

Show all tables from database into select form in php

Hiho,
I have a little problem with showing all table names on page in form .
Code bellow:
<select name="users" onchange="showTables(this.value)">
<option value="">Select a table:</option>';
$result = mysql_list_tables($db_name);
for ($i = 0; $i < count(mysql_num_rows($result)); $i++){
echo '<option value="' . mysql_tablename($result, $i) . '">' . mysql_tablename($result, $i) . '</option>';
}
echo '</select>
</form>
<br>
<div id="tablesDb"><b>All content from selected table will be listed there.</b></div></div>';
I try with mysqli too ,and i get results but still without names of the tables and nothing can be select.
Maybe someone know how to get this work.
First, you definitely want to be using MySQLi, as the MySQL extension is deprecated. Second, you can probably get all of the data you need in a simple SELECT query like this:
select `TABLE_NAME` from information_schema.tables
Try this
$mydbname = 'database_name';
$con=mysqli_connect("localhost","my_user","my_password",$mydbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$options = '';
// for the query you can use two of the following lines (line1+line2 OR line3+line4)
//Use line (line1+line2)
$result = mysqli_query($con,"SHOW TABLES");
$column_name ='Tables_in_'.$mydbname;
// OR (line3+line4
$result = mysqli_query($con,"SELECT TABLE_NAME AS tbl FROM information_schema.tables" WHERE TABLE_SCHEMA = \"'.$mydbname.'\"; ");
$column_name ='tbl';
while($row = mysql_fetch_array($result))
$options .= '<option value="' . $row[$column_name] . '">' . $row[$column_name] . '</option>';
echo '<select name="users" onchange="showTables(this.value)">';
echo '<option value="0">Select a table:</option>';
echo $options;
echo '</select>';
I modify code to this:
<select name="db_tablelist" onchange="showTables(this.value)">
<option value="">Select a table:</option>';
$result = mysqli_query($con,"SHOW TABLES");
while($row = mysqli_fetch_array($result)) { echo '<option value="' . $row[0] . '">'.$row[0].''; } echo '';
echo '</select>
</form>
<br>
<div id="tablesDb"><b>All content from selected table will be listed there.</b></div></div>';
And now everything works .

PHP: select current option

I have a DB table with all categories (id, category) and table with all of events (id, event, categoryID).
And on the event editing form I have the select field with all of the categories (getting from the DB). But sinse I'm developing an editing form, I need to select current category by default.
This is my select field (PHP method that gets all the categories from DB and puts them in the following order):
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3">Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>
Let's say, current event is under category 3, so I need the following HTML to be generated:
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3" selected>Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>
How do I achieve it with PHP, if I have the catID?
Hopefully, this question is clear enough. Sorry for my bad explanation
UPD: This is my PHP code that generates category list:
public function getCatList($conf) {
$mysqli = $this->dbConnect($conf);
// Quering...
$query = "SELECT * FROM categories";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['id'] . '">' . $row['category'] . '</option>';
}
}
Appending to your while iteration a condition will solve this for you:
while($row = mysqli_fetch_array($result)) {
$isSelected = $row['id'] == $catID;
echo '<option '.($isSelected ? 'selected="selected"' : '').' value="' . $row['id'] . '">' . $row['category'] . '</option>';
}
You're making a comparison if the current value is the same as that stored in $catID - and store the boolean result in a variable. In the echo you're just doing a conditional forking and appending the selected attribute if the value was true, otherwise not appending any empty string.
You can do it like
while($row = mysqli_fetch_array($result)) {3
echo '<option value="' . $row['id'] . '"';
//if condition is met then make the option selected
if($row['categoryID'] == 3) {
echo " selected='selected' ";
}
echo '>' . $row['category'] . '</option>';
}
you should add the selected catID as a parameter to your getCatList-function. Then just change the creation of your HTML to include the selected-attribute:
public function getCatList($conf, $catID = 0) {
$mysqli = $this->dbConnect($conf);
// Quering...
$query = "SELECT * FROM categories";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"' . ($row['id']==$catID?' selected':'') . '>' . $row['category'] . '</option>';
}
}
now you can just pass the catID to your function:
$myConf = "something";
$myCatID = 3;
getCatList($myConf, $myCatID);
//query for the categories and options
$current_value = 3;
$total_num_of_options = mysql_num_rows($your_query_result);
for($count = 1; $count <= $total_num_of_options){
echo "<option value='".$count."' ";
if($count == $current_value)
echo "selected";
echo ">cat".$count."</option>";
}

Mark an option as selected

I am populating a dropdown menu with data in a database, using this code:
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '">' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
I want to add the selected=selected to the option selected and then use that option in another query, but I have problem adding the selected tag in the actual selected entry.
More INFO:
I am using dropzone.js to upload images, and I want to select the category on the fly. The category has to be selected from the dropdown menu and used in the INSERT query.
if you have a form with POST then the code is
$selected = "";
if($_POST['galleries']==$name) $selected=" selected='selected'";
echo '<option value="' .$name. '"'.$selected.'>' .$name. '</option>';
Just as an example try this,
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
if( $i == 1 )
{
$sel = 'selected="selected"';
}
else
{
$sel = '';
}
echo '<option value="' .$name. '"'. $sel .' >' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
If you want to select a specific option then you are going to have a variable containing that specific option example:
$conditionalName = "Mark";
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '"'; if($name=$conditionalName){ echo ' selected=selected';} echo '>'. $name. '</option>';
}
echo '</select>';
echo '</label>';
}

how can i make a default value if the option value of a select tag isnt a specific value?

<select name="corequisite">
<option value="" selected="selected">No Corequisite</option>';
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res)){
echo'<option value="'.$row['Code'].'"';if($row['Code']==$result['corequisite']) echo'
selected="selected"';;echo'>'.$row['Name'].'</option>';
}
echo'</select>';
I want the 'No Corequisite' value to be selected if no other options will be match with $result['corequisite']. But the first item in the table will be selected!
How can I fix it?
It may be fixed by inserting 'corequisite' as the first record. A good way?
Answering your last question, probably a bad way.
Try this code, currently on the laptop so haven't tested it.
<select name="corequisite">
<?php
$res = mysql_query("SELECT * FROM course");
$rows = mysql_num_rows($res);
if ($rows == 0)
{
$sel = 'selected';
} else
{
$sel = '';
}
echo '<option '.$sel.' value="">No Corequisite</option>';
while($row = mysql_fetch_array($res))
{
$sel = '';
if ($row['Code'] == $result['corequisite'])
{
$sel = 'selected';
}
echo '<option '.$sel.' value="'.$row['Code'].'">' . $row['Name'] . '</option>';
}
?>
</select>
I haven't tested it, but this should work
<select name="corequisite">
<?php
// this is your string that will return all your results
$to_echo = '';
// this will check if any value inside is selected ( this will be useful later )
$any_item_selected = false;
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res))
{
if($row['Code']==$result['corequisite']) // OK! so one item is selected, remember it!
{
$to_echo .= '<option selected="selected" value="'.$row['Code'].'">'.$row['Name'].'</option>';
$any_item_selected = true;
}
else
$to_echo .= '<option value="'.$row['Code'].'">'.$row['Name'].'</option>';
}
// Now we can check if anything were selected, and prepend the "No corequisite" option, selected or not! :)
if( ! $any_item_selected)
$to_echo = '<option value="" selected="selected">No Corequisite</option>' . $to_echo;
else
$to_echo = '<option value="">No Corequisite</option>' . $to_echo;
echo $to_echo;
?>
</select>

Categories