停止将重复的条目添加到数据库PHP SQLITE

时间:2018-12-02 17:13:50

标签: php sqlite

我创建了一个PHP表单,该表单将添加到用PHP创建的数据库中,但是,我试图添加一个函数,该函数将阻止用户将相同的水果添加到数据库中,我将如何尝试执行此操作我已经尝试了一段时间了,谢谢。

正如您在下面看到的那样,通过将变量添加到数据库中,PHP脚本可以很好地工作,但是在实施检查以确保水果名称与我正在努力的数据库中的水果名称不匹配时,PHP脚本可以正常工作。

 <?php
    //SQLite Database test query
    $db=sqlite_open("fruitshop.db"); 

    if(isset( $_POST['fruit']) && strcmp($_POST['fruit'],"") != 0 ){ //Adds to Database

        $item = sqlite_escape_string($_POST["fruit"]);

        $number=$_POST['number'];


        sqlite_query($db,"INSERT INTO fruit (fruit) VALUES ('$item')");
        sqlite_query($db,"INSERT INTO stock (Number) VALUES ($number)");

        $query = "SELECT * from stock, fruit WHERE stock.Item = fruit.id AND fruit.fruit = '$item', 'fruit' = '{$item}'";
        $result=sqlite_query($db, $query);





        echo "<table border=1>";
        echo "<tr><th>Fruit</th><th>Qty</th>";
        echo "<h2>". "Newly added Fruit"."</h2>";
        while($row=sqlite_fetch_array($result,SQLITE_ASSOC ))
        {
            echo "<tr>";
             echo "<td>" . $row['fruit.fruit'] . "</td><td>" . $row['stock.Number'] . "</td>"; 
           echo "</tr>";
        }
        echo "</table>";
    echo "<h2>". "Show All Fruits"."</h2>";

    echo "<table border=1>\n";



    //NOte the use of SQLITE_ASSOC
    echo "</br>\n";


    $result=sqlite_query($db,"SELECT * from stock, fruit WHERE stock.Item = fruit.ID"); //Shows Databse

    echo "<th>Fruit</th><th>Qty</th>\n";

    while($row=sqlite_fetch_array($result,SQLITE_ASSOC))
    {
        echo "<tr>\n";
        echo "<td>" . $row['fruit.fruit'] . "</td>\n"; 
        echo "<td>" . $row['stock.Number'] . "</td>\n"; 
       echo "</tr>\n";
    }
    echo "</table>\n";




    }
    sqlite_close($db);


    ?>

    <html>
    <h2> Add Fruits to Database </h2>
    <form name="CheckFruit" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    Fruit
    <input type="text" name="fruit" />
    <br>
    Stock
    <input type="number" name="number" />
    <br>

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



    </html>

2 个答案:

答案 0 :(得分:0)

您可以在列模式中将水果列声明为UNIQUE,这样数据库将拒绝重复的值,而无需您进行进一步的控制。

如果相反,您想检查PHP代码中是否已经存在该值,则可以查询该值并检查是否返回了行。如果返回行,则已经存在一个值,您可以在执行插入操作之前解决该情况

$query = "SELECT * from fruit WHERE fruit.fruit = '$item', 'fruit' = '{$item}'";
$result=sqlite_query($db, $query);
if (sqlite_num_rows($result) === 0) {
    sqlite_query($db,"INSERT INTO fruit (fruit) VALUES ('$item')");
    sqlite_query($db,"INSERT INTO stock (Number) VALUES ($number)");
} else {
    // Value is already present
}

注意:我从未使用过SQLITE,所以希望语法正确

答案 1 :(得分:0)

您可以使用我的更新代码。虽然没有尝试代码,但它应该可以工作。还要注意我如何使用R version 3.5.1 (2018-07-02) Platform: x86_64-pc-linux-gnu (64-bit) Running under: CentOS Linux 7 (Core) Matrix products: default BLAS: /home/janinek/.conda/envs/R-3.5.1/lib/R/lib/libRblas.so LAPACK: /home/janinek/.conda/envs/R-3.5.1/lib/R/lib/libRlapack.so locale: [1] LC_CTYPE=C LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] repr_0.15.0 ggplot2_3.0.0 reshape2_1.4.3 loaded via a namespace (and not attached): [1] Rcpp_0.12.18 compiler_3.5.1 pillar_1.3.0 plyr_1.8.4 [5] bindr_0.1.1 base64enc_0.1-3 tools_3.5.1 digest_0.6.15 [9] uuid_0.1-2 jsonlite_1.5 evaluate_0.11 tibble_1.4.2 [13] gtable_0.2.0 pkgconfig_2.0.1 rlang_0.2.1 IRdisplay_0.5.0 [17] IRkernel_0.8.12 bindrcpp_0.2.2 withr_2.1.2 stringr_1.3.1 [21] dplyr_0.7.6 grid_3.5.1 tidyselect_0.2.4 glue_1.3.0 [25] R6_2.2.2 pbdZMQ_0.3-3 purrr_0.2.5 magrittr_1.5 [29] scales_0.5.0 htmltools_0.3.6 assertthat_0.2.0 colorspace_1.3-2 [33] labeling_0.3 stringi_1.2.4 lazyeval_0.2.1 munsell_0.5.0 [37] crayon_1.3.4 而不是empty。那是更优雅的PHP代码。

strcmp