关键'categoryname_UNIQUE'重复输入'食物'

时间:2017-01-20 10:24:56

标签: javascript php mysql database primary-key

每当我尝试插入/添加具有相同名称的类别名称时,即使我没有在我的代码中添加任何验证,我也会收到此错误。这是我的代码如下。我需要删除此错误,而是将自己的验证设置为“不允许相同的数据”。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Survey Settings</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet">



  </head>
<body>

    <br/>
    <hr/>
    <?php
    require_once("/dao/CategoryDAO.php");
    require_once("/dao/TopicDAO.php");

    $category = new CategoryDAO();
    $topic = new TopicDAO();
    $allCategories_arr = $category->getAllCategories();
    $allTopics_arr = $topic->getAllTopicTitles();

    ?>
    <div class="container">
        <div class="row">
            <form method="POST">
                <?php
                if(isset($_POST['submit'])){
                    include 'testdb.php';

                    $addcategory = $_POST['categoryname'];

                    $query = mysqli_query($con, "SELECT categoryname FROM category WHERE categoryname = '.$addcategory.'");

                        $insert = mysqli_query($con, "INSERT INTO category (`categoryname`) VALUES ('$addcategory')");
                        if(!$insert){
                            echo mysqli_error($con);
                        }
                        else{
                            header("Location:Settings.php");
                            echo 'Category successfully added!';
                        }

                }
                ?>
            <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                <tr>
                    <th>Category ID</th>
                    <th>Category Name</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach($allCategories_arr as $ar) {

                    echo "<tr>";
                    echo "<th>" . $ar['category_id'] . "</th>";
                    echo "<th>" . $ar['categoryname'] . "</th>";
                    echo "<th><a class='btn btn-default' href='viewsubcategory.php?catid=" . $ar['category_id'] . "' >More Info</a><a class='btn btn-info' href='EditCategory.php?category_id=".$ar['category_id']."'>Edit</a></th>";
                    echo "</tr>";
                }
                ?>
               </tbody>
            </table>
                <div class="col-sm-2">
                    <input type="text" name="categoryname" class="form-control input-sm" required />
                </div>

                <input type="submit" name="submit" value="Add Category" class="btn btn-danger" />
        </div>
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#example').DataTable();
        } );
    </script>


</body>
    </html>

1 个答案:

答案 0 :(得分:0)

您必须从数据库中删除categoryname字段的UNIQUE键集