我在互聯網上搜索了一個在我們的drupal 7網站上使用我們的歷史數據庫的方法。我有一個建立了姓名,結婚日期等數據庫。我想有一個基本頁面來做搜索。我發現了一個幫助代碼的網站,但它不起作用。我不是程序員,但我可以按照指示行事。下面是我的代碼有:用drupal 7查詢數據庫
在使用PHP代碼作爲我的輸入格式基本頁面,
<html>
<body>
<script language=」javascript」 type=」text/javascript」>
<!–
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject(「Msxml2.XMLHTTP」);
} catch (e) {
try{
ajaxRequest = new ActiveXObject(「Microsoft.XMLHTTP」);
} catch (e){
// Something went wrong
alert(「Your browser broke!」);
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById("ajaxDiv");
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var lastphp = document.getElementById("lastphp").value;
var queryString = 「?lastphp=」 + lastphp;
ajaxRequest.open(「GET」, 「/php/check.php」 + queryString, true);
ajaxRequest.send(null);
}
//–>
</script>
<form name="myForm">
<table border=」0″>
<tr>
<td width = 100>Last Name: <br /></td>
<td><input type="text" id="lastphp"> </td>
</tr>
</table>
<br />
<input type="button" onclick="ajaxFunction()" value="Search" />
</form>
<div id="ajaxDiv"></div>
</body>
</html>
然後,我創建了一個PHP文件:
<?php
//Connect to MySQL Server
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","root","") or die('Cannot connect to the database because: ' . mysql_error());
//specify database ** EDIT REQUIRED HERE **
mysql_select_db(「genealogy」) or die(「Unable to select database」); //select which database we’re using
// Retrieve data from Query String
$last = $_GET['lastphp'];
// Escape User Input to help prevent SQL Injection
$last = mysql_real_escape_string($last);
//Build and run a SQL Query on our MySQL tutorial
$query = 「SELECT * from columbus_ledger_enquirer_obituary_db」; //just grab every row from our table
$results = mysql_query($query)or die(mysql_error());
//print what the user entered (eventually I’m sure you’ll want to use this data in your query)
echo 「You Entered: 」 . $last . 「<br><br>」;
//print the results
echo 「Database Results: <br>」;
while($row = mysql_fetch_array($results)){
echo 「$row[lastname]<br>」; //NOTE: Here we are printing all the data from the ‘lastname’ column of our database, either change this name, or make sure you have a lastname column with some data in it
}
?>
我把php文件放在一個名爲php的文件夾下/ sites/default。
基本頁面有一個不錯的框和搜索按鈕,但是當我鍵入一個名字時,什麼都不會發生。
這裏有人能告訴我什麼是錯?
感謝
這看起來不像是一種正確的「Drupal」做事方式。我建議閱讀手冊,並從簡單的事情開始。 – SpaceBeers 2012-03-26 14:26:42