我正在尝试使用查询值。
请看这段代码:
//Anything goes here
........
$show="SELECT *FROM persons";
$result=mysqli_query($con,$show);
echo "<form method='post'>";
echo "<table border=1>";
echo "<tr>";
echo "<td>name</td><td>Firstname</td><td>Lastname</td><td>address</td><td>phone</td>";
echo "</tr>";
while ($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='mname' value=".$row[0]."></td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td><input type='submit' name='single' value='edit'></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
?>
<?
if (isset($_POST['single']))
{
$show="SELECT *FROM persons WHERE name='$_POST[mname]'";
//another sql query here
............
.............
但是,这不是正确的编码。
我只想要用户点击编辑,然后它将为每个查询执行特定命令。但是在这里,循环之后它只会得到最后一行的$row[0]
。
如何执行此操作:当用户单击每行旁边的edit
按钮时,它将只获得此行的值
答案 0 :(得分:1)
试试我的代码。 page.php是您当前的页面
........
$show="SELECT *FROM persons";
$result=mysqli_query($con,$show);
echo "<form method='post'>";
echo "<table border=1>";
echo "<tr>";
echo "<td>name</td><td>Firstname</td><td>Lastname</td><td>address</td><td>phone</td>";
echo "</tr>";
while ($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='mname' value=".$row[0]."></td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td><a href='page.php?single=".$row[0]."'>edit</a></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
?>
<?
if (isset($_GET['single']))
{
$show="SELECT *FROM persons WHERE name='$_GET[single]'";
答案 1 :(得分:0)
如果出现以下其他情况,请保留您的代码:
// if form submitted then show edit form with selected person data
if (isset($_POST['single'])) {
$show = "SELECT *FROM persons WHERE name='$_POST[mname]'";
//another sql query here
............
.............
} else {
$show = "SELECT *FROM persons";
$result = mysqli_query($con, $show);
echo "<table border=1>";
echo "<tr>";
echo "<td>name</td><td>Firstname</td><td>Lastname</td><td>address</td><td>phone</td>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<form method='post'>";
echo "<tr>";
echo "<td><input type='text' name='mname' value=" . $row[0] . "> <input type='hidden' name='id' value=" . $row[1] . "></td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td><input type='submit' name='single' value='edit'></td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
?>
}
还在表单中使用隐藏字段并在该字段中保留记录ID,并在查询中使用它,因为它将精确查询并提高性能。 另一个有用的事情是你应该在每一行都有一个新的表单标签,比如我在while循环中使用了form标签,使用这个代码只会将一个人的id,name发送到你的编辑页面。在您的情况下,它将提交整个数据。
答案 2 :(得分:0)
使用此功能。
//Anything goes here
........
$show="SELECT * FROM persons";
$result=mysqli_query($con,$show);
echo "<form method='post'>";
echo "<table border=1>";
echo "<tr>";
echo "<td>name</td><td>Firstname</td><td>Lastname</td><td>address</td><td>phone</td>";
echo "</tr>";
while ($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='mname' value='".$row[0]."'></td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td><input type='submit' name='single' value='edit'></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
?>
<?
if (isset($_POST['single']))
{
$show="SELECT * FROM persons WHERE name='".$_POST[mname]."'";
//another sql query here
............
.............