2011-09-22 61 views
0

我已經花了最近幾天的機智作爲保存按鈕使用PHP腳本的簡單概念。當從網頁按下按鈕時,將數據從table A插入table B,然後刪除table A並重定向回我的主index.htmlPHP SQLite3查詢插入,刪除和重定向

<?php 
try { 
    $db = new PDO('sqlite:/srv/db/data.db'); 
} 

    $db->exec("INSERT * INTO Archive FROM resultstbl"); 
    $db->exec("DELETE * FROM resultstbl") 
unset($db); 
?> 

到目前爲止,我可以使用這個PHP查詢的一些幫助,以及任何指導。

回答

1

我會做這樣的事情:

<? 

if(isset($_POST['but1'])) // this checks if the button is clicked 
{ 
    $db = new PDO('sqlite:/srv/db/data.db'); // I assume this is working fine 
    $db->exec("INSERT INTO Archive SELECT * FROM resultstbl"); // tables must be equivalent in terms of fields 
    $db->exec("DELETE FROM resultstbl") // You want to delete the records on this table or the table itself? This deletes the records 

    header("Location: index.php?page=home"); // This will only work if you didn't output anything to the screen yet. If you displayed something it will fail 
    die(); 

} 

?> 

<form action="sql.php" method="POST"> <!-- Assuming this page is sql.php --> 

    <input type="submit" name="but1" value="GO!"/> 
</form> 

您可以檢查語法的插入和這裏的SQLite刪除statments:

http://www.sqlite.org/lang_insert.html

http://www.sqlite.org/lang_delete.html

+0

謝謝你非常非常很多,我乞求把我的頭髮拉出來。只是一個快速的附加問題,但是......腳本按照我想要的循環並刪除了表格的內容,但它沒有將resultstbl的內容複製到檔案中。這可能是因爲Archive中有一個關鍵字,而resultstbl中沒有匹配的列? – bikerben

+0

當然,表格必須具有完全相同的結構才能從一個直接選擇到另一個。爲了解決你現在遇到的問題,你應該這樣做:'插入存檔(field1,field2,field3)(從resultstbl中選擇field1,field2,field3)' –

+0

只需要一個腳註,上面我的問題的答案是的,這兩個字段都需要具有完全相同的字段才能使用所提到的INSERT語法。 – bikerben