首页 > 解决方案 > 使用从 html 下拉列表中选择的 SQL 数据

问题描述

新手警报我已经阅读了几个小时,但看不到如何做我想做的事。我有一个带有药物和剂量等列表的 SQL 数据库。我试图创建一个“编辑”页面,以便我可以从下拉列表中选择药物(这似乎有效),并编辑它的某些部分(大小、框数量、剂量等

问题是我无法让选定的药物项目(从下拉菜单)成为一个变量,或者如果有必要,转发到第二个 PHP 页面。我在那里留下了一些尝试编辑,但它们导致错误(TRIED NOT WORK)事实上,它正在加载editmed.php以在提交时加载我的编辑页面。(不确定这是否是最好的方法)。

任何帮助都会非常感谢

        <!DOCTYPE html>
        <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Editor</title>
    <link rel="stylesheet" href="global.css">
     </head>
    <body>

    <div class="header">
    <h1>Edit Medication Item</h1>
    </div>

    <div class="topnav">
    <a href="indexhl.php">Home</a>
    <a class="active" href="edit.php">Edit Medication</a>
    <a href="presc_coll.php">Prescription Collection</a>
    <a href="add-new.php">Add New Medication</a>
    <a href="pillbox.php">Pillbox refill</a>
    <a href="individualtopup.php">Individual refill</a>
    </div>

    <?php
    // server connect
    $mysqli = new mysqli("localhost:port", "USERNAME", "PASSWORD", "prescription") 
    or die ('Cannot connect to db');

    // select medication name field from db
    echo "<p><b><U><i><p style='color:red'>Select Medication</I></U></p><p style='color:black'></b></p>";
    $sqlSelect="select Medication from general";
    $result = $mysqli -> query ($sqlSelect);
    //$medselect=$result['Medication'];**TRIED NOT WORK** 

    echo "<form action='editmed.php'>";
    echo "<select id=`Medication` name=`medselect`>";  

    while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['Medication'] . "'>" . $row['Medication'] . "</option>";
    }
    //$medselect=$result['Medication'];**TRIED NOT WORK**


    echo "</select>";
    echo "<input type='submit' value='submit'>";
    // echo "<input type='submit' value='<$medselect>' name='medselect'/>" ; **TRIED NOT WORK**

    echo "</form>" ; 
    echo "</body>";
    echo "</html>";

    ?> 

    </body>

    </html>

编辑过的.php >

    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Editor</title>
    <link rel="stylesheet" href="global.css">
    </head>
    <body>


    <div class="header">

    <h1>Edit Medication Item</h1>
    </div>

    <div class="topnav">
    <a href="indexhl.php">Home</a>
    <a class="active" href="edit.php">Edit Medication</a>
    <a href="presc_coll.php">Prescription Collection</a>
    <a href="add-new.php">Add New Medication</a>
    <a href="pillbox.php">Pillbox refill</a>
    <a href="individualtopup.php">Individual refill</a>
    </div>


    <?php

    // server connect
    $link = mysqli_connect("localhost:port", "user", "password", 
    "prescription") 
    or die ('Cannot connect to db');

    // $sql = "SELECT ID, Medication, dosagedaily, Box_Size, Boxed_remain, 
    grandtotal, last_collected, Dosage, countdown FROM general where 
    `Medication` = $medselect";

    $sql = "SELECT ID, Medication, dosagedaily, Box_Size, Boxed_remain, 
    grandtotal, last_collected, Dosage, countdown FROM general where 
    `Medication` = 'Esomeprazole 40mg'";
 
 
    // you check sql and link true
    if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
            echo "<table>";
            echo "<tr>";
            echo "<th>Index</th>";
            echo "<th>Medication</th>";
            echo "<th>Daily Dosage</th>";
            echo "<th>Box Size</th>" ;
            echo "<th>Boxed Remaining</th>" ;
            echo "<th>Grand Total</th>";
            echo "<th>Last presc collection</th>";
            echo "<th>Dosage</th>";
            echo "<th>Pills Remaining</th>";
            echo "</tr>";
            
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
            echo "<td>" . $row['ID'] . "</td>";
            echo "<td>" . $row['Medication'] . "</td>";
            echo "<td>" . $row['dosagedaily'] . "</td>";
            echo "<td>" . $row['Box_Size'] . "</td>";
            echo "<td>" . $row['Boxed_remain'] . "</td>";
            echo "<td>" . $row['grandtotal'] . "</td>";
            echo "<td>" . $row['last_collected'] . "</td>";
            echo "<td>" . $row['Dosage'] . "</td>";
            echo "<td>" . $row['countdown'] . "</td>";
            
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
    } 
    else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
 
    // Close connection
    mysqli_close($link);

我不知道如何从输入表单中分配变量,这就是我的问题所在。如果我分配了一个变量,那么我可以创建实际的编辑部分。

标签: phphtmlsql

解决方案


推荐阅读