2017-05-28 125 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行)。试试看并反馈我 –