dongpan1365 2018-12-02 17:13
浏览 59
已采纳

停止向数据库PHP SQLITE添加重复条目

I have created a PHP form which adds to a database that I have created in PHP, however, I am trying to add a function which will stop the user from adding the same fruit into the database how would I try to do this as I have been trying to do it for a while thanks.

As you can see below the PHP script works fine by adding the variable's to the database however when it comes to implementing a check to make sure the fruit name does not match one from the database already I am struggling.

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



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


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

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

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




    }
    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条回答 默认 最新

  • duanjian4150 2018-12-02 21:46
    关注

    You can use my updated code. Didn't get to try the code though but it should work. Also note how I used empty instead of strcmp. That's a more elegant PHP code.

    <?php
        //SQLite Database test query
        $db=sqlite_open("fruitshop.db"); 
    
        if(isset( $_POST['fruit']) && !empty($_POST['fruit']) ){ //Adds to Database
    
            $item = sqlite_escape_string($_POST["fruit"]);
    
            $number = $_POST['number'];
    
    
            $test = sqlite_query($db, "SELECT * FROM fruit WHERE (fruit = '$item')");
    
            if(sqlite_num_rows($test) == 0){
    
                sqlite_query($db,"INSERT INTO fruit (fruit) VALUES ('$item')");
                sqlite_query($db,"INSERT INTO stock (Number) VALUES ($number)");
    
            } else {
                // Just in case you want this too.
                // echo "This database already contains a fruit called {$_POST['fruit]'}";
            }
    
            $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>
    ";
    
    
    
        //NOte the use of SQLITE_ASSOC
        echo "</br>
    ";
    
    
        $result=sqlite_query($db,"SELECT * from stock, fruit WHERE stock.Item = fruit.ID"); //Shows Databse
    
        echo "<th>Fruit</th><th>Qty</th>
    ";
    
        while($row=sqlite_fetch_array($result,SQLITE_ASSOC))
        {
            echo "<tr>
    ";
            echo "<td>" . $row['fruit.fruit'] . "</td>
    "; 
            echo "<td>" . $row['stock.Number'] . "</td>
    "; 
           echo "</tr>
    ";
        }
        echo "</table>
    ";
    
    
    
    
        }
        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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置