php - 我如何检查我的 MySQL 表以确保我没有输入相同的内容两次?

标签 php mysql duplicates

现在我正在尝试创建一个 PHP/MySQL 设置,将名称添加到数据库中。如果它已经存在,我不希望它将名称添加到数据库中。现在它正在将名称添加到数据库中。

我为此使用了三个文件。

首先是 Names.php 文件:

<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>

<body>
    <div id="main">
        <h1>About</h1>

        <form action="Insert.php" method="post">
            <p>What is your full name?</p><input type="text" name="names"><br>

            <input type="submit">
        </form>

        <?php include("Footer.php");?>
    </div> 
</body>
</html>

接下来是 Insert.php 文件:

<?php 
$con = mysql_connect("localhost","a7068104_user2","wiseguy1345");
if(!$con) {
    die("could not connect to localhost:" .mysql_error());
} 

mysql_select_db("a7068104_world") or die("Cannot connect to database");

$result = mysql_query("SELECT * FROM names_1 ORDER BY names");
if(!$result) {
    die('Invalid SELECT query:' .mysql_error());
}

$sql="INSERT INTO names_1 (names) VALUES ('$_POST[names]')";

if (!mysql_query($sql,$con)) {
    die('Error: ' . mysql_error());
}

header("refresh:1.5; url=NamesAction.php");

mysql_close($con)
?>

<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>

<body>
    <div id="main">
        <h1>Names</h1>
        <p>You will be redirected back to the <b>Names</b> page in a moment.</p>
        <?php include("Footer.php");?>
    </div>
</body>
</html>

最后是 NamesAction.php 文件:

<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>

<body>
    <div id="main">
    <h1>Names</h1>

    <?php 
        $con = mysql_connect("localhost","a7068104_user2","wiseguy1345");
        if(!$con) {
            die("could not connect to localhost:" .mysql_error());
        } 

        mysql_select_db("a7068104_world") or die("Cannot connect to database");
    ?>

    <?php
        mysql_query("LOCK TABLES names_1 WRITE;");

        $result = mysql_query("SELECT * FROM names_1 ORDER BY names DESC LIMIT 100000");
        if(!$result) {
            die('Invalid SELECT query:' .mysql_error());
        }

        echo "<table border='2'>
        <tr>
        <th>Name</th>
        </tr>";

        while($row = mysql_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['names'] . "</td>";
            echo "</tr>";
        }

        echo "</table>";

        mysql_query("UNLOCK TABLES;");

        mysql_close($con);
    ?>

    <?php include("Footer.php");?>
    </div>
</body>
</html>

请帮忙,因为我不知道如何确保我不会将某些内容输入数据库两次!我想做的是检查数据库以确保它尚未添加,如果已添加,则已向用户发送消息并添加它,如果未添加,则已发送消息说它正在添加到数据库并将其添加到数据库中。

谢谢, 莱昂纳多

最佳答案

首先不要使用mysql_*系列函数。但我将使用它们编写以下代码,因为您可能更习惯于使用它们。但说真的,不要使用它们。而是使用 mysqliPDO .

 $name = mysql_real_escape_string($_POST['names']);
 $query = "SELECT * FROM names_1 WHERE names='$name'";
 $result = mysql_query($query);
 if(mysql_num_rows($result) > 0 ){
       //Already in Db
 }
 else{
      $query = "INSERT INTO names_1 (names) VALUES('$name')";
      $result = mysql_query($query);
      if($result){
          //successful
      }
      else{
          //Unsuccessful
      }
 }

或者只是让数据库引擎为您处理任务,方法是向其他答案建议的唯一列添加唯一约束。

关于php - 我如何检查我的 MySQL 表以确保我没有输入相同的内容两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033771/

相关文章:

php - 限制函数参数以允许特定值

来自文件路径的 PHP CURL PUT

php - 使用不带 foreach 的数组更新 mysql

php-从 php 插入后未触发 MySQL 触发器

PHP-如何对具有重复元素值的数组元素求和

mysql - 显示重复值子查询mysql

php - 计算有多少笔付款是通过 cashondelivery、paypal、payu 等方式进行的?

MYSQL 工作台 - 数据库文件的位置 - win 8

php - 从数据库插入选择框列

c# - 从非常大的文本文件中删除重复的字符串