How to Update Database Table Using Dropdown in Datatable - php

I've spent more than 48hrs trying to solve the following problem to no avail. I want to use datatable in php to update record. I am able to display record based on the user selected, However, some of the inputs are <Select>.
The following code populates the options but I'm getting
Undefined Index error
<td><select name="class_assigned['<?php echo $id; ?>']" class="form-control
class_assigned"><option value="<?php echo $class_assigned; ?>"><?php echo
$class_assigned; ?>
</option>
<?php
$class_sql = "SELECT Class_Name FROM tbl_classes WHERE Class_Name !=
'$class_assigned'";
$class_result = $conn->query($class_sql);
if ($class_result->num_rows > 0) {
while($row = $class_result->fetch_assoc()) {
$class_name = $row['Class_Name'];
?>
<option value="<?php echo $class_name; ?>"><?php echo $class_name; ?>
</option>
<?php }} ?>
</select>
</td>
While the following code populates the Current Class_Name but I couldn't make other options appear:
<td><?php echo '<select name="class_assigned['.$row["id"].']" class="form-control"><option value="'.$class_assigned.'">'.$class_assigned.'</option></select>'; ?></td>
What I want:
Let's say the table displays SS 1A, and the user wants to update to SS 2A. I want the Options in the dropdown to display all classes available in tbl_classes.
Please have in mind that the table can display multiple rows at a time (Array).
Thank you for your help.

Related

submit two values from two different dropdown list in php

I have a function having two dropdown list and one submit button, the user must choose a value from dropdown list A and a value from dropdown list B so he can submit them.
I knew that I need to have an attribute in the value section as follows
<select name="students" id="students">
<?php
while($data = mysqli_fetch_array($selectstudent1)) {
$name=$data['name'];
$idname =$data['user_id_student'];
?>
<option name="studentdd" value="<?php echo $idname; ?>"><?php
echo $name; ?></option>
<?php } ?>
</select>
and the second dropdown list here
<select name="groups" id="groups">
<?php
while($data1 = mysqli_fetch_array($selectgroup1)) {
$groupschoose = $data1['group_number']; ?>
<option name="nogroup" value="<?php echo $groupschoose; ?>">
<?php echo $groupschoose; ?></option>
<?php }//while ?>
</select>
so, i need to get the "php echo $idname" (student id) from A dropdown list
and "php echo $groupschoose; " (group id) form drop downlist B
so I tried this PHP code as follows:
<?php
if(isset($_POST['addmember'])){
//addmember is id for dropdownlists form
$groupnum= $_POST['nogroup'];
$stuid=$_POST['studentdd'];
echo $groupnum;
echo $stuid ;
$sq="SELECT user_id_student FROM student WHERE
user_id_student=$stuid ";
$sq1 = mysqli_query($con,$sq);
while ($sq2 = mysqli_fetch_array($sq1)) {
$tes = $sq2['user_id_student'];
if( $tes == $stuid){
$sqlmem="UPDATE student
set group_id= $groupnum
WHERE user_id_student=$stuid";
$resultmem = mysqli_query($con,$sqlmem);}
}
}
?>
But it won't work, and I'm not sure where is the missing section
I hope my Q is clear and I'm sorry if it's little bit long, but i'm looking for your help, thank you!!

Drop down list not populating (PHP/Myadmin)

I am trying to get a dropdown list to populate. I have two tables, a registration table and a studentclasses table. I am looking to populate the dropdown list from the class table, as a field entry in the registration table. My code is as follows:
<p>Class: <select name="classID">
<?php $classlist_sql="SELECT * FROM studentclasses";
$classlist_qry=mysqli_query($dbconnect, $classlist_sql);
$classlist_rs=mysqli_fetch_assoc($classlist_qry);
do { ?>
<option value="<?php echo $classlist_rs['classID']; ?>"
<?php
if($classlist_rs['classID']==$_SESSION['signuptest']['classID']) {
echo "selected=selected";
}
?>
><?php echo $classlist_rs['classcode']; ?></option>
<?php } while ($classlist_rs=mysqli_fetch_assoc($classlist_qry));
?></select>
</p>
My Studentclasses table has the fields classID and classcode, and I am wanting the classcode to appear in the drop down.
Any help would be greatly appreciated.

showing selected option from mysql in dropdown

