gpt4 book ai didi

PHP 更新从数据库中选择标签

转载 作者:行者123 更新时间:2023-11-29 16:14:52 25 4
gpt4 key购买 nike

我有 3 <select>下拉菜单。上第一<select>我有2,4,6作为选项。上二<select>我有3,6,9,第三个<select>我有4,8,12。用户只需选择其中之一即可。例如,如果她在第一个 <select> 上选择 2那么她就不能选择另一个<select>艾莫尔。然后,当单击“保存”按钮时,它将保存在数据库中。

问题是当我尝试更新数据(例如选择另一个选项并保存)时,没有任何变化。下面是我的代码。

这就是我从数据库中调用数据的方式。

<?php
$con=mysqli_connect("server", "user", "pass", "db");

$edit=$_GET['id'];
$select="SELECT * FROM table WHERE id='$edit'";

$run=mysqli_query($con,$select);
$row=mysqli_fetch_array($run);

$id=$row['id'];
$num=$row['num'];

?>

<html>
<body>
<form action="edit.php?id=<?php echo $id; ?>" method="post">

<select type="text" name="num">
<option disabled selected>Twos</option>
<option value="two">2</option>
<option value="four">4</option>
<option value="six">6</option>
</select>

<select type="text" name="num">
<option disabled selected>Threes</option>
<option value="three">3</option>
<option value="six">6</option>
<option value="nine">9</option>
</select>

<select type="text" name="num">
<option disabled selected>Fours</option>
<option value="four">4</option>
<option value="eight">8</option>
<option value="twelve">12</option>
</select>

<label>Number: </label>
<select type="text" name="num">
<option><?php echo $num; ?></option> //this is where I want to display the data from the db
</select>

<div>
<input type="submit" value="Update" name="update">
</div>
</form>
</body>
</html>

这是我的 php 代码。

<?php
if (isset($_POST['update'])){

$up_id=$_GET['id'];
if(isset($_POST['num'])){
$num_u=$_POST['num'];

$update="UPDATE table SET num='$num_u' WHERE id='$up_id'";

$run_u=mysqli_query($con,$update);
if ($run_u){
echo "<script>alert('Record Updated Successfully')</script>";
echo "<script>window.open('view.php', '_self')</script>";
}else {
echo "<script>alert('Record Update Failed!!')</script>";
}
} else {
echo "<script>alert('Select Atleast one num!!')</script>";
}
}
?>

我知道有关 SQL Injection 的问题完成这里之后我会解决这个问题。我仍在学习中,非常感谢你们的帮助。我希望你们能帮助我。非常感谢。

片段

<html>

<body>
<form action="edit.php?id=<?php echo $id; ?>" method="post">
<label>First Choices: </label>
<select type="text" name="num">
<option disabled selected>Twos</option>
<option value="two">2</option>
<option value="four">4</option>
<option value="six">6</option>
</select> <br>

<label>Second Choices: </label>
<select type="text" name="num">
<option disabled selected>Threes</option>
<option value="three">3</option>
<option value="six">6</option>
<option value="nine">9</option>
</select> <br>

<label>First Choices: </label>
<select type="text" name="num">
<option disabled selected>Fours</option>
<option value="four">4</option>
<option value="eight">8</option>
<option value="twelve">12</option>
</select> <br><br>

<label>Number: </label>
<select type="text" name="num">
<option>Data from DB here
</option> //this is where I want to display the data from the db
</select> <br><br>

<div>
<input type="submit" value="Update" name="update">
</div>
</form>
</body>
</html>

最佳答案

您可以使用 javascript 来修复它,我在您的代码中发现了一个错误,即您没有使用 form 标记。 我做了一个示例,代码如下:

<?php
$num=2;
?>
<form action="your_backend_file_name.php" method="POST">
<select type="text" name="num">
<option disabled selected>Twos</option>
<option value="two">2</option>
<option value="four">4</option>
<option value="six">6</option>
</select>

<select type="text" name="num">
<option disabled selected>Threes</option>
<option value="three">3</option>
<option value="six">6</option>
<option value="nine">9</option>
</select>

<select type="text" name="num">
<option disabled selected>Fours</option>
<option value="four">4</option>
<option value="eight">8</option>
<option value="twelve">12</option>
</select>

<label>Number: </label>
<select type="text" name="num">
<option disabled selected>Data from db</option>
<option value="dbData"><?= $num ?></option> //this is where I want to display the data from the db
</select>

<div>
<input type="submit" value="Update" name="update">
</div>
</form>

并添加下面给出的 JavaScript 代码:

<script>
var radios = [].slice.call(document.querySelectorAll('select[name=num]'));

radios.forEach(function(radio) {
radio.addEventListener('change', function() {
radios.forEach(function(r) {
r.disabled = r !== radio;
});
});
});
</script>

然后我打印了来自前端的数据到名为 your_backend_file_name.php 的后端文件。 your_backend_file_name.php ,该文件包含以下代码:

<?php
if(!empty($_POST)){
print_r($_POST);
}
?>

检查此片段:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form action="your_backend_file_name.php" method="POST">
<select type="text" name="num">
<option disabled selected>Twos</option>
<option value="two">2</option>
<option value="four">4</option>
<option value="six">6</option>
</select>

<select type="text" name="num">
<option disabled selected>Threes</option>
<option value="three">3</option>
<option value="six">6</option>
<option value="nine">9</option>
</select>

<select type="text" name="num">
<option disabled selected>Fours</option>
<option value="four">4</option>
<option value="eight">8</option>
<option value="twelve">12</option>
</select>

<label>Number: </label>
<select type="text" name="num">
<option disabled selected>Data from db</option>
<option value="dbData">123</option> //this is where I want to display the data from the db
</select>

<div>
<input type="reset" value="Reset" name="reset" id="resetbtn">
<input type="submit" value="Update" name="update">
</div>
</form>

</body>
</html>

<script>
var radios = [].slice.call(document.querySelectorAll('select[name=num]'));

radios.forEach(function (radio) {
radio.addEventListener('change', function () {
radios.forEach(function (r) {
r.disabled = r !== radio;
});
});
});

$("#resetbtn").click(function(){
$( 'form select[name=num]' ).removeAttr( "disabled" );
});
</script>

关于PHP 更新从数据库中选择标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54919632/

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