简体   繁体   中英

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:

$query = "SELECT * FROM electrical ORDER BY item_id LIMIT $offset, $RPP"; 

With this query the div is populated on page load.

I have a select box that has 6 choices:

<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.

Script for showTable is working, after making selection alert shows choice picked.

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. then run that query and echo the data. after that, you can call this function in the script file after 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". during the fetching the time a loading gif will be visible in the "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. What you should do is to insert the q -variable from the query string into the SQL-query like this

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM