用id捕获搜索后的行

capture the row after search with id

本文关键字:搜索 id      更新时间:2023-09-26

html 中的样本表单

<form action="search.php" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="Search" />
</form> 

当我尝试搜索时,它会显示包含所有字段的整行。这很好。但当我点击编辑时没有捕捉到这些细节。

search.php文件:

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xxx"; //server
$db = "xxx"; //database name
$user = "xxx"; //dabases user name
$pwd = "xxx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
echo '<table border=1 width=999>
<tr>
    <th valign=top>ID</th>
    <th valign=top>Name</th>
    <th valign=top>Telephone</th>
    <th valign=top>E-mail</th>
    <th valign=top>Country Visiting for</th>
    <th valign=top>Visa Category</th>
    <th valign=top>Other Category</th>
    <th valign=top>Passport No</th>
    <th valign=top>Remarks</th>
    <th valign=top>Date(Created)</th>
    <th valign=top>Updated Remarks</th>
    <th valign=top>Updated Date</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
    <tr>
        <td>'.$row['id'].'</td>
        <td>'.$row['name'].'</td>
        <td>'.$row['Telephone'].'</td>
        <td>'.$row['E_mail'].'</td>
        <td>'.$row['country'].'</td>
        <td>'.$row['visa_categeory'].'</td>
        <td>'.$row['other_category'].'</td>
        <td>'.$row['passport_no'].'</td>
        <td>'.$row['remarks'].'</td>
        <td>'.$row['date'].'</td>
        <td>'.$row['updated_remarks'].'</td>
        <td>'.$row['updated_date'].'</td>
    </tr>';

}
echo '
</table>';
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
<div><a href="update.php?id=$id">Edit</a></div>

我的update.php文件是这样的。

<?php
$host="xxx"; // Host name 
$username="xxx"; // Mysql username 
$password="xxx"; // Mysql password 
$db_name="xxx"; // Database name 
$tbl_name="customer_details"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=intval($_GET['id']);
// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table align="center" style="margin-left:100px" width="600" border="0" cellspacing="1"     cellpadding="0">
<tr>
<td align="left"><strong>ID</strong></td><td align="left">
<input name="id" disabled type="text" id="id" value="<?php echo $rows['id']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Name</strong></td><td align="left">
<input name="name" disabled type="text" id="name" value="<?php echo $rows['name']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Telephone</strong></td><td align="left">
<input name="Telephone" disabled type="text" id="Telephone" value="<?php echo     $rows['Telephone']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Country Visiting for</strong></td><td align="left">
<input name="country" disabled type="text" id="country" value="<?php echo     $rows['country']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Visa Category</strong></td><td align="left">
<input name="visa_categeory" disabled type="text" id="visa_categeory" value="<?php     echo $rows['visa_categeory']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Other Category</strong></td><td align="left">
<input name="other_category" disabled type="text" id="other_category" value="<?php     echo $rows['other_category']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Remarks</strong></td><td>
<textarea name="remarks" id="remarks" value="<?php echo $rows['remarks']; ?>"     size="20"><?php echo $rows['remarks']; ?></textarea>
</td></tr><tr>
<td align="left"><strong>Date</strong></td><td>
<input name="date" type="text" id="date" value="<?php echo $rows['date']; ?>"     size="20">
</td></tr>
<tr>
<td align="left"><strong>Modified Remarks</strong></td><td>
<textarea name="updated_remarks" id="updated_remarks" value="<?php echo     $rows['updated_remarks']; ?>" size="20"><?php echo $rows['updated_remarks']; ?></textarea>    </td></tr>
<tr>
<td align="left"><strong>Modified Date</strong></td><td>
<input name="updated_date" type="text" id="updated_date" value="<?php echo     $rows['updated_date']; ?>" size="20">
</td></tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="left">
<input type="submit" name="Submit" value="Update">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection 
mysql_close();
?>

最后更新ac.php,如下所示。

<?php
$host="xxx"; // Host name 
$username="xxx"; // Mysql username 
$password="xxx"; // Mysql password 
$db_name="xxx"; // Database name 
$tbl_name="customer_details"; // Table name 
$id=$_POST['id'];
$name=$_POST['name'];
$remarks=$_POST['remarks'];
$date=$_POST['date'];
$updated_remarks=$_POST['updated_remarks'];
$updated_date=$_POST['updated_date'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database 
$sql="UPDATE $tbl_name SET id='$id', name='$name', remarks='$remarks', date='$date',     updated_remarks='$updated_remarks', updated_date='$updated_date' WHERE     id='$id'";
$result=mysql_query($sql);
// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
header('Location: list_records.php');
}
else {
echo "ERROR";
}
?>

现在我来解释一下。

当我尝试使用id进行搜索时,它会显示整行(所有字段)。但当我在该文件中单击"编辑"时,它不会捕获该行的详细信息(该字段),我在问如何捕获该行详细信息(所有字段)。提前感谢。

我发现你的代码出了什么问题。实际上,您并没有向update.php页面传递任何特定的ID。您需要在while循环中使用<a href="update.php?id=$id">Edit</a>,然后在链接中传递每一行的id。更新你的Update.php代码如下,我认为它会起作用:

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xxx"; //server
$db = "xxx"; //database name
$user = "xxx"; //dabases user name
$pwd = "xxx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
//$output = "";
echo '<table border=1 width=999>
<tr>
    <th valign=top>ID</th>
    <th valign=top>Name</th>
    <th valign=top>Telephone</th>
    <th valign=top>E-mail</th>
    <th valign=top>Country Visiting for</th>
    <th valign=top>Visa Category</th>
    <th valign=top>Other Category</th>
    <th valign=top>Passport No</th>
    <th valign=top>Remarks</th>
    <th valign=top>Date(Created)</th>
    <th valign=top>Updated Remarks</th>
    <th valign=top>Updated Date</th>
    <th valign=top>Action</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
    <tr>
        <td>'.$row['id'].'</td>
        <td>'.$row['name'].'</td>
        <td>'.$row['Telephone'].'</td>
        <td>'.$row['E_mail'].'</td>
        <td>'.$row['country'].'</td>
        <td>'.$row['visa_categeory'].'</td>
        <td>'.$row['other_category'].'</td>
        <td>'.$row['passport_no'].'</td>
        <td>'.$row['remarks'].'</td>
        <td>'.$row['date'].'</td>
        <td>'.$row['updated_remarks'].'</td>
        <td>'.$row['updated_date'].'</td>
        <td><a href="update.php?id='.$row['id'].'">Edit</a></td>
    </tr>';

}
echo '
</table>';
//echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
<div></div>

您面临的问题是因为您在HREF到update.php的search.php中使用的$id变量不存在。首先将获取的DB值放在该变量中。

此外,最好使用$_SERVER['HP_SELF']重用代码,而不是使用所有不同的文件。