Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>How To Insert Data Into MySQL db using form in php</title>
  4. </head>
  5. <body>
  6.  
  7.  
  8. <?php
  9.  
  10. $hostname = "10.15.14.247";
  11. $db_user = "root"; // change to your database password
  12. $db_password = "master"; // change to your database password
  13. $database = "acesbd"; // provide your database name
  14.  
  15. $db = mysql_connect($hostname, $db_user, $db_password);
  16. mysql_select_db($database,$db);
  17.  
  18. echo"<form method=post action=''><input type=hidden name=todo value=search>";
  19.  
  20.  
  21. if($search_text != "" and $search_text != 'procura'){
  22. ?>
  23. <input type=text name=search_text value="<?php echo $_POST['search_text']; ?>"><input type=submit value=Search><br>
  24. <?php
  25. }
  26. else echo"<input type=text name=search_text value='procura' onFocus=\"this.value=''\"><input type=submit value=Search><br>";
  27.  
  28. echo"<select name='dropdown'>";
  29. $sqlin = "SELECT US_ID, US_nome FROM unidades_saude ORDER BY US_nome";
  30. if($result = mysql_query($sqlin ,$db)) {
  31. echo"<option value=0>--seleccionar extensão--</option>";
  32. while ($row = mysql_fetch_array($result)){
  33. $field1=$row[0];
  34. $field2=$row[1];
  35. echo "<option value=$field1>$field2</option>";
  36. }
  37. echo"</select>";
  38. }else {
  39. echo "ERROR1: ".mysql_error();
  40. }
  41.  
  42.  
  43. echo"<br><input type=radio name=type value=any checked>Qualquer ocorrência <input type=radio name=type value=exact>Palavra exacta
  44.  
  45. </form>";
  46.  
  47. $columnsSelect = ("CON_Nome,
  48. TS_nome,
  49. US_nome,
  50. CON_Email,
  51. CON_Telefone,
  52. CON_Fax,
  53. CON_Ext_Tele,
  54. TS_ID,
  55. US_ID,
  56. CON_ID");
  57.  
  58. $columnsSearch = array("CON_Nome",
  59. "TS_nome",
  60. "US_nome",
  61. "CON_Email",
  62. "CON_Telefone",
  63. "CON_Fax",
  64. "CON_Ext_Tele");
  65.  
  66.  
  67. $todo=$_POST['todo'];
  68. if(isset($todo) and $todo=="search"){
  69. $search_text=$_POST['search_text'];
  70. $type=$_POST['type'];
  71.  
  72. $search_text=ltrim($search_text);
  73. $search_text=rtrim($search_text);
  74.  
  75. if ($search_text == 'procura')
  76. $query="SELECT $columnsSelect FROM view_con_ts_us ORDER BY CON_ID ASC";
  77. //$query="SELECT US_nome FROM unidades_saude";
  78. else{
  79. switch ($type) {
  80. case "any":
  81. if ($dropdown == 0) {
  82. $kt=split(" ",$search_text);//Breaking the string to array of words
  83. // Now let us generate the sql
  84. while(list($key,$val)=each($kt)){
  85. if($val<>" " and strlen($val) > 0){
  86. $x = 0;
  87. while($columnsSearch[$x] != null){
  88. $q .= " $columnsSearch[$x] like '%$val%' or ";
  89. $x++;
  90. }
  91. }
  92.  
  93. }// end of while
  94. $q=substr($q,0,(strlen($q)-3));
  95. // this will remove the last or from the string.
  96. $query="SELECT $columnsSelect FROM view_con_ts_us WHERE ($q) ORDER BY CON_ID ASC";
  97. }else{
  98. $kt=split(" ",$search_text);//Breaking the string to array of words
  99. // Now let us generate the sql
  100. while(list($key,$val)=each($kt)){
  101. if($val<>" " and strlen($val) > 0){
  102. $x = 0;
  103. while($columnsSearch[$x] != null){
  104. $q .= " $columnsSearch[$x] like '%$val%' or ";
  105. $x++;
  106. }
  107. }
  108.  
  109. }// end of while
  110. $q=substr($q,0,(strlen($q)-3));
  111. // this will remove the last or from the string.
  112. $query="SELECT $columnsSelect FROM view_con_ts_us WHERE ($q) and US_ID = $dropdown ORDER BY CON_ID ASC";
  113. }
  114. break;
  115. case "exact":
  116. if ($dropdown == 0) {
  117. $query="SELECT $columnsSelect FROM view_con_ts_us WHERE CON_Nome = '$search_text' or
  118. TS_nome = '$search_text' or
  119. US_nome = '$search_text' or
  120. CON_Email = '$search_text' or
  121. CON_Telefone = '$search_text' or
  122. CON_Fax = '$search_text' or
  123. CON_Ext_Tele = '$search_text' ORDER BY CON_ID ASC";
  124. }else
  125. $query="SELECT $columnsSelect FROM view_con_ts_us WHERE (CON_Nome = '$search_text' or
  126. TS_nome = '$search_text' or
  127. US_nome = '$search_text' or
  128. CON_Email = '$search_text' or
  129. CON_Telefone = '$search_text' or
  130. CON_Fax = '$search_text' or
  131. CON_Ext_Tele = '$search_text') and US_ID = $dropdown ORDER BY CON_ID ASC";
  132.  
  133. break;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. } // end of if else based on type value
  140.  
  141. echo $query;
  142. echo "<br><br>";
  143. if($result = mysql_query($query ,$db)){
  144. while($row = mysql_fetch_array($result)){
  145. ?>
  146. <table border="1" bgcolor="#CCFFCC" style="border-collapse: collapse; border-left-width:0; border-top-width:0; border-bottom-width:0" bordercolor="#FFFFFF" cellpadding="2" cellspacing="0" height="10" width="100%" style="table-layout:fixed"><tr>
  147. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[0]" ?></p></td>
  148. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[1]" ?></p></td>
  149. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[2]" ?></p></td>
  150. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[3]" ?></p></td>
  151. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[4]" ?></p></td>
  152. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[5]" ?></p></td>
  153. <td height=25 width="80px" bordercolor="#952500" style="border-style: solid; border-width: 1" bgcolor="#FFDDDD"><p class="corpo_tabela"><?php echo"$row[6]" ?></p></td>
  154. </tr></table>
  155. <?php
  156. }
  157. }else {
  158. echo "ERROR1: ".mysql_error();
  159. }
  160. }
  161.  
  162.  
  163. ?>
  164.  
  165. </body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement