MySQL and PHP delete row from dropdown - php

I wish to use dropdown to list all rows from database. When I select one option, delete that selected row with button.
Here what I have by now, this only shows dropdown:
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Nije se moguće konektovati: ' . mysql_error());
}
mysql_select_db("videoteka", $con);
$result = mysql_query("select ime,id from filmovi");
$options="";
echo "Odaberite film:";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$ime=$row["ime"];
$options.="<OPTION VALUE=\"$id\">".$ime.'</option>';
}
mysql_close($con);
And in body:
<select name=thing>
<option value=0><?=$options?></option>
</select>

Ok, so two things, just to try and put you in the right direction: first, in the body you can remove the <option> tag, since your php variable already contains those.
<select name=thing>
<?=$options?>
</select>
Second, the delete part. There are different ways to do this, but one thing you will certainly need is an HTML form. Your select (and the button) will need to be in this form, which will be submitted to this very same PHP page. In the beginning of your PHP code you will check the $_POST variable to determine what row to delete. I hope you know what $_POST is, otherwise this is going to be a pretty useless explanation.

Related

Selecting the value of a mysql populated drop down list

I am trying to use the value of a mysql populated dropdown list in with php and keep getting a blank return. I have scoured the internet and found very similar questions on here to my problem but everyone i try seems to fail. The select list populates without a problem using the code below.
<form action="includes/insertEquine.php" method="POST">
<label for="select-gender" class="select">Choose Gender</label>
<select name="select-gender id="select-gender">
<?php
$sql = ("SELECT genderType FROM gender");
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<option value ='".$row['genderType']."'>" . $row['genderType'] . "</option>";
} ?>
</select></form>
when i try to select it using the below I get an empty space returned.
<?php $genderType = $_POST['select-gender']; ?>
Any further suggestions would greatfully recived.
I have already looked at the below with no success
Getting value from populated drop down list
and
How to dynamically get the value of a select tag from MySQL table .
You are missing a " in
<select name="select-gender id="select-gender">
So replace it as
<select name="select-gender" id="select-gender">
That is why you arent getting a value with key select-gender in $_POST

Save result from dropdown list to another table

I have two tables in my database. I have a form to add information to table news. In my form I have dropdown list with information from another table courses. I want to save the result of the selection and save it in table news. How I can do that?
code:
<p>Course</p>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("university");
$sql = "SELECT name FROM courses";
$result = mysql_query($sql);
print '<select>';
while ($row = mysql_fetch_assoc($result)) {
print '<option value='.$row['name'].'>'.$row['name'].'</option>';
}
print '</select>';
?>
First you would need to give a nameattribute to your select tag, then as soon as you post your form, you will get the value of the selected option the exact same way as you get the value of other input fields.
I would also advise to get both the id and the name of your courses from your selct query. You should then use the id as the value attribute of your option , qhile you can keep the name to display it to the user. That way you would store the id of the selected course instead of the name, meaning that if a name slightly changes, you will still have the proper course id stored.
So your final HTML for this field should look something like that :
<select name="selectedcourse">
<option value="1">Course 1</option>
<option value="2">Course 2</option>
<option value="3">Course 3</option>
</select>
And you would get the seleted value server-side in PHP using something like :
$_POST["selectedcourse"]
If, of course, your form method was POST and not GET...

How to populate dependant select menus dynamically using jQuery, PHP and MySQL?

