使用id搜索后捕获行

时间:2013-07-09 06:13:28

标签: php javascript jquery mysql

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();
?>

最后是update_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进行搜索时,它会显示整行(所有字段)。 但是,当我单击该文件中的编辑时,它不捕获该行详细信息(该字段)我问如何捕获该行详细信息(所有字段)。 在此先感谢。

2 个答案:

答案 0 :(得分:1)

我发现你的代码有什么问题。实际上,您没有将任何特定ID传递给update.php页面。您需要在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>

答案 1 :(得分:0)

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

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