php - SQL Selection Based on html input -


how can write sql query based on user select on html interface? example

id     |    acc_type       |     bill_charge  |  month   |  year _________________________________________________________________ 1      |        store      |         12       |  january |  2014      2      |     water plant   |         11       |  january |  2013 3      |       store       |         51       |  february|  2014 4      |      pump house   |         34       |  march   |  2014 4      |      pump house   |         33       |  january |  2014 4      |      pump house   |          5       |  january |  2014 4      |      pump house   |         22       |  march   |  2013 4      |      pump house   |          0       |  march   |  2014 

and user in charge of months , year view data right thing can figure redirect different php file have query every single month , in particular month every single year 2014-2020 this:

select id , acc_type , bill_charge , month , year   acc_info , acc_details , acc_bill  acc_info.id = acc_details.id  , month ="january" , year = "2014" 

and user selecting input

<select name="month" id="month" width="300px" required/> <option value="">--select month--</option> <option value="january">january</option> <option value="february">february</option> <option value="march">march</option> <option value="april">april</option> <option value="may">may</option> <option value="june">june</option> <option value="july">july</option> <option value="august">august</option> <option value="september">september</option> <option value="october">october</option> <option value="november">november</option> <option value="december">december</option> </select>   <select  name="year" id="year" width="300px" required/> <option value="">--select year--</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> <option value="2020">2020</option> </select> 

is there better way this?

you pass values php script through form. first, define form:

<form action="script.php" method="post"> <select name="month" id="month" width="300px" required/> <option value="">--select month--</option> <option value="january">january</option> <option value="february">february</option> <option value="march">march</option> <option value="april">april</option> <option value="may">may</option> <option value="june">june</option> <option value="july">july</option> <option value="august">august</option> <option value="september">september</option> <option value="october">october</option> <option value="november">november</option> <option value="december">december</option> </select>  <select  name="year" id="year" width="300px" required/> <option value="">--select year--</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> <option value="2020">2020</option> </select> </form> 

after defining form, write php script parses incoming values, connects database , send request. example this:

<?php $month = $_request["month"]; $year = $_request["year"];  // database example php.net manual $mysqli = new mysqli("localhost", "my_user", "my_password", "world");  /* check connection */ if ($mysqli->connect_errno) {     printf("connect failed: %s\n", $mysqli->connect_error);     exit(); }  /* select queries return resultset */ if ($result = $mysqli->query("select id , acc_type , bill_charge , month , year                                 acc_info , acc_details , acc_bill                                acc_info.id = acc_details.id  , month ='".$month."' , year = '".$year."'")) {     printf("select returned %d rows.\n", $result->num_rows);      /* free result set */     $result->close(); }  $mysqli->close(); ?> 

please consider reading php + mysql manual, either through mysqli (http://php.net/manual/en/book.mysqli.php) or pdo (http://php.net/manual/en/ref.pdo-mysql.php).

p.s. please note using code $month = $_request["month"] might unsafe , lead sql injection. more on please read article: http://php.net/manual/en/security.database.sql-injection.php

update

if you're willing output data, may this:

if ($result = $mysqli->query("select id , acc_type , bill_charge , month , year                                 acc_info , acc_details , acc_bill                                acc_info.id = acc_details.id  , month ='".$month."' , year = '".$year."'")) {     while($output = $result->fetch_assoc) {         echo $output['id']." ".$output['acc_type']." ".$output['bill_charge']." ".$output['month']." ".$output['year']."<br>";     }      /* free result set */     $result->close(); } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -