如何将数据从数据库中填充的选择菜单发布到表中?

时间:2018-02-13 16:13:00

标签: php html

我的php表单在这里我制作了一个从数据库中填充的下拉菜单......

<?php 
include("mysql_connect.php") 
?>

<html>
<head>
<link rel="stylesheet" href="form-style.css">
</head>
<body>

<form action="form_result.php" method="post">

    <select name="cpu">
        <?php 
            $result = $conn->query("SELECT * FROM cpus");
            while ($row = $result->fetch_assoc()) {
                echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
            }
        ?>
    </select>

这里我尝试从下拉菜单中发布所选选项,但结果只是显示数据库表的ID号,所以我试图创建一个函数来回显该特定ID的产品名称,但它不起作用......

<?php
include("mysql_connect.php");

function select_cpu() {
    $result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
    while ($row = $result->fetch_assoc()) {
        echo "{$row['CpuManufacturer']} {$row['CpuName']}";
    }
}

if(isset($_POST['submit'])) {
    $cpu = $_POST['cpu'];

        echo "<table>";
        echo "<tr><th>You have selected:</th><tr>";
            echo "
            <tr><td hidden>".$cpu."</td><td>".select_cpu()."</td></tr>
            ";
        }
        echo "</table>";

?>

2 个答案:

答案 0 :(得分:1)

select_cpu函数中,您应该返回字符串而不是回显它:

function select_cpu() {
    $result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
    $str = "";
    while ($row = $result->fetch_assoc()) {
        $str .= "{$row['CpuManufacturer']} {$row['CpuName']}";
    }
    return $str;
}

答案 1 :(得分:0)

您的第一个代码段几乎是正确的,但如果您查看了源代码,则会注意到. ├── android │ ├── android.iml │ ├── AndroidManifest.xml │ ├── assets │ │ ├── icon.png │ │ ├── img │ │ │ ├── 10.png │ │ │ ├── 11.png │ │ │ ├── 1.png │ │ │ ├── 2.png │ │ │ ├── 3.png │ │ │ ├── 4.png │ │ │ ├── 5.png │ │ │ ├── 6.png │ │ │ ├── 7.png │ │ │ ├── 8.png │ │ │ ├── 9.png │ │ │ └── line.png │ │ └── words.yml │ ├── build.gradle │ ├── ic_launcher-web.png │ ├── libs │ │ ├── armeabi │ │ │ ├── libgdx-bullet.so │ │ │ └── libgdx.so │ │ ├── armeabi-v7a │ │ │ ├── libgdx-bullet.so │ │ │ └── libgdx.so │ │ └── x86 │ │ ├── libgdx-bullet.so │ │ └── libgdx.so │ ├── proguard-project.txt │ ├── project.properties │ ├── res │ │ ├── drawable-hdpi │ │ │ └── ic_launcher.png │ │ ├── drawable-mdpi │ │ │ └── ic_launcher.png │ │ ├── drawable-xhdpi │ │ │ └── ic_launcher.png │ │ ├── drawable-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── values │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── values-hu │ │ └── strings.xml │ └── src │ └── com │ └── infosoftcorp │ └── wordpress │ └── ish │ └── android │ └── AndroidLauncher.java ├── asd.html ├── bitbucket-pipelines.yml ├── build │ ├── android-profile │ │ ├── profile-2018-02-12-00-57-01-965.rawproto │ │ ├── profile-2018-02-12-00-58-00-422.rawproto │ │ ├── profile-2018-02-12-00-59-18-263.rawproto │ │ ├── profile-2018-02-12-00-59-55-075.rawproto │ │ ├── profile-2018-02-12-01-00-22-650.rawproto │ │ └── profile-2018-02-12-01-03-02-363.rawproto │ ├── generated │ │ ├── mockable-android-26.jar │ │ └── mockable-android-27.jar │ └── intermediates │ ├── lint-cache │ │ ├── api-versions-6-27.0.1.bin │ │ ├── typos-en.txt-2.bin │ │ └── typos-hu.txt-2.bin │ └── proguard-files │ ├── proguard-android-optimize.txt-2.3.0 │ └── proguard-android.txt-2.3.0 ├── build.gradle ├── core │ ├── build │ │ ├── classes │ │ │ └── main │ │ │ └── com │ │ │ └── infosoftcorp │ │ │ └── wordpress │ │ │ └── ish │ │ │ ├── FileHandler$$anonfun$1.class │ │ │ ├── FileHandler.class │ │ │ ├── FileHandler$.class │ │ │ ├── InfoSoftHangman$$anonfun$create$1.class │ │ │ ├── InfoSoftHangman$$anonfun$drawLines$1$1.class │ │ │ ├── InfoSoftHangman.class │ │ │ ├── InfoSoftHangman$InputHandler.class │ │ │ ├── Parsing$$anonfun$parseString$1.class │ │ │ ├── Parsing$$anonfun$removeEach$1.class │ │ │ ├── Parsing.class │ │ │ ├── Parsing$.class │ │ │ ├── Parsing$DecLives.class │ │ │ ├── Parsing$HMSignal.class │ │ │ ├── Parsing$ParseError.class │ │ │ ├── Parsing$ParsingResult.class │ │ │ ├── Save.class │ │ │ └── Save$.class │ │ ├── libs │ │ │ └── core-1.0.jar │ │ └── tmp │ │ ├── compileScala │ │ └── jar │ │ └── MANIFEST.MF │ ├── build.gradle │ ├── core.iml │ └── src │ └── com │ └── infosoftcorp │ └── wordpress │ └── ish │ ├── FileHandler.scala │ ├── InfoSoftHangman.scala │ ├── Parsing.scala │ └── Save.scala ├── desktop │ ├── build │ │ ├── classes │ │ │ └── main │ │ │ └── com │ │ │ └── infosoftcorp │ │ │ └── wordpress │ │ │ └── ish │ │ │ └── desktop │ │ │ └── DesktopLauncher.class │ │ ├── dependency-cache │ │ ├── libs │ │ │ └── desktop-1.0.jar │ │ └── tmp │ │ └── <chaos> │ ├── build.gradle │ ├── desktop.iml │ ├── out │ │ └── production │ │ └── classes │ └── src │ └── com │ └── infosoftcorp │ └── wordpress │ └── ish │ └── desktop │ └── DesktopLauncher.java ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── is-hangman.iml ├── is-hangman.ipr ├── is-hangman.iws ├── local.properties ├── README.md └── settings.gradle 代码错误。这是因为您错误地将HTML中显示的双引号误认为是PHP所看到的。要在PHP回显中输出双引号,请将其转义(<option>)或将其换成单引号($something = "a double quote: \"")。

尝试更改此行:

$something = 'a double quote: "'

到此:

echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";