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条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”