I have a dropdown showing options from mysql, no problem there. The problem is on the update page when i want to show the option already selected previously.
The dropdown selects options from the margins table and puts the value into a field in the products table.
This is the query that selects the product record :
<?php
$recordID = $_GET["recordID"];
$product_result = mysqli_query($con,"SELECT * FROM products WHERE product_code='$recordID'") or die(mysqli_error($con));
$product = mysqli_fetch_array($product_result);
$checked_special = $product['product_special'];
$checked_publish = $product['product_publish'];
$checked_frontpage = $product['product_display_frontpage'];
$checked_facebook = $product['display_facebook'];
{
?>
And this is the part of the form that gets the options from the margins table and displays them on page.
<tr>
<td>Display Facebook</td>
<td><input type="checkbox" name="display_facebook" id="display_facebook" value="y" <?php if ($checked_facebook == 'y') echo 'checked'; ?> /></td>
<td><strong>Margin Group :</strong></td>
<td>
<select name="margin_group" id="margin_group"><?php
$resul2 = mysqli_query($con,"SELECT * FROM margins");
while($row2 = mysqli_fetch_array($resul2))
{
?> <option value="<?php echo $row2['margin_group']; ?>"> <?php echo $row2['margin_group']; ?></option>
<?php } ?> </select></td>
</tr>
How can i get the $product['margin_group'] value from the products table show as selected option in the dropdown, so that the user doesn't have to reselect every time they update the page.
Thanks :)
MsKazza
The idea is to add the word selected in the desired option tag like this :
<option value="x" selected>x</option>
This way it will be selected in the form Check this
in order to do that we will make a conditional statement for every option value in the while loop. If the value meets the condition, we will echo the word selected
<?php while($row2 = mysql_fetch_array($resul2): ?>
<option value="<?= $row2['margin_group']; ?>"
<?php if($row2['margin_group']) == $products_table_variable) : ?>
selected
<?php endif; ?>
><?= $row2['margin_group']; ?></option>
<?php endwhile ?>
<select name="margin_group" id="margin_group">
<?php
$datasource = mysqli_query($con,"SELECT * FROM margins");
while($getdata= mysql_fetch_array($$datasource)){
?>
<option value="<?=$row2['margin_group']?>" <?php if($getdata['colume_name']==$row2['margin_group']) echo "selected";?>> <?=$row2['margin_group']?></option>
<?php } ?>
</select>
Hope it will help you :)

Updating MySql DB with Dropdown values

My update statement isn't working correctly, I am attempting to pull the data from the database, populate a dropdown with either "Y" or "N" inside it, on submit the values are entered into the database and the page refreshes.
So far I have my list of items, each with correctly populated dropdown, it is now my submit that is failing to work.
<?php
$updatedFeatProd = $_POST['featuredProduct'];
var_dump($updatedFeatProd);
if ($_POST) {
foreach ($_POST['featuredProduct'] as $key => $val) {
$query = 'UPDATE tblProducts SET featuredProduct = ' . $updatedFeatProd . '
WHERE fldID = ' . $val;
$sql = dbQuery($query);
}
}
$sql = dbQuery('SELECT fldId, fldName, featuredProduct FROM tblProducts');
?>
<form method="post" action="#" name="featuredProd">
<table>
<tr><td><p>Product Name</p></td><td><p>Is a featured product?</p></td></tr>
<?php
$products = dbFetchAll($sql);
foreach ($products as $product) {
//var_dump($product['fldName']);
?>
<tr>
<td>
<p><?php echo $product['fldName']; ?></p>
</td>
<td>
<select name="featuredDropdown">;
<?php
if ($product['featuredProduct'] == 'Y') {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">N</option>
<?php
} else {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">Y</option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
The presentaton here does not make much sence. You have a dropdown with the ProductName in one slot and a 'N' in another.
Once the user has selected 'N' for a product they have no idea what they have said NO to, as they can no longer see the product name that they have selected NO for.
It would make more sence to provide a <label> containing the product name and a YES/NO dropdown beside it for them to select from.
However the reason your update code is not working is that you have called the dropdown featuredDropdown
<Select name="featuredDropdown">
and you are trying to process a field called featuredProduct in the update code
foreach ($_POST['featuredProduct'] as $key => $val) {
Your next problem will probably be that you are oututting more than one <Select name="featuredDropdown"> so you need to make that into an array as well like this:
<Select name="featuredDropdown[]">
Then you will have an array of featuredDropdown in the $_POST array. $_POST['featuredDropdown'][]

Echoing row name where posted ID, for pre-selected form value

This will hopefully be an easy one, but I'm lacking the skills!
<select name="search_category" id="select1" >
<option value="">By Category</option>
<?php if (!empty($_POST['search_category'])) { ?>
<option value="<?php echo $_POST['search_category']; ?>" selected="selected"><?php echo $_POST['search_category']; ?></option>
<?php }?>
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
The above is one of many select in a search module. It returns a dynamic list of options from a query higher up in my page. My goal is to have the option last searched pre-selected. Everything works as intended, but my problem is minimal really; the value of the posted search category is an ID($row->id. What I am hoping to do is use the associated $row->name for display, but keep the id for value so my search function still works.
In other words, I'm hoping to do something like:
<?php echo $row->name; WHERE ID = $_POST['search_category']
Is there an easy way to do that in the above code, or will I need to add a special query at the top of my page, fetching the individual row name that matches the posted id?
Thanks!
EDIT: To simplify, I already have a query that returns row->id and row->name, which I use in a foreach loop to populate my option values and names. I simply need a way, or a line that I can add to my query to also get the value of the row->name that matches the POSTED id.
i would write a short function which gives this functionality even for others applications.
just like
<?php
function($id, $table) {
select ... etc
}
?>
For security Reasons I would suggest to use Prepared Statements or mysql_escape
Hope i could help
Perhaps
SELCET('id', 'name' FROM yourTable WHERE 'name' = $_POST['ID'])
you mean something like this or would you select the dropdown option
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"
<?php if($row->name == $_POST['search_category']) : ?>
selected="selected"<?php } ?>>
<?php echo $row->name; ?>
</option>
<?php endforeach; ?>

Categories