0

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();

 }
  • Is this all in the same file? Because if so, you'll need [AJAX](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php). –  Apr 02 '19 at 00:13
  • Nowhere are you including a `$brand_title` form value that you could retrieve when the form is posted. I also don't see it anywhere in your insert statement. Normally you would store the ID of something only and not the actual title. – Dave Apr 02 '19 at 00:30
  • I have to store both title and id should i define title separately ? – MaryBeauty2020 Apr 02 '19 at 00:32
  • Please note, that if you are storing id and title in the table, it means that you are following any Database Normalisation. – KiranD Apr 02 '19 at 00:41

1 Answers1

0

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.

KiranD
  • 433
  • 2
  • 7
  • 20
  • now brand id and brand title both are not inserted? – MaryBeauty2020 Apr 02 '19 at 00:17
  • so what will be the solution to define title separately? – MaryBeauty2020 Apr 02 '19 at 00:19
  • What's the datatype for "brand_id" column in the database? It shouldn't be int or float.. It should be varchar ideally as it is a string. – KiranD Apr 02 '19 at 00:20
  • ohh ok mine is int I will change and check – MaryBeauty2020 Apr 02 '19 at 00:22
  • now brand title is saved in the place of brand id and brand title is empty – MaryBeauty2020 Apr 02 '19 at 00:23
  • Query (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')) just inserts only one of them as I see.. you need to change the db structure so that it can store both id and title, then change the query to store both id and title. Please read my updated answer for other things you need to change. – KiranD Apr 02 '19 at 00:32
  • please use explode instead of split and it should fix the error. – KiranD Apr 02 '19 at 00:38