I am making an insert mobile data page where for brands i had made dropdown menu the dropdown menu is working perfectly but when I insert mobile data brand id is inserted in database but brand title is not inserted why?
// code for dropdown menu
<tr>
<td style="color:#fff"><b>Mobile Brand</b></td>
<td>
<select name="Mbrand">
<option>Select Brand</option>
<?php
$get_brands = "select * from brands";
$run_brands = mysqli_query($con, $get_brands);
while ($row_brands=mysqli_fetch_array($run_brands))
{
$brand_id = $row_brands['brand_id'];
$brand_title = $row_brands['brand_title'];
echo "<option value='$brand_id'>'$brand_title'</option>";
}
?>
</select>
</td>
</tr>
----------------------------
// code for defining data
<?php
if(isset($_POST['insert_mobile'])){
//text data variables
$mobile_title=$_POST['Mname'];
$mobile_price=$_POST['Mprice'];
$mobile_brand=$_POST['Mbrand'];
$mobile_desc=$_POST['mdesc'];
$mobile_keywords = $_POST['key'];
-----------------------------
//code for inserting data
$insert_mobile = "insert into mobile_phone (mobile_name,brand_id,date,price,img1,img2,img3,mobile_desc,product_keywords) values ('$mobile_title','$mobile_brand',NOW(),'$mobile_price','$mobile_img1','$mobile_img2','$mobile_img3','$mobile_desc','$mobile_keywords')";
$run_mobile= mysqli_query($con,$insert_mobile);
if($run_mobile)
{
echo "<script>alert('mobile added successfully')</script>";
exit();
}
Please try using $brand_title instead of $brand_id in the option value.
echo "<option value='$brand_id'>'$brand_title'</option>";
will become
$brand = $brand_id.':'.$brand_title;
echo "<option value='$brand'>'$brand_title'</option>";
Edit
Option value is actually the one that gets passed in $_POST[] values and that can be stored in database as you need.
Edit
Replace the below code
$mobile_brand=$_POST['Mbrand'];
with
$brand_id = explode(':', $_POST['Mbrand'])[0];
$brand_title = explode(':', $_POST['Mbrand'])[1];
And please change the Insert query accordingly, so that it can accomodate or store both brand_id and brand_title in respective columns. Please check the datatypes of those columns before you run the script.
Related
//This is my php to view dropdown box <?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name=country value=''>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
echo "</select>";
?>
But when i am storing in DB my country_id is always 0.
You are not fetching country id from SQL Query
Change
$query="SELECT cname FROM country";
To
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
Therefore $r[0] is getting blank value.
Use this code for your reference
//This is my php to view dropdown box
<?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT countryid,cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name='country'>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[countryid]'> $r[cname] </option>";
}
echo "</select>";
?>
I am going with Pupil's answer to change the query to
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
and then change this
echo "<select name=country value=''>";
to
echo "<select name=country>"; //remove value=''
EDIT:
Please check your column type may be it is "INT" when you try to store text it stores 0.
I have a MySQL database and am trying make a conditional dropdown menu ("Subcategory") show values based on the values in the first dropdown, ("Category").
This is a reference data table, so the parent ID of the subcategory should match the ref_data_id of the category.
The conditional list relies on the value of the first dropdown box, and I have tried $_POST and $_GET to try to get the value from the first object to use in my MySQLi query but neither seems to work.
Can anyone help?
<?php
// connect to the database
include("connectdb.php");
?>
<html>
<!--First Dropdown Menu - CATEGORIES-->
<div class="label">Select Category:</div>
<select name ="Category_HTML">
<option value = "">---Select---</option>
<?php
$stmt = "SELECT * FROM `ref_data` WHERE Parent_ID IS NULL ;";
$result = mysqli_query($mysqli, $stmt);
while ( $row=mysqli_fetch_array($result)) {
$description = $row['Description'];
$refdataID = $row['Ref_Data_ID'];
echo "<option value='$refdataID'> $description </option>";
}
?>
</select>
<!--Second Dropdown Menu - Subcategory-->
<div class="label">Select Subategory:</div>
<select name="subcategory_HTML">
<option value = "">---Select---</option>
<?php
$idvalue = $_POST['Category_HTML'];
$stmt = "SELECT * FROM `ref_data` WHERE Parent_ID = $idvalue;";
$result = mysqli_query($mysqli, $stmt);
while ( $row=mysqli_fetch_array($result)) {
$description = $row['Description'];
$refdataID = $row['Ref_Data_ID'];
echo "<option value='$refdataID'> $description </option>";
}
?>
</select>
</html>
try to make an AJAX call when a value is select in the category list i.e
<select name ="Category_HTML" onchange="AJAX_CALL()">
and populate the result in the
<select name="subcategory_HTML">
I have 2 tables one 'brand' that stores all the brand names and 'brand_category' that stores all the brand names with their corresponding categories. I have a multiple select box that loads all the items in the 'brand' table when the page loads, but I need to highlight the items that are already submitted by the user previously in the 'brand_category' table with the rest of the brands that are left so that the user knows what are the brands left to be entered in the 'brand_category' table. Below is the code but it doesn't work. Please help to resolve this issue
<div class="selectbox">
<label id="brand" class="brand_label">Brand:</lable>
<?php
echo "<select name='brand' class='cat_brands' multiple>"
<option value='0'>None</option>";
$query = mysql_query("SELECT id, name FROM brand");
while($br_query = mysql_fetch_assoc($query)){
$query_select = mysql_query("SELECT * FROM brand_category WHERE brand_id='".$br_query['id']."'");
while($brnd_select = mysql_fetch_assoc($query_select)){
if($brnd_select['brand_id']==$br_query['id']){
echo "<option style='background-color: red' value='".$br_query['id']."'>".$br_query['name']."</option>";
}
}
}
?>
</div>
The above code only displays the brands entered in the 'brand_category' table rest of the brands are not displayed. I need to display them also.
You forgot to mention else condition inside while loop.Here is the code
<div class="selectbox">
<label id="brand" class="brand_label">Brand:</lable>
<?php
echo "<select name='brand' class='cat_brands' multiple>"
<option value='0'>None</option>";
$query = mysql_query("SELECT id, name FROM brand");
while($br_query = mysql_fetch_assoc($query)){
$query_select = mysql_query("SELECT * FROM brand_category WHERE brand_id='".$br_query['id']."'");
while($brnd_select = mysql_fetch_assoc($query_select)){
if($brnd_select['brand_id']==$br_query['id']){
echo "<option style='background-color: red' value='".$br_query['id']."'>".$br_query['name']."</option>";
}else{
echo "<option value='".$br_query['id']."'>".$br_query['name']."</option>";
}
}
}
?>
</div>
If I have understood your query correctly you are trying to pull out all the brands and to make the ones in the brands_category table to display as selected.
You should better be using MySQL PDO extensions rather than this obsolete method
There are better ways to implement this, e.g. by having just one table or by using a Join query, anyway... this should work.
<select name='brand' class='cat_brands' multiple>
<option value='0'>None</option>
<?php
$query = mysql_query("SELECT id, name FROM brand");
while($br_query = mysql_fetch_assoc($query))
{
$query_select = mysql_query("SELECT * FROM brand_category WHERE brand_id='".$br_query['id']."'");
if(mysql_num_rows($query_select) > 0)
{
while($brnd_select = mysql_fetch_assoc($query_select))
{
echo "<option style='background-color: red' value='".$br_query['id']."' selected='selected'>".$br_query['name']."</option>";
}
}
else
{
echo "<option style='background-color: yellow' value='".$br_query['id']."'>".$br_query['name']."</option>";
}
} ?>
</select>
i am new here but i have a problem in inserting the id and the value of the checkboxes into my database here is the code of the form:
<?php
include('db.php');
$sql = "select * from sheet1 order by course_level asc";
$r = mysqli_query($dbc,$sql) or die(mysqli_error($dbc));
$co = '';
while($row = mysqli_fetch_array($r)) {
$co .= '<tr><td>'.$row['course_level'].'</td><td><input name="courses[]"
type= "checkbox" value = "'.$row['course_code'].'">'.$row['course_code'].'
</td> <td>'.$row['course_title'].'</td><td>'.$row['course_lecturer'].'
</td><input type=hidden name=cid[] value="'.$row['cid'].'">
</tr>';
}
?>
And this is the action code:
<?php
include('db.php');
if(isset($_POST['courses']))
echo 'lie';
else
echo 'true';
foreach($_POST['courses'] as $row=>$id){
$courses=$id;
$cid = $_POST['cid'][$row];
$sql = "insert into selected_courses values ('','$courses','$cid')";
$r = mysqli_query($dbc,$sql);
}
if($r)
echo 'done';
?>
thanks a lot.
You have several problems here, the main one being you are attempting to store two different reference values to the same row (course_code and cid) in your selected_courses table. You should really only store the primary key (cid?).
I'd suggest dropping the course_code column from your selected_courses table, remove the hidden input and structure your checkbox like this
<input type="checkbox"
name="courses[]"
value="<?php echo htmlspecialchars($row['cid']) ?>">
Then your INSERT query simply becomes
// Forget mysqli, move to PDO
$stmt = $dbc->prepare('INSERT INTO selected_courses (cid) VALUES (?)');
$stmt->bindParam(1, $cid);
foreach ($_POST['courses'] as $cid) {
$stmt->execute();
}
I want to send the name and id field through the option tag. Right now as it is set up it is only sending id, how can send name as well to insert it in the data base?
<select name="category_id" size="1"><br />';
$sql = "SELECT id, name
FROM categories
ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n ";
}
echo'
</select>
Make the option value have this format: name$id
<select name="category_id" size="1"><br />';
$sql = "SELECT id, name
FROM categories
ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['name']."$".$row['id']."\">".$row['name']."</option>\n ";
}
echo'
</select>
then when you retrieve the data you can explode it as so
$option = explode("$", $_POST['category_id']);
echo $option[0]; // Name
echo $option[1]; // Id
If I understand you correctly, you want a hidden <input> field and some javascript that writes the <option>'s text.
However, I can't think of a single application where that is desirable... The point of database normalization is to avoid duplication.