gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:48:37 25 4
gpt4 key购买 nike

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

?>

最佳答案

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

关于php - 如何从数据库中填充的选择菜单中将数据发布到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48771059/

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