-3

我正在尝试使用查询值。

请看这段代码:

//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每一行旁边的按钮时,它只会得到这一行的值

4

3 回答 3

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]'";
于 2013-04-16T05:15:37.800 回答
0

将您的代码保持在 if else 条件中,如下所示:

// 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 循环中使用了表单标签一样,使用这个你的代码只会将一个人的 id,name 传递给你的编辑页面。在您的情况下,它将提交整个数据。

于 2013-04-16T05:26:47.227 回答
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
   ............
   .............
于 2013-04-16T05:38:02.843 回答