html向下钻取下拉选定值未插入MYSQL
Posted
技术标签:
【中文标题】html向下钻取下拉选定值未插入MYSQL【英文标题】:html drill down drop down selected value does not inserted in MYSQL 【发布时间】:2018-07-07 18:33:38 【问题描述】:我有两个下拉菜单。首先从数据库下拉填充。根据第一个下拉列表的选定值从数据库填充第二个下拉列表。
$(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>
插入.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);
获取状态.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: ./');
?>
我已经尝试了网上给出的几乎所有解决方案。但不明白我的数据是不是插入mysql数据库。 How to insert HTML select value as text in MySQL via PHP PHP Drop down list selected value not inserted in the database
【问题讨论】:
【参考方案1】:您需要正确连接它们,而且您不要将,
放在查询的最后一列之后
跟随变化
$sql1="INSERT INTO table1 (Country, State)
VALUES ('$c', '$s',)";
到
$sql1="INSERT INTO table1 (country, state)
VALUES ('".$c."', '".$s."')";
【讨论】:
我也试过了,但是这个数据没有插入 @Sarah_Salar 如果出现任何错误,请告诉我? 不要给出错误。只是不要将值插入表中。提交表格后为空白。没有插入记录 为什么没有人帮助这个论坛 @Sarah_Salar 已经回答了,请您检查一下表格列【参考方案2】:如果想在第二个下拉列表中添加选项,您需要使用append
而不是html
并且您使用相同的ID 即#c
以将响应写入ajax 成功,将其更改为第二个下拉ID 即@ 987654325@
你可以试试这个:
$(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>");
);
);
然后在插入查询中删除,
。
$sql1="INSERT INTO table1 (Country, State)
VALUES ('$c', '$s')";
【讨论】:
请看这个问题***.com/questions/49043561/…以上是关于html向下钻取下拉选定值未插入MYSQL的主要内容,如果未能解决你的问题,请参考以下文章