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

时间:2019-02-28 06:25:32

标签: php mysql

我有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的问题,在此完成后,我将解决此问题。我仍在学习,非常感谢你们的帮助。我希望你们能帮助我。非常感谢。

SNIPPET

<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>

1 个答案:

答案 0 :(得分:0)

您可以使用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>