通过按钮一次更新一行MYSQL表

时间:2016-02-19 14:01:59

标签: php html mysql

我目前在表格的每一行旁边都有一个删除和更新按钮。删除按钮的工作原理是从表中删除该行,但更新按钮仅适用于表中的最后一行。如果我在此之前尝试更改一行中的字段并按更新 - 它将更新为最后一行的值...如何解决此问题? (这样每行都可以单独更新)。

while($row = mysql_fetch_assoc($result)) 
        {


                echo 
                "
                    <tr>
                        <td style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['topic_id'] . "</td>
                        <td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_name' value='" . $row['topic_name'] . "'></td>
                        <td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_address' value='" . $row['topic_address'] . "'></td>
                        <td><button type='submit' name='updateTopic' value='".$row['topic_id']."'>Update</button></td>
                        <td><button type='submit' name='deleteTopic' value='".$row['topic_id']."'>Delete</button></td>
                    </tr>

                ";



            }

            if(isset($_POST['updateTopic'])){
                $updateID = $_POST['updateTopic'];
                $updateTopicName = $_POST['up_topic_name'];
                $updateTopicAddress = $_POST['up_topic_address'];
                $updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
                echo "<meta http-equiv='refresh' content='0'>";
            }


            if(isset($_POST['deleteTopic'])){
                $deleteID = $_POST['deleteTopic'];
                $deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
                $deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
                echo "<meta http-equiv='refresh' content='0'>";
            }

        echo "</form></table>";

3 个答案:

答案 0 :(得分:1)

两个按钮都是单独的,因此它们将始终位于POST数组

通过$_GET请求执行编辑和删除的更快捷方法,并将您的链接设为

 http://localhost/project_name/file_name?id=".$row['topic_id']."&operation=delete

用于编辑:

 http://localhost/project_name/file_name?id=".$row['topic_id']."&operation=edit

在服务器端,添加此检查

if(isset($_GET['operation']) && trim($_GET['operation'])=="edit")){
 // Edit code
}
else if(isset($_GET['operation']) && trim($_GET['operation'])=="delete")){
   // Delete code
}

答案 1 :(得分:0)

HTML结构不正确(tr&gt;形式&gt; td很糟糕^^)但这样做应该更好。对于最佳结构,您应该使用div而不是table。 这个想法是1 action = 1 form:

<?php
if(isset($_POST['updateTopic'])){
    $updateID = $_POST['updateTopic'];
    $updateTopicName = $_POST['up_topic_name'];
    $updateTopicAddress = $_POST['up_topic_address'];
    $updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
    echo "<meta http-equiv='refresh' content='0'>";
}


if(isset($_POST['deleteTopic'])){
    $deleteID = $_POST['deleteTopic'];
    $deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
    $deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
    echo "<meta http-equiv='refresh' content='0'>";
}

echo "<table>";
while($row = mysql_fetch_assoc($result)) 
{
    echo "<tr>
            <form method='POST' action=''>
                <input type='hidden'  name='updateTopic' value='".$row['topic_id']."'>
                <td style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['topic_id'] . "</td>
                <td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_name' value='" . $row['topic_name'] . "'></td>
                <td style='font-weight: normal;background-color: #ff9999;' class='row-amount'><input type='text' name='up_topic_address' value='" . $row['topic_address'] . "'></td>
                <td><button type='submit'>Update</button></td>
            </form>
            <form method='POST' action=''>
                <input type='hidden' name='deleteTopic' value=".$row['topic_id'].">
                <td><button type='submit'>Delete</button></td>
            </form>
        </tr>";
}
echo "</table>";

注意:我从fetch row中删除了你的更新/删除操作,因为你只需要执行一次。例如,使用您的代码,您可以在每个循环中删除。

答案 2 :(得分:0)

问题是因为所有“name”和“adress”字段都具有相同的输入名称......

当你尝试读取$ updateTopicName = $ _POST ['up_topic_name']时,你想要什么up_topic_name?第一排的那个?第二排的一个?在提交时,名为up_topic_name的所有输入都将被最后一个值覆盖。

要解决此问题,您必须(仅)命名输入up_topic_name和up_topic_address,如:

<input type='text' name='up_topic_name[{$row['topic_id']}]' value='" . $row['topic_name'] . "'>

然后在读取值时,执行:

$updateID = $_POST['updateTopic'];
$updateTopicName = $_POST['up_topic_name'][$updateID];