php pdo代码从用户输入动态插入行

时间:2015-02-02 15:36:41

标签: php pdo

我的问题是 -

列名 - 总计

文本框名称 - totalloop

我需要的是当用户在totalloop文本框中输入5时...然后在DB中插入5行

价值在总栏里面就像 1,2,3,4,5 ......... ok。

再次当用户在totalloop文本框中输入3时......然后在DB中插入3行

价值进入总栏来自 6,7,8 ......... ok。

每个想法都可以从我的代码中正常工作....

plz建议将查询和变量放在何处,以便每次从数据库列中的循环插入最大值。

   if(isset($_POST['save']))
    {       
                    $book_no = $_POST['book_no'];                   
                    $created = date("Y-m-d H:i:s");                     

                    $query = $database->getRow("SELECT MAX(book_no) AS max1 FROM scheme_master;");
                        $largestNumber = $query['max1'];
                        $top = $largestNumber + 1;  

                    for($row=1;$row<=$book_no;$row++)
                    {   

                        $insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,created) VALUES (:book_no,:created)", 
                        array(':book_no'=>$top,':created'=>$created));
                    }
                    $_SESSION['message'] = "Books Created Successfully";                        
    }   

1 个答案:

答案 0 :(得分:0)

if(isset($_POST['save']))
    {       
                    $book_no = $_POST['book_no'];                   
                    $created = date("Y-m-d H:i:s");                         
                    for($row=1;$row<=$book_no;$row++)
                    {   
                        $query = $database->getRow("SELECT MAX(book_no) AS max1 FROM scheme_master;");
                        $largestNumber = $query['max1'];
                        $top = $largestNumber + 1;
                        $insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,created) VALUES (:book_no,:created)", 
                        array(':book_no'=>$top,':created'=>$created));
                    }
                    $_SESSION['message'] = "Books Created Successfully";                        
    }       

此代码根据输入的值从用户...循环插入查询获取...并且每次在DB列中插入max + 1值。