我正在创建一个管理部分,以便能够更新用户信息。我有一个通过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');
}
?>
我将进入edituser.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,显示我如何获得已登录的CURRENT用户信息。
<?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>';
}
}
}
}
?>
答案 0 :(得分:0)
您可以添加输入文本字段以显示您从数据库中检索的数据。
而不是$ row [&#39; rowname&#39;];
你可以添加
<input type="text" name="product_<?php echo $product_id;?>" value="<?php echo $row['rowname'];?>">
并使用post方法发送到服务器。
答案 1 :(得分:-1)
将您的修改链接行更改为
<td class="tdproduct"><a href='editusers.php?id'='<?php echo $row['id'];?>'>EDIT</a></td>
在.php之后丢失问号会导致语法错误。当你到达editusers.php页面时,你可以使用$ _GET(“id”)访问id;