[英]how to write php MySql query
I would like to be able to make a selection from a select-box and view the selected table. 我希望能够从选择框中进行选择并查看所选表。
My skill level with php/MySQL at this point is: 我在php / MySQL的技能水平是:
$query = "SELECT * FROM electrical ORDER BY item_id LIMIT $offset, $RPP";
With this query the div
is populated on page load. 通过此查询,将在页面加载时填充div
。
I have a select box that has 6 choices: 我有一个选择框,有6个选择:
<form>
<select name="dbtables" onchange="showTable(this.value)" id="selectBox">
<option value="">Select a materials table:</option>
<option value="" id="hr"></option>
<option value="equipment">Worker Equipment</option>
<option value="tool">General Tools</option>
<option value="electrical">Electrical Materials</option>
<option value="mechanical">Mechanical Materials</option>
<option value="plumbing">Plumbing Materials</option>
<option value="hvac">HVAC Materials</option>
</select>
</form>
Each choice is a table in a single MySQL database. 每个选择都是单个MySQL数据库中的一个表。
Script for showTable
is working, after making selection alert shows choice picked. showTable
脚本正在运行,在发出选择警报后显示选择的选择。
function showTable(str) {
if (str == "") {
document.getElementById("materials").innerHTML = "";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("materials").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "index.php?q=" + str, true);
xmlhttp.send();
alert(str);
}
Any pointers or help is appreciated appreciated. 任何指针或帮助表示赞赏。
in your index.php pick the $str value using $_GET method and construct the query. 在index.php中,使用$ _GET方法选择$ str值并构造查询。 then run that query and echo the data. 然后运行该查询并回显数据。 after that, you can call this function in the script file after alert(). 之后,您可以在alert()之后在脚本文件中调用此函数。
function useHttpResponse() {
if (http.readyState == 4) {
if(http.status == 200) {
var txt = http.responseText;
var response_msg="<h4><span class='darkblue'>" + txt + "</span></h4>"
document.getElementById('showtime').innerHTML = response_msg;
}
} else { //you may skip this else portion
document.getElementById('showtime').innerHTML = '<img src="images/loading.gif" /><span class="darkblue">retrieving data...</span>';
}
}
here your fetched data will show on a div with id "showtime". 在这里,您提取的数据将显示在ID为“ showtime”的div上。 during the fetching the time a loading gif will be visible in the "showtime" div. 在获取时间期间,加载的gif将在“ showtime” div中可见。 or you may skip that else portion. 或者您可以跳过其他部分。 hope that function will help you. 希望该功能对您有所帮助。
I think what you're looking for is the SQL-query to execute on your database. 我认为您正在寻找的是要在数据库上执行的SQL查询。 What you should do is to insert the q
-variable from the query string into the SQL-query like this 您应该做的是将查询字符串中的q
-variable插入到SQL查询中,如下所示
$query = "SELECT * FROM " . $_REQUEST['q'] . " ORDER BY item_id LIMIT $offset, $RPP";
Ofcourse you should check or escape the variables to prevent SQL injections, using something like mysql_real_escape_string
, but that's not really the scope of this question. 当然,您应该使用mysql_real_escape_string
类的方法检查或转义变量以防止SQL注入,但这实际上不是此问题的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.