简体   繁体   English

html下拉下拉选择值未在MYSQL中插入

[英]html drill down drop down selected value does not inserted in MYSQL

i have two drop downs. 我有两个下拉菜单。 first drop down populate from database. 首先从数据库中下拉列表。 second drop down populated from database based on selected value of first drop down. 根据第一个下拉列表的选定值从数据库填充第二个下拉列表。

 $(document).ready(function() { $("#c").change(function() { var c1 = $('#c :selected').text(); if(c1 != "") { $.ajax({ url:'getstatw.php', data:{c:c1}, type:'POST', success:function(response) { var resp = $.trim(response); $("#c").html(resp); } }); } else { $("#c").html("<option value=''>Select state</option>"); } }); }); 
  <form id = "world" method="post" action="insert.php"> <select name="country" id="c" style = "width:200px" class="btn btn-primary dropdown-toggle" ;> <option>country</option> <?php $sql = "select DISTINCT country from table1"; $res = mysqli_query($con, $sql); if(mysqli_num_rows($res) > 0) { while($row = mysqli_fetch_object($res)) { echo "<option value='".$row->id."'>".$row->c."</option>"; } } ?> </select> <br><br> <label for="s" >State</label> <select name="State" id="s" style = "width:200px " ; class="btn btn-primary dropdown-toggle";><option>Select state</option></select><br><br> <button id = "sub" type="submit" class="btn btn-primary" disabled>Submit</button> </form> 

insert.php insert.php

 $con=mysqli_connect("localhost","root","","world"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // escape variables for security //home tab $c = mysqli_real_escape_string($con, $_POST['country']); $s = mysqli_real_escape_string($con, $_POST['state']); //query for table_mainast $sql1="INSERT INTO table1 (Country, State) VALUES ('$c', '$s',)"; //query for table_dataast if (!mysqli_query($con,$sql1)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); 
getstate.php getstate.php

 <?php $con=mysqli_connect("localhost","root","","test"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if(isset($_POST['c'])) { $sql = "select DISTINCT `State` from `table2` where `Country`='".mysqli_real_escape_string($con, $_POST['c'])."'"; $res = mysqli_query($con, $sql); if(mysqli_num_rows($res) > 0) { echo "<option value=''>------- Select --------</option>"; while($row = mysqli_fetch_object($res)) { echo "<option value='".$row->id."'>".$row->c."</option>"; } } } else { header('location: ./'); } ?> 

i have tried almost all solution given on net. 我已经尝试了网上给出的几乎所有解决方案。 but do not understand my data is not inserted into mysql database. 但是不明白我的数据没有插入到mysql数据库中。 How to insert HTML select value as text in MySQL via PHP PHP Drop down list selected value not inserted in the database 如何通过PHP在MySQL中将HTML选择值作为文本插入PHP PHP下拉列表中未插入数据库的选择值

You need to concantenate them properly and also you don't you put , after last column in the query 您需要正确concantenate他们,你也不要你放,最后一栏后,在查询

Change following 更改以下

$sql1="INSERT INTO table1 (Country, State)
VALUES ('$c', '$s',)";

to

$sql1="INSERT INTO table1 (country, state)
VALUES ('".$c."', '".$s."')";

if want to add options to the second drop down you need to use append not html and your using same ID ie #c to write the response in ajax success, change it to second drop down ID ie #s 如果要在第二个下拉列表中添加选项,则需要使用append not html和您使用相同的ID(即#c以成功在Ajax中写入响应,将其更改为第二个下拉ID,即#s

you can try this: 您可以尝试以下方法:

$(document).ready(function() {
  $("#c").change(function() {
    var c1 = $('#c :selected').text();
    if(c1 != "") {
      $.ajax({
        url:'getstatw.php',
        data:{c:c1},
        type:'POST',
        success:function(response) {
          var resp = $.trim(response);
          $("#s").append(resp);
        }
      });
    } else {
      $("#c").append("<option selected value=''>Select state</option>");
    }
  });
});

then remove , in insert query. 然后取出,在插入查询。

$sql1="INSERT INTO table1 (Country, State)
VALUES ('$c', '$s')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM