php - 如何在重复的列值上更新 mySQL 键

标签 php mysql html database

我正在编写一个将发布到 mySQL 数据库的 php 应用程序。我有那部分工作,但我有一个名为 repID 的列,它将包含客户端修复 ID。

将内容添加到数据库的方式是通过我网站上的管理员。我想这样做,如果技术人员输入现有的 repID,它只会更新 key 。而不是制作具有不同修复状态的副本

PHP

$repID = mysql_real_escape_string($_POST['repID']);
$clientName = mysql_real_escape_string($_POST['clientName']);
$devModel = mysql_real_escape_string($_POST['devModel']);
$repStatus = mysql_real_escape_string($_POST['repStatus']);
$tracking = mysql_real_escape_string($_POST['tracking']);
$sql = mysql_query("INSERT INTO status (`repID`, `clientName`, `devModel`, `repStatus`,     `tracking`) VALUES ('$repID','$clientName','$devModel','$repStatus', '$tracking');");

输入页面

<?php
include_once('../resources/init.php');
$query = "SELECT * FROM status WHERE repStatus != 'Finished';";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
$random = rand(1000000000, 9999999999);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>
        <title>Add A Repair</title>
    </head>
    <body>
        <div id="wrapper">
            <div id="application">
                <div id="randomNum">
                    <?php echo $random; ?>
                </div>
                <form method="post" action="insert.php">
                    <div id="repID">
                        <label for="repID">Repair ID</label>
                        <input type="text" name="repID" />
                    </div>
                    <div id="clientName">
                        <label for="clientName">Client Name</label>
                        <input type="text" name="clientName" />
                    </div>
                    <div id="devModel">
                        <label for="devModel">Device Model</label>
                        <input type="text" name="devModel" />
                    </div>
                    <div id="repStatus">
                        <label for="repStatus">Repair Status</label>
                        <select name="repStatus">
                            <option value="Diagnosis Stage">Diagnosis Stage</option>
                            <option value="Problem Found">Problem Found</option>
                            <option value="Possible Solution">Possible Solution</option>
                            <option value="Parts Ordered">Parts Ordered</option>
                            <option value="Parts Recieved">Parts Recieved</option>
                            <option value="Parts/Software Installation Stage">Parts/Software       m  Installation Stage</option>
                            <option value="Testing Stage">Testing Stage</option>
                            <option value="Finished">Finished</option>
                        </select>
                    </div>
                    <div id="tracking">
                        <label for="tracking">Tracking Number</label>
                        <input type="text" name="tracking" />
                    </div>
                    <div id="submit">
                        <input type="submit" value="Submit" />    
                    </div>
                </form>
                <div id="currentClients">
                    Current Clients
                    <br /><br />
                    <table border="0" cellspacing="2" cellpadding="2">
                        <tr>    
                            <th>Repair ID</th>
                            <th>Client Name</th>
                            <th>Device Model</th>
                            <th>Repair Status</th>
                            <th>Tracking</th>
                        </tr>
                        <?php
                        $i = 0;
                        while ($i < $num) {
                            $v1 = mysql_result($result, $i, "repID");
                            $v2 = mysql_result($result, $i, "clientName");
                            $v3 = mysql_result($result, $i, "devModel");
                            $v4 = mysql_result($result, $i, "repStatus");
                            $v5 = mysql_result($result, $i, "tracking");
                            ?>
                            <tr>
                                <td><?php echo $v1; ?></td>
                                <td><?php echo $v2; ?></td>
                                <td><?php echo $v3; ?></td>
                                <td><?php echo $v4; ?></td>
                                <td><?php echo $v5; ?></td>
                            </tr>
                            <?php
                            $i++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

最佳答案

使用 MySQL 的 INSERT ... ON DUPLICATE KEY UPDATE .当然要确保 repID 是主键或唯一键。

关于php - 如何在重复的列值上更新 mySQL 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462928/

相关文章:

PHP 内部联接使用 PDO 验证多个用户(如果已存在)

php - 网页与索引页略有不同

MySQL:如何选择和处理 "n to n"相关数据以提高性能

html - 悬停时布局更改

html - 形成多个步骤。一个或多个网页

php - 更新新表加入另外两个表?

javascript - 创建 chrome 扩展以将数据发送到本地主机服务器

Mysql存储一个数字而不是值集

php - 如何使用 Twitter 直接消息流作为输入?

css - 我怎样才能选择这个元素?