Php 从html表内部集成DB info以将单行信息输出到另一个页面
我正在创建一个管理部分,以便能够更新用户信息。我有一个通过while循环构造的表,该表显示了我调用的所有用户信息。然后,我添加了一个编辑选项,并创建了一个customeredit.php页面,我将在其中创建输入字段,我可以在其中键入更改并提交 我的问题是,我如何编程我的代码,当单击单行中的编辑按钮时,它会识别该行ID并将其与记录关联 我没有为customeredit.php文件附加任何代码,因为它只有基本格式。我是否需要在customeredit.php文件中插入任何内容来实现此功能Php 从html表内部集成DB info以将单行信息输出到另一个页面,php,html,mysql,Php,Html,Mysql,我正在创建一个管理部分,以便能够更新用户信息。我有一个通过while循环构造的表,该表显示了我调用的所有用户信息。然后,我添加了一个编辑选项,并创建了一个customeredit.php页面,我将在其中创建输入字段,我可以在其中键入更改并提交 我的问题是,我如何编程我的代码,当单击单行中的编辑按钮时,它会识别该行ID并将其与记录关联 我没有为customeredit.php文件附加任何代码,因为它只有基本格式。我是否需要在customeredit.php文件中插入任何内容来实现此功能 <?
<?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>
edituser.php中的相关代码
<?php
require_once 'core/init.php';
$user = new User();
if(Session::exists('home')) {
echo '<p>' . Session::flash('home') . '</p>';
}
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
?>
<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="email" 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'];
?>
我将在edituser.php中输入的一些输入字段
<?php
require_once 'core/init.php';
$user = new User();
if(Session::exists('home')) {
echo '<p>' . Session::flash('home') . '</p>';
}
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
?>
<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="email" 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'];
?>
显示如何获取当前登录用户信息的PHP
<?php
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'firstname' => array(
'required' => true,
'min' => 2,
'max' => 50
),
'lastname' => array (
'required' => true,
'min' => 2,
'max' => 50
),
'email' => array (
'required' => true,
'min' => 2,
'max' => 50
),
'username' => array (
'required' => true,
'min' => 2,
'max' => 50
)
));
if($validation->passed()) {
try{
$user->update(array(
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'email' => Input::get('email'),
'username' => Input::get('username')
));
Session::flash('home', 'Your details have been updated successfully.');
Redirect::to('edituser.php');
} catch(Exception $e) {
die($e->getMessage());
}
} else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
}
?>
您可以添加输入文本字段以显示从数据库检索的数据 而不是$row['rowname'] 你可以加上
<input type="text" name="product_<?php echo $product_id;?>" value="<?php echo $row['rowname'];?>">
并使用post方法发送到服务器。将编辑链接行更改为
在.php之后缺少问号将导致语法错误。当您进入editusers.php页面时,您可以使用$\u GETid 所以我要做的就是进入customeredit.php文件并键入echo代码?这适用于我点击编辑的任何唱片吗?理想情况下,我希望能够识别所选id,而不仅仅是从该表输出信息。我将把输入字段,从数据库内回声信息。然后允许更新信息。这不是在做什么吗?我是否需要在GET语句中添加任何内容?它只需添加$_GETid;,即可识别所有内容?您将使用它从数据库中获取特定用户,如select*from users where ID=