-3

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

4

2 回答 2

1

我发现你的代码有什么问题。实际上,您没有将任何特定 ID 传递给您的 update.php 页面。您需要<a href="update.php?id=$id">Edit</a>在 while 循环中执行此部分,然后在链接中传递每一行的 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>
于 2013-07-09T06:33:06.810 回答
0

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

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

于 2013-07-09T06:46:47.253 回答