如何使用href url编辑记录以检索记录

时间:2017-11-11 10:31:22

标签: php html mysql sql

当我使用<a href点击任何记录上的“编辑”按钮时,我想从网址获取数据并允许使用$_GET编辑该记录,如何将值传递给我用于检索记录的SQL查询?

任何帮助将不胜感激。

<?php
include("../include/connect.php");
$gid = (empty($_GET['cu_id']));
if (isset($_POST['cu_id'])) {
$id = $_POST['cu_id'];
}
if (isset($_POST['cu_name'])) {
$customer = $_POST['cu_name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['phone'])){
$phone =($_POST['phone']);
}
if (isset($_POST['password'])) {
$pass =$_POST['password'];
}
if (isset($_POST['coun_id'])) {
$counter =$_POST['coun_id'];
}
if (isset($_POST['card_id'])){
$card_id =$_POST['card_id'];
}
$q=mysqli_query($conn,"SELECT * FROM customer WHERE cu_id ='".$gid."'")or 
die (mysqli_error());
$r = mysqli_fetch_assoc ($q);
if (isset($_REQUEST['edit']) AND $_REQUEST['edit'] == 'customer') {
echo "
<form action= '' method ='post' class'input_type'>
<input type='text' name='cu_name' value='".$r['cu_name']."'/><br/>
<input type='text' name='email' value='".$r['email']."'/><br/>
<input type='text' name='phone' value='".$r['phone']."'/><br/>
<input type='text' name='password' value='".$r['password']."'/><br/>
<input type='text' name='coun_id' value='".$r['coun_id']."'/><br/>
<input type='text' name='card_id' value='".$r['card_id']."'/><br/>
<input type='submit' value='save edit' id='sends'/><br/>
<input type='hidden' name='cu_id' value='".$gid."'/><br/>
<input type='hidden' name='edit' value='customer'/><br/>
</form>";
}
/* query */
$query=mysqli_query($conn,"SELECT * FROM customer order by cu_id desc")or 
die (mysqli_error());
echo "<center>";
echo"<table width='40%' border='1'cellpadding='5' id='ttb' >
<tr>
<th>cu id </th>
<th>cu name</th> 
<th>email </th>
<th>phone </th>
<th> coun id</th>
<th> card id</th>
<th> update</th>
<th>delete</th>
</tr>";
while($row = mysqli_fetch_assoc($query)){
echo "
<tr>
<td>".$row['cu_id']."</td>
<td>".$row['cu_name']."</td>
<td>".$row['email']." </td>
<td>".$row['phone']." </td>
<td> ".$row['coun_id']."</td>
<td> ".$row['card_id']."</td>
<td>
<a href='cucontrol.php?edit&id=".$row['cu_id']."'>edit</a></td>
</td>
<td><a href='cucontrol.php?delete=user&id=".$row['cu_id']."'>delete</a></td>
</tr>";
}
echo "</table>";
echo "</center>";
echo "</div>";
mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:0)

cucontrol.php 上的

使用$_GET['cu_id']从网址获取ID,并将其传递给您的SQL查询以检索记录where cu_id = $id_from_url。您必须回显表单中的记录以允许用户更改信息并将POST更改回页面,然后您需要从帖子中获取新的详细信息并使用{{ 1}}在SQL查询中更新记录。

<强> cucontrol.php

UPDATE