Inserting into table from dropdown using data from another table - php

i have a problem, I managed to list the option values for my dropdown from cms.php
$query = 'SELECT * FROM categories';
$result = mysql_query($query) or die(mysql_error());
echo "<select name='categories'>";
echo "<option value =''>Select Category</option>";
while ($row = mysql_fetch_array($result)){
$categories = $row['categories'];
echo "<option value='$categories'>$categories</option>";
}
echo "</select>";
It works, the dropdown menu lists all the data from the "categories" table.
HOwever i can't insert those values from the dropdown into another table.
$categories = isset($_POST['categories']);
echo $categories;
$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="INSERT INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','".mysql_real_escape_string($_POST['prod_name'])."','{$categories}','$_POST[prod_price]',
'".mysql_real_escape_string($_POST['prod_desc'])."','$_POST[prod_quantity]','{$image}')";
this just outputs 1, from the echo $categories; it also stores 1 into the database. what did i do wrong? Help please.

As requested.
Remove the isset and () from $categories = isset($_POST['categories']);
change it to $categories = $_POST['categories'];

Related

Filtering database base on a drop-down list that populating from MySQL

I would like to filter some data from MySQL database base on a drop-down list that populating from the MySQL database.
Now I have got a drop-down list like this:
which is populating from the MySQL database:
The following are my code for the drop-down list:
<?php
$result2 = $conn->query("select id, field from customer_details GROUP BY field");
echo "<select id='sf' name='id'>";
echo '<option value="">Show All</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $field);
$id = $row['id'];
$field = $row['field'];
echo '<option value = "'.$id.'">'.$field.'</option>';
}
echo "</select>";
?>
Does anyone know how can I get the selected text of the drop-down menu?
I have tried to use $_POST[] in different ways like $_POST['id']
<?php
$result2 = $conn->query("select id, field from customer_details GROUP BY field");
echo "<select id='sf' name='id'>";
echo '<option value="">Show All</option>';
while ($row = $result2->fetch_assoc()) {
unset($id, $field);
$id = $row['id'];
$field = $row['field'];
echo '<option value = "'.$id.'">'.$field.'</option>';
}
echo "</select>";
echo $_POST['id'];
?>
then Notice pop up from the website:
Notice: Undefined index: id
What should I do?

Inserting dynamic information into the database (dynamic drop-down menu)

I have this code, that gets the categories from the database and displays them on a dropdown menu. Now, what I want to do, Is when something is selected and the button isset, get the ID of that category (table: categories ,row category_id) then submit it into (table: posts ,row category_id). I don't know how to get the ID of the category that was selected in the dropdown. Thanks.
$db variable is the database connection.
This is what I've got so far. This is the submission of the data to the database:
include('../includes/db_connect.php');
if(isset($_POST['submit'])){
$newTitle = $_POST['newTitle'];
$newPost = $_POST['newPost'];
$newCategory = $_POST['newCategory'];
$my_date = date("Y-m-d H:i:s");
if(!empty($newPost))
if(!empty($newTitle)){
$sql="INSERT INTO posts (title, body, category_id)
VALUES('$newTitle', '$newPost', '$newCategory')";
$query = $db->query($sql);
if($query){
echo "Post entered to database";
}else{
echo "Error Submitting the data";
}
}
}
This is the select:
<textarea name="newPost" cols="176" rows="25"/></textarea><br>
<select name=""="newCategory" id="newCategory">
<option value="0">Select category</option>
<?php
$result = mysqli_query($db, "SELECT * FROM categories");
if ( $result ) {
while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
echo '<option value="' . $row['category'] . '">' . $row['category'] . '</option>';
}
} else {
echo 'Couldn\'t fetch Categories from MySQL.';
}
?>
Ps - I know this is easily injectable, you don't need to tell me, Just answer the question please :)
You mostly likely just need to change this line, and have the value be the category primary key like id or whatever it is called.
echo "<option value='{$row['id']}'>{$row['category']}</option>";
Otherwise after submit you would have to run a mysql query to find the id by searching where category='$newCategory'

Why is my drop down list not populating with the table data?

WHy is my drop down list not populating with the table data? (dropdown box is empty)
And what is used to display data upon selection of an item in that drop down - is it a "VIEW" (please do provide a study link so I can learn)
My Code
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
else
{
echo "Connected to mySQL</br>";
}
//$query = 'SELECT FirstName FROM persons';
//$result = mysqli_query($con,$query);
$query = mysqli_query($con,"SELECT 'FirstName' FROM persons");
//print_r($query);
//echo '<select name="FirstName">';
echo "<select name= 'FirstName'>";
//while($row=mysqli_fetch_array($result))
while($row=mysqli_fetch_array($query))
{
echo $row;
//echo "<option value='".$row['FirstName']."'>".'</option>';
}
echo '</select>';
?>
You had 2 errors:
I pointed the first in the comment: to print an option you must use this code:
echo "<option value='". $row['FirstName']."'>".$row['FirstName']
. '</option>';
The second is in your SQL: you are not selecting the FirstName field from the database, but a string 'FirstName' instead. That's why it is printed twice as you said. Use this SQL to get the field:
$query = mysqli_query($con,"SELECT FirstName FROM persons");
Also usually people put an id of the record and not a field, that may have possible duplicates into the value of an <option>. So, I would have used:
echo "<option value='". $row['id']."'>".$row['FirstName']
. '</option>';
selecting the id from the database together with first name.
Try this:
echo "<option value='".$row['FirstName']."'>".$row['FirstName']."</option>";
Also seems that you are having an issue with the database query. Swap your while loop with the following and see if it works
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row['FirstName']."'>".$row['FirstName']."</option>";
}
$result->free();
}

Make an SQL query a drop down menu

I have two SQL queries that display different results from my database. I'm using these results as navigation links in my nav bar.
At the moment all the results display as a single line of text, I want to make each SQL query display as a drop down menu, with all the results from the query as an option in the drop down.
Here's the code I'm using:
<?php
$q = "SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
//display product categories
}
mysqli_free_result($result); //free result
$q = "SELECT brand_id, brand_name FROM Brand";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
echo "<a href='brand.php?id=$row[0]'>$row[1]</a> ";
//display product Brands
}
?>
use select option
echo "<select name='category'>";
while ($row = mysqli_fetch_row($result)){
echo "<option value='$row[0]'>$row[1]</option> ";
}
echo "</select>";
$q="SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'],$q);
$option1.="<select name='category'>";
while ($row = mysqli_fetch_row($result)){
$option1.="<option value='$row[0]'>$row[1]</option> ";
}
$option1.="</select>";
same for second
print this $option1 value in your view file
<select onchange="document.location.href='category.php?id='+this.value;">
<?php
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)):?>
<option value="<?=$row[0];?>"><?=$row[1];?></option>
<?php endwhile;?>
</select>

Keeping lastly selected value from a dropdownlist after button click

Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>

Categories