gpt4 book ai didi

php - 将数据从 MySQL 数据库加载到 HTML 文本框

转载 作者:行者123 更新时间:2023-11-30 22:17:07 25 4
gpt4 key购买 nike

我还在学习,谁能帮帮我,我的代码有什么问题吗?我需要在您单击“加载”按钮时加载,程序将搜索在下拉列表中选择的数据库 ID,它们会带来名称 .. 等并将其显示在文本框中。对不起,我的英语。

<?php

$servername = "localhost";
$username = "estgv15592";
$password = "estgv155922016";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


if(isset($_POST["loadbtn"]))
{

$id = (integer) $_POST["id"];

$query = "SELECT NOME, MORADA, PRECO FROM FICHA_DE_OBRA WHERE ID_FICHAOBRA = '$id' ";
$result = mysqli_query($conn, $query);
$details = mysql_fetch_array($result);

$nome = $details["NOME"];
$morada = $details["MORADA"];
$preco = $details["PRECO"];
}

$sql = "SELECT * FROM FICHA_DE_OBRA";

$result = mysqli_query($conn, $sql);

echo '<form id="form" method="post">';
echo "<select name ='id'>";
echo "<option value=''>Selecione Número ficha Obra</option>";

while($row = mysqli_fetch_array($result))

{
echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
}
echo "</select>";

$conn->close();
?>


<input type="submit" value="Load" name="loadbtn">
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="upName" style="text-align:right" value="<?php echo $nome;?>"/></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="upCost" style="text-align:right" value="<?php echo $morada;?>" /></td>
</tr>
<tr>
<td>Active</td>
<td><input type="text" name="upActive" style="text-align:right" value="<?php echo $preco;?>" /></td>
</tr>
</table>
</div>
<br/>

</form>

最佳答案

您没有使用正确的 php 标签:(例如 <?php echo $preco;?>):

<tr>
<td>Name</td>
<td><input type="text" name="upName" style="text-align:right" value="<?php echo $nome; ?>"/></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="upCost" style="text-align:right" value="<?php echo $morada; ?>" /></td>
</tr>
<tr>
<td>Active</td>
<td><input type="text" name="upActive" style="text-align:right" value="<?php echo $preco; ?>" /></td>
</tr>

使用 mysqli_query mysqli_fetch_array 函数并注意 mysqli_query 中的第一个参数应该是你出错的连接对象:

$result = mysqli_query($conn, $query);    // first PHP block
$result = mysqli_query($conn, $sql); // second PHP block

$details = mysqli_fetch_array($result); // first PHP block
$row = mysqli_fetch_array($result) // second PHP block

然后将下面的行移动到第一个 PHP block 的顶部,或 $conn在您的第一个 PHP block 中将是未定义的:

$servername = "localhost";
$username = "estgv15592";
$password = "your_password";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

关于php - 将数据从 MySQL 数据库加载到 HTML 文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37910489/

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