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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?