I have searched quite a bit on here about this topic. But I could not find a solution for my problem. I'd appreciate it a lot if you could help me, this is for a school project I am working on.
I have a database with a table ("Main_table") and columns including "sector" and "sub_sector". I want to have two select boxes, first one will load all the records from database in "sector" column and the second one will load all the records from database in "sub_sector" column depending on the selection value of the first select box. (For example: If I select "plastics" on the first select box, then second select box should be updated with sub_sector values where sector value is equal to "plastics").
I have managed to load the options values from database for the first select box but when I click on any selection, it does not load any option to the second select box. You can find the codes below. I did not put "sector_options.php" below, as it seems to work just fine.
index.html shown below:
<script>
$(document).ready(function() {
$('#filter_sector')
.load('/php/sector_options.php'); //This part works fine - uploads options to the first select box
$('#filter_sector').change(function() {
$('#filter_subsector').load('/php/subsector_options.php?filter_sector=' + $("#filter_sector").val()
} //This part does not work - no options on the second select box
);
});
</script>
<body>
<div id="sectors"><p>Sector:</p>
<select id="filter_sector" name="select_sector" multiple="multiple" size="5"> </select>
</div>
<div id="subsectors"><p>Sub Sector:</p>
<select id="filter_subsector" name="select_subsector" multiple="multiple" size="5"> <option value="" data-filter-type="" selected="selected">
-- Make a choice --</option>
</select>
</div>
</body>
</html>
sector_options.php shown below:
<?php
$link = mysqli_connect("*******", "*******","******","********") or die (mysql_error());
$query = "SELECT sector FROM Main_table ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {
$options .= "<option value=\"".$row['sector']."\">".$row['sector']."</option>\n ";
}
echo $options;
?>
subsector_options.php shown below:
<?php
$link = mysqli_connect("********", "*****,"*******", "********") or die (mysql_error());
$Sectors = $_REQUEST['filter_sector'];
$query = "SELECT sub_sector FROM Main_table WHERE sector='$Sectors'";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {$options .= "<option value=\"".$row['sub_sector']."\">".$row['sub_sector']."</option>\n ";
}
echo $options;
?>
For completeness, the solutions were:
Check how AJAX operations are doing using a browser network monitor
Load AJAX fetcher scripts in a browser tag - in many cases they will render quite happily there, allowing them to be more easily debugged
AJAX scripts that return HTML for injection should only return that HTML, and not a full HTML document.

retrieve mysql data as array and display in multiple combo boxes

i am trying to use the mysql data items into the select combo box. it basically works well but the problem is when there are multiple combo boxes it is a lot of load to the server since adding each combo box takes a lot of time. i am trying to figure out a better way. may be pull date once into an array just for the session and place it in the combo boxes.
The logic is basically it is a quotation form where about 3500 items will be shown as drop down and user will select and then enter price and other details. the rows are dynamically added or deleted by the user.
i am currently using the following code:-
<?php
$con = mysql_connect('blah blah blah');
if (!$con) {
die ('Could not connect: ' . mysql_error());}
$db = mysql_select_db('blah',$con);
$extract1 = mysql_query("query") OR die (mysql_error());
$numrows1 = mysql_num_rows($extract1);
echo "<select name='itemname' title='select Item Name'>";
echo "
<option>Select Item Description</option>
";
while ($row1=mysql_fetch_assoc($extract1))
{
$ic=$row1['ItemName'];
echo "
<option>$ic</option>
";
}
echo "</select>";
mysql_close($con);
?>
Don't echo your option do this in your while statement:
$ic[]=$row1['ItemName'];
then outside of the while loop anywhere on the page:
foreach($ic as $i){
echo "<option>".$i."</option>";
}
First off, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
<?php
$con = mysql_connect('blah blah blah') or die(mysql_error());
$db = mysql_select_db('blah',$con) or die(mysql_error());
$result = mysql_query("query MAYBE NARROW DOWN TO MORE RELEVANT RESULT SET") or die (mysql_error());
$option = '<select size="1" name="optionBox">';
if(mysql_num_rows($result)>=1){
while ($row=mysql_fetch_assoc($result)){
$option .="<option selected value=\"".$row['ItemName']."\">".$row['ItemName']."</option>\n";
}
}else{
$option .='<option selected value="0">No items to list</option>';
}
$option .='</select>';
echo $option;
mysql_close($con);
?>
Yes, if your data changes infrequently enough, it may be a good idea to put the data into an array on the session, and render it from there. Depending on the frequency of change of your data, you might be able to get away with rendering it to a non-session data element (for example, a file on your filesystem) and populating your comboboxes from there (or just rendering all your choices into the combobox elements in that file, and using that data directly); that depends on the frequency by which your data is updated, of course.

how to get php to read in a string of words from a dropdown box?

Can any one please help me? I am new to php, currently I am trying to use $_POST['item'] to obtain a string (consists of many words) from a dropdown box and then process it. However, when I use $_POST['item'], only first word is returned, the rest of words are missing.
I have a dropdown box, something like:
echo "<form action='process_form.php' method='post'>";
echo "Select an item<br />";
echo "<select name='item' id='item'>";
...
...
...
each item in the dropdown box is a string that has product names like:
dressmaker mannequin size 12
torso mannequin in white color
...
User will then select an item from the dropdown box. When I used $_POST['item'] to obtain this string, I get the first word "dressmaker", all the rests were missing.
How do I get the whole string?
Many thanks in advance.
I am not sure exactly what you are doing but I would do something like this. For this example I assume that the values "dressmaker mannequin size 12" will correspond to the values of the columns in the database to which I will refer to as "colA, colB, colC, and colD", and in addition I assume you have an "ID" column in your database.
Here is the code I would use to generate the select drop-down list:
//$query is the variable storing the result of the mysql_query();
//assumption is that the result-set is non-empty
echo '<select name="item" id="item">\n'; //\n - new line character
//run through the loop to generate the items inside the list
while ($result = mysql_fetch_assoc($query)) {
//note the id - it will be used to find data in the
//database after the POST is complete
//couple of temp variables (not necessary but makes code cleaner)
$database_id = $result["ID"];
$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12
//add option to the select drop-down
echo "<option value=\"$database_id\">$colA $colB $colC $colD</option>\n";
}
echo "</select>";
Now to retrieve the data from the POST. I am including the code Drewdin suggested.
//form was submitted already
//assumption is that the database connection is established
$item_id = mysql_real_escape_string(trim($_POST["item"]));
//Now get the info from the database for this id
//table is "table"
$string = "SELECT * FROM table WHERE ID = $item_id";
$query = mysql_query($string) or die("Could not complete the query: $string");
//assumption here is that the result set is non-empty
$result = mysql_fetch_assoc($query);
$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12
//now you can use the values of colA-D to compute whatever you want
Hope this helps. Using database ids is nice for security plus it makes things more manageable.
Regarding using this blog. You can post comments to the answers people post. If you want however to add something to your question, you can edit your original question and just make sure its obvious what was added.
When the issue is resolved and if any answer helped and you liked it, you can pick it as the final answer by clicking the check mark next to it.
Best of luck
without seeing your select options i think this might help you
if (isset($_POST['submit'])) {
// Grab the output of the select list
$Select_Output = mysqli_real_escape_string( $dbc, trim($_POST['item']));
//What ever else you want to do here...
}
I also would use Miki725's post to make sure your select list is setup correctly
This is how the php gets the values from the HTML select item:
<select name="item" id="item">
<option value="this is option 1">option 1</option>
<option value="this is option 2">option 2</option>
<option value="this is option 4">option 3</option>
...
</select>
Basically you would do something like:
$var = $_POST['item'];
The $var will be the whatever was entered into the "value" of the option tag. So just make sure you have the proper values entered into the value fields.
Hope that helps.

Categories