2017-05-28 138 views
0

什麼是從HTML腳本中查詢SQLITE數據庫的寫入語法? 下面的代碼(其中btw在單獨的php文件中使用時能夠完美地工作)不會在文本區域中返回任何內容....如何從HTML腳本中查詢SQLITE數據庫

任何幫助將不勝感激。由於

<p>Patient search:</p> 
<textarea id="SearchBoxPt" textarea name="SearchBoxPt" style="height: 30px; resize: none; width: 600px;">  
</textarea></p> 

<button type="button" onclick="populateField_SearchPt()">Load Pt 1</button> 

<script> 
function populateField_SearchPt() 
{   
    <?php 
    class MyDB extends SQLite3 
    { 
     function __construct() 
     { 
      $this->open('Anagrafica.db'); 
     } 
    } 

    $db = new MyDB(); 
    if(!$db) 
    { 
     echo $db->lastErrorMsg(); 
    } else 
    { 
     echo "Opened database successfully\n\n"; 
    } 

    $results = $db->query('SELECT name FROM Anagrafica WHERE hospital_ID="1"'); 
    ?>  

    document.getElementById ("SearchBoxPt").value = $results; 
} 
</script> 
+0

你需要獲取你的結果:http://php.net/manual/en/sqlite3.open.php – Progrock

+0

而且你還需要呼應你的醫院名稱。 – Progrock

+0

而你的html文件必須解析爲Php。 – Progrock

回答

1

下面是一個例子

PHP

<?php 
class MyDB extends SQLite3 
{ 
    function __construct() 
    { 
     $this->open('Anagrafica.db'); 
    } 
} 

$db = new MyDB(); 
if(!$db) 
{ 
    echo $db->lastErrorMsg(); 
} 

$hId = $_GET['hId']; //we're getting the passed hId as a paramater in the url 

$query = $db->prepare("SELECT name FROM Anagrafica WHERE hospital_ID=:id"); 
$query->bindValue(':id', $hId, SQLITE3_INTEGER); 
$results = $query->execute()->fetchArray(); 
echo $results['name']; 
?> 

HTML

<p>Enter Hospital Id: 
    <input id="HospitalId" type="number" value="1"> 
</p> 

<p>Patient search:</p> 
<textarea id="SearchBoxPt" textarea name="SearchBoxPt" style="height: 30px; resize: none; width: 600px;">  
</textarea> 
</p> 

<button type="button" onclick="populateField_SearchPt()">Load Pt 1</button> 

<script> 
function populateField_SearchPt() 
{ 
    var inputId = document.getElementById("HospitalId").value; //we get the user input value and put it in a var 

    var xhttp = new XMLHttpRequest(); 
    xhttp.open("GET", "path/to/yourPhpFile.php?hId=" + inputId, true); // we're passing the hId to the server as a parameter 
    xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
      document.getElementById("SearchBoxPt").value = this.responseText; 
     } 
    }; 
    xhttp.send(); 

} 
</script> 
+0

不工作...字段保持空白 – jeddi

+0

嘗試轉到localhost/path/to/yourPhpFile.php?hId = 1並檢查輸出結果 –

+0

空白頁。 ..我注意到你的初始php代碼和最新的hld之間的引號有一些差異。這可能是原因嗎? $ hId = $ _GET ['hId']; //我們在URL中獲取傳遞的參數作爲參數 $ results = $ db-> query(「SELECT name FROM Anagrafica WHERE hospital_ID = $ hId」) - > fetchArray(); //獲取特定條目 - 硬編碼 $ results = $ db-> query('SELECT name FROM Anagrafica WHERE hospital_ID =「1」') - > fetchArray(); – jeddi

1

你不能把PHP腳本的JavaScript知道PHP在服務器端執行,而不是瀏覽器。

的解決辦法是保持你的PHP代碼到一個分離式文件,並做出AJAX調用到PHP上的每個按鈕,即可從數據庫retreive數據。

PHP

<?php 
class MyDB extends SQLite3 
{ 
    function __construct() 
    { 
     $this->open('Anagrafica.db'); 
    } 
} 

$db = new MyDB(); 
if(!$db) 
{ 
    echo $db->lastErrorMsg(); 
} 

$results = $db->query('SELECT name FROM Anagrafica WHERE hospital_ID="1"')->fetchArray(); 
echo $results['name']; 
?>  

HTML

<p>Patient search:</p> 
<textarea id="SearchBoxPt" textarea name="SearchBoxPt" style="height: 30px; resize: none; width: 600px;">  
</textarea> 
</p> 

<button type="button" onclick="populateField_SearchPt()">Load Pt 1</button> 

<script> 
function populateField_SearchPt() 
{ 
    var xhttp = new XMLHttpRequest(); 
    xhttp.open("GET", "path/to/yourPhpFile.php", true); 
    xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
      document.getElementById("SearchBoxPt").value = this.responseText; 
     } 
    }; 
    xhttp.send(); 

} 
</script> 
+0

謝謝。我已經嘗試過,但PHP必須有一些問題,因爲它不檢索值... – jeddi

+0

你確定echo $ results ['name']; ? – jeddi

+0

我已經更新了php文件(第16行)。試試看並反饋我 –