检查数据库php中的重复项

时间:2018-05-21 12:32:55

标签: php sql-server

我想在输入新类别时检查我的数据库中是否有重复的类别...我错过了什么吗?

<?php
if (isset($_POST['create_new_category'])) {
    $newcategory = $_POST['Add_category'];
    $query1 = "SELECT * FROM JobAdCategories WHERE jacat_title ='" . $newcategory . "'" OR die(sqlsrv_errors());
    $statement1 = sqlsrv_query($connection, $query1);
    $count      = sqlsrv_num_rows($statement1);
    if ($statement1 === false) {
        echo "Error (sqlsrv_query):" . print_r(sqlsrv_errors(), true);
    }
    if ($newcategory == null) {
        echo "give category";
    } else if ($count == 0) {
        $query2     = "INSERT INTO JobAdCategories (jacat_title) VALUES ('" . $newcategory . "')";
        $statement2 = sqlsrv_query($connection, $query2);
        if ($statement2 === false) {
            echo "Error (sqlsrv_query2):" . print_r(sqlsrv_errors(), true);
        }
        echo "new category inserted";
    } else {
        echo "category exists";
    }
}

0 个答案:

没有答案