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

时间:2021-08-02 03:46:47

My php form here i made a drop down menu populated from a database...

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

Here i tried to post the selected options from the drop down menu, but the results were just showing the ID number of the database table so i tried to make a function that would echo the Name of the product for that specific ID, but it didn't work...

在这里,我试图从下拉菜单中发布所选的选项,但结果只是显示数据库表的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 个解决方案

#1


1  

In the select_cpu function, you should return the string instead of echoing it:

在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;
}

#2


0  

Your first code snippet was almost right, but if you viewed the source you'd notice the <option> code was wrong. This is because you're mistaking the double quotes shown in HTML with what PHP sees. To output a double quote in a PHP echo, either escape it ($something = "a double quote: \"") or wrap it in single quotes instead ($something = 'a double quote: "').

您的第一个代码段几乎是正确的,但如果您查看了源代码,您会发现

Try changing this line:

尝试更改此行:

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

to this:

对此:

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

#1


1  

In the select_cpu function, you should return the string instead of echoing it:

在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;
}

#2


0  

Your first code snippet was almost right, but if you viewed the source you'd notice the <option> code was wrong. This is because you're mistaking the double quotes shown in HTML with what PHP sees. To output a double quote in a PHP echo, either escape it ($something = "a double quote: \"") or wrap it in single quotes instead ($something = 'a double quote: "').

您的第一个代码段几乎是正确的,但如果您查看了源代码,您会发现

Try changing this line:

尝试更改此行:

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

to this:

对此:

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