gpt4 book ai didi

php - 选择通过 while 循环生成的单个输出记录,反射(reflect)数据库中表中的信息

转载 作者:行者123 更新时间:2023-11-29 22:32:38 25 4
gpt4 key购买 nike

我正在尝试添加编辑从数据库输出的数据的功能。它通过 while 循环输出到我创建的表中。我将在 while 循环中添加一个按钮/链接,该按钮/链接将显示在每个记录的末尾,当您单击该按钮/链接时,它将提供编辑该记录中的信息的选项。然后提交,新信息将被保存并显示。

如何使此功能适用于输出数据中的单个记录?如果我在 while 循环内添加链接/按钮,它会识别出它是针对该特定记录的吗?我会将按钮放在 echo $row['description'] 之后循环。

如果可能的话,我将如何捕获该单个记录以显示在我将按钮链接到的页面中?

<table class="tableproduct">
<tr>
<th class="thproduct">Product ID</th>
<th class="thproduct">Product Name</th>
<th class="thproduct">Price</th>
<th class="thproduct">Category</th>
<th class="thproduct">Description</th>
</tr>


<?php
while($row = mysqli_fetch_assoc($q)) : ?>

<tr>
<td class="tdproduct"><?php echo $row['product_id']; ?> </td>
<td class="tdproduct"><?php echo $row['name']; ?> </td>
<td class="tdproduct"><?php echo $row['price']; ?> </td>
<td class="tdproduct"><?php echo $row['category']; ?> </td>
<td class="tdproduct"><?php echo $row['description']; ?> </td>
</tr>
</table>
<?php endwhile; ?>

新代码

管理用户




 <?php
$con = mysqli_connect("localhost","root","","bfb");
$q = mysqli_query($con,"SELECT * FROM users");
?>
<table class="tableproduct">
<tr>
<th class="thproduct">ID</th>
<th class="thproduct">First Name</th>
<th class="thproduct">Last Name</th>
<th class="thproduct">Email</th>
<th class="thproduct">Username</th>
<th class="thproduct">Group</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($q)) : ?>

<tr>
<td class="tdproduct"><?php echo $row['id']; ?> </td>
<td class="tdproduct"><?php echo $row['firstname']; ?> </td>
<td class="tdproduct"><?php echo $row['lastname']; ?> </td>
<td class="tdproduct"><?php echo $row['email']; ?> </td>
<td class="tdproduct"><?php echo $row['username']; ?> </td>
<td class="tdproduct"><?php echo $row['group']; ?> </td>
<td class="tdproduct"><a href='editusers.phpid'='<?php echo $row['id']; ?>'>EDIT</a></td>
</tr>

<?php endwhile; ?>
</table>

编辑用户.php

    <div class="whitepage">
<div class="content">
<form action="" method="post">
<div class="field">
<label for="firstname">First name</label>
<input type="text" name="firstname" class="inputbar" value="<?php echo escape($user->data()->firstname); ?>" required>
</div>
<div class="field">
<label for="lastname">Last name</label>
<input type="text" class="inputbar" name="lastname" value="<?php echo escape($user->data()->lastname); ?>" required>
</div>
<div class="field">
<label for="email">Email</label>
<input type="email" class="inputbaremail" name="email" value="<?php echo escape($user->data()->email); ?>" required>
</div>
<div class="field">
<label for="username">Username</label>
<input type="text" class="inputbar" name="username" value="<?php echo escape($user->data()->username); ?>" required>
</div>

<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<label for="signinButton">
<input type="submit" id="signinButton" value="Update">
</label>
</form>

<?php
$_GET['id'];
?>
</div>
</div>

最佳答案

您可以从这里开始:

while($row = mysqli_fetch_assoc($q)) : ?>
<tr>
<td class="tdproduct"><?php echo $row['product_id']; ?> </td>
<td class="tdproduct"><?php echo $row['name']; ?> </td>
<td class="tdproduct"><?php echo $row['price']; ?> </td>
<td class="tdproduct"><?php echo $row['category']; ?> </td>
<td class="tdproduct"><?php echo $row['description']; ?> </td>
<td class="tdproduct"><a href='edit.php?product_id='<?php echo $row['product_id']; ?>'>EDIT</a></td>
</tr>
<?php endwhile; ?>
</table>

所以你需要创建另一个 edit.php文件并读取 product_id来自$_GET('product_id')那里。

别忘了动起来</table>在循环之外:-)

编辑 1 在 edit.php 中,您将更改查询:

if($row = mysqli_fetch_assoc($q)) { ?>
<tr>
<form action='save.php'>
<td class="tdproduct"><input name="product_id" value="<?php echo $row['product_id']; ?>" /> </td>
<td class="tdproduct"><input name="product_name" value="<?php echo $row['name']; ?>" /> </td>
<td class="tdproduct"><input name="price" value="<?php echo $row['price']; ?>" /> </td>
<td class="tdproduct"><input name="category" value="<?php echo $row['category']; ?>" /> </td>
<td class="tdproduct"><input name="description" value="<?php echo $row['description']; ?>" /> </td>
<td class="tdproduct"><input type="submit" value="SAVE" /></td>
</form>
</tr>
<?php }; ?>

关于php - 选择通过 while 循环生成的单个输出记录,反射(reflect)数据库中表中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29680453/

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