【问题标题】:How to Edit records using href url to retrieve the record如何使用 href url 编辑记录以检索记录
【发布时间】:2018-04-24 12:24:50
【问题描述】:

当我使用<a href 在任何记录上单击编辑按钮时,我想从 url 获取数据并允许编辑该记录,使用 $_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);
?>

【问题讨论】:

  • 互联网上有谣言说缩进代码使其可读。你听说过那个谣言吗?
  • 如果您要请一群专业人士将他们的宝贵时间花在您遇到的问题上,那么不要只是转储您不可读的代码并将相同的文本复制粘贴 5 次。请正确格式化您的代码并阅读How to Ask。之后,我们也许可以帮助您。
  • 我投了反对票,并投票结束,因为您的问题似乎根本没有任何努力。您的问题文本重复了 3 次,但即便如此我也不知道您的问题是什么。请访问帮助中心并阅读how to ask,以及如何创建minimal, complete, and verifiable example

标签: php html mysql sql


【解决方案1】:

在您的 cucontrol.php 上使用 $_GET['cu_id'] 从 URL 中获取 id,并将其传递给您的 SQL 查询以检索记录 where cu_id = $id_from_url。您必须在表单中回显记录以允许用户更改信息并将POSTit 返回页面,然后您需要从帖子中获取新的详细信息并在您的 SQL 中使用UPDATE查询以更新记录。

cucontrol.php

if(isset($_GET['cu_id'])){

$id = $_GET['cu_id'];

 $query = mysqli_query($conn,"SELECT * FROM customer WHERE cu_id = '$id'") or 
 die (mysqli_error($query));
 while($row = mysqli_fetch_assoc($query)){

 echo '
   <form method="POST">
   <input type="hidden" name="id" value ="'.$row['cu_id'].'"/>
   <input type="text" name="c_name" value ="'.$row['cu_name'].'"/>
   <input type="text" name="c_email" value ="'.$row['email'].'"/>
   <input type="text" name="c_phone" value ="'.$row['phone'].'"/>
   <input type="text" name="coun_id" value ="'.$row['cu_id'].'"/> //delete this line if they cant edit it
   <input type="text" name="card_id" value ="'.$row['cu_id'].'"/>//delete this line if they cant edit it
   <input type="submit" name="submit" value ="Send"/>
   </form>';

  }


   if(isset($_POST['submit'])){
 $id = $_POST['id'];
 $c_name = $_POST['c_name'];
 $c_email = $_POST['c_email'];
 $c_phone = $_POST['c_phone'];

 $query = mysqli_query($conn,"UPDATE customer SET cu_name = '$c_name',
 email = '$c_email', phone = '$c_phone' WHERE cu_id = '$id'") or 
 die (mysqli_error($query));

 if ($query){

  echo '<script>alert("Update Successful!");</script>';

        }

     }

【讨论】:

  • 我犯了一个小错误,将UPDATE customers 更改为UPDATE customer 你的表名是客户而不是客户
猜你喜欢
  • 2014-03-28
  • 2014-10-23
  • 2019-11-13
  • 2013-06-18
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多