如何在重复列值上更新mySQL密钥

时间:2011-09-18 16:49:11

标签: php mysql html database

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

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

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');");

INPUT PAGE

<?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>

1 个答案:

答案 0 :(得分:3)

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