gpt4 book ai didi

javascript - html 钻取下拉所选值未插入 MYSQL

转载 作者:行者123 更新时间:2023-11-29 17:59:42 26 4
gpt4 key购买 nike

我有两个下拉菜单。首先从数据库下拉填充。根据第一个下拉列表的选定值从数据库填充第二个下拉列表。

$(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);
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: ./');
}


?>

我几乎尝试了网上给出的所有解决方案。但不明白我的数据没有插入到mysql数据库中。 How to insert HTML select value as text in MySQL via PHP PHP Drop down list selected value not inserted in the database

最佳答案

您需要正确连接它们,而且您不会将 , 放在查询的最后一列之后

更改以下

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

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

关于javascript - html 钻取下拉所选值未插入 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com