Translation update done using Pootle.
[phpmyadmin/crack.git] / tbl_select.php
blob0fbd24c77e3a485a54a442d1956dea597b9c501b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles table search tab
6 * display table search form, create SQL query from form data
7 * and include sql.php to execute it
9 * @todo display search form again if no results from previous search
10 * @version $Id$
11 * @package phpMyAdmin
14 /**
15 * Gets some core libraries
17 require_once './libraries/common.inc.php';
18 require_once './libraries/relation.lib.php'; // foreign keys
19 require_once './libraries/mysql_charsets.lib.php';
21 $GLOBALS['js_include'][] = 'tbl_change.js';
22 $GLOBALS['js_include'][] = 'jquery/jquery-1.4.2-min.js';
23 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.min.js';
25 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
26 $titles['Browse'] =
27 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
28 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
29 .$strBrowseForeignValues . '" />';
31 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
32 $titles['Browse'] .= $strBrowseForeignValues;
34 } else {
35 $titles['Browse'] = $strBrowseForeignValues;
38 /**
39 * Not selection yet required -> displays the selection form
41 if (!isset($param) || $param[0] == '') {
42 // Gets some core libraries
43 require_once './libraries/tbl_common.php';
44 //$err_url = 'tbl_select.php' . $err_url;
45 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
47 /**
48 * Gets tables informations
50 require_once './libraries/tbl_info.inc.php';
52 /**
53 * Displays top menu links
55 require_once './libraries/tbl_links.inc.php';
57 if (!isset($goto)) {
58 $goto = $GLOBALS['cfg']['DefaultTabTable'];
60 // Defines the url to return to in case of error in the next sql statement
61 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
63 // Gets the list and number of fields
64 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
65 $fields_cnt = PMA_DBI_num_rows($result);
66 // rabue: we'd better ensure, that all arrays are empty.
67 $fields_list = $fields_null = $fields_type = $fields_collation = array();
68 while ($row = PMA_DBI_fetch_assoc($result)) {
69 $fields_list[] = $row['Field'];
70 $type = $row['Type'];
71 // reformat mysql query output
72 if (strncasecmp($type, 'set', 3) == 0
73 || strncasecmp($type, 'enum', 4) == 0) {
74 $type = str_replace(',', ', ', $type);
75 } else {
77 // strip the "BINARY" attribute, except if we find "BINARY(" because
78 // this would be a BINARY or VARBINARY field type
79 if (!preg_match('@BINARY[\(]@i', $type)) {
80 $type = preg_replace('@BINARY@i', '', $type);
82 $type = preg_replace('@ZEROFILL@i', '', $type);
83 $type = preg_replace('@UNSIGNED@i', '', $type);
85 $type = strtolower($type);
87 if (empty($type)) {
88 $type = '&nbsp;';
90 $fields_null[] = $row['Null'];
91 $fields_type[] = $type;
92 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
93 ? $row['Collation']
94 : '';
95 } // end while
96 PMA_DBI_free_result($result);
97 unset($result, $type);
99 // <markus@noga.de>
100 // retrieve keys into foreign fields, if any
101 // check also foreigners even if relwork is FALSE (to get
102 // foreign keys from innodb)
103 $foreigners = PMA_getForeigners($db, $table);
105 <script type="text/javascript">
106 // <![CDATA[
107 function PMA_tbl_select_operator(f, index, multiple) {
108 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
109 <?php
110 reset($GLOBALS['cfg']['UnaryOperators']);
111 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
112 echo ' case "' . $operator . "\":\r\n";
115 bDisabled = true;
116 break;
118 default:
119 bDisabled = false;
121 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
123 // ]]>
124 </script>
125 <form method="post" action="tbl_select.php" name="insertForm">
126 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
127 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
128 <input type="hidden" name="back" value="tbl_select.php" />
130 <fieldset id="fieldset_table_search">
132 <fieldset id="fieldset_table_qbe">
133 <legend><?php echo $strDoAQuery; ?></legend>
134 <table class="data">
135 <thead>
136 <tr><th><?php echo $strField; ?></th>
137 <th><?php echo $strType; ?></th>
138 <th><?php echo $strCollation; ?></th>
139 <th><?php echo $strOperator; ?></th>
140 <th><?php echo $strValue; ?></th>
141 </tr>
142 </thead>
143 <tbody>
144 <?php
145 $odd_row = true;
147 for ($i = 0; $i < $fields_cnt; $i++) {
149 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
150 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
151 <td><?php echo $fields_type[$i]; ?></td>
152 <td><?php echo $fields_collation[$i]; ?></td>
153 <td><select name="func[]">
154 <?php
155 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
156 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
157 echo "\n" . ' '
158 . '<option value="' . htmlspecialchars($fc) . '">'
159 . htmlspecialchars($fc) . '</option>';
161 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
162 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
163 echo "\n" . ' '
164 . '<option value="' . htmlspecialchars($fc) . '">'
165 . htmlspecialchars($fc) . '</option>';
167 } else {
168 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
169 echo "\n" . ' '
170 . '<option value="' . htmlspecialchars($fc) . '">'
171 . htmlspecialchars($fc) . '</option>';
173 } // end if... else...
174 if ($fields_null[$i]) {
175 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
176 echo "\n" . ' '
177 . '<option value="' . htmlspecialchars($fc) . '">'
178 . htmlspecialchars($fc) . '</option>';
183 </select>
184 </td>
185 <td>
186 <?php
187 // <markus@noga.de>
188 $field = $fields_list[$i];
190 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
192 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
193 // f o r e i g n k e y s
194 echo ' <select name="fields[' . $i . ']">' . "\n";
195 // go back to first row
197 // here, the 4th parameter is empty because there is no current
198 // value of data for the dropdown (the search page initial values
199 // are displayed empty)
200 echo PMA_foreignDropdown($foreignData['disp_row'],
201 $foreignData['foreign_field'],
202 $foreignData['foreign_display'],
203 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
204 echo ' </select>' . "\n";
205 } elseif ($foreignData['foreign_link'] == true) {
207 <input type="text" name="fields[<?php echo $i; ?>]"
208 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
209 class="textfield" />
210 <script type="text/javascript">
211 // <![CDATA[
212 document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
213 // ]]>
214 </script>
215 <?php
216 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
217 // e n u m s
218 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
219 $cnt_enum_value = count($enum_value);
220 echo ' <select name="fields[' . $i . '][]"'
221 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
222 for ($j = 0; $j < $cnt_enum_value; $j++) {
223 echo ' <option value="' . $enum_value[$j] . '">'
224 . $enum_value[$j] . '</option>';
225 } // end for
226 echo ' </select>' . "\n";
227 } else {
228 // o t h e r c a s e s
229 echo ' <input type="text" name="fields[' . $i . ']"'
230 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
232 $type = $fields_type[$i];
233 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
235 <script type="text/javascript">
236 //<![CDATA[
237 document.write('<a title="<?php echo $strCalendar;?>" href="javascript:openCalendar(\'<?php echo PMA_generate_common_url();?>\', \'insertForm\', \'field_<?php echo ($i); ?>\', \'<?php echo (substr($type, 0, 9) == 'timestamp') ? 'datetime' : substr($type, 0, 9); ?>\', \'\')"><img class="calendar" src="<?php echo $pmaThemeImage; ?>b_calendar.png" alt="<?php echo $strCalendar; ?>"/></a>');
238 //]]>
239 </script>
240 <?php
243 <input type="hidden" name="names[<?php echo $i; ?>]"
244 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
245 <input type="hidden" name="types[<?php echo $i; ?>]"
246 value="<?php echo $fields_type[$i]; ?>" />
247 <input type="hidden" name="collations[<?php echo $i; ?>]"
248 value="<?php echo $fields_collation[$i]; ?>" />
249 </td>
250 </tr>
251 <?php
252 } // end for
254 </tbody>
255 </table>
256 </fieldset>
257 <?php
258 PMA_generate_slider_effect('searchoptions', $strOptions);
260 <fieldset id="fieldset_select_fields">
261 <legend><?php echo $strSelectFields; ?></legend>
262 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
263 multiple="multiple">
264 <?php
265 // Displays the list of the fields
266 foreach ($fields_list as $each_field) {
267 echo ' '
268 .'<option value="' . htmlspecialchars($each_field) . '"'
269 .' selected="selected">' . htmlspecialchars($each_field)
270 .'</option>' . "\n";
273 </select>
274 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
275 <label for="oDistinct">DISTINCT</label>
276 </fieldset>
278 <fieldset id="fieldset_search_conditions">
279 <legend><?php echo '<em>' . $strOr . '</em> ' .$strAddSearchConditions; ?></legend>
280 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
282 <input type="text" name="where" class="textfield" size="64" />
283 </fieldset>
285 <fieldset id="fieldset_limit_rows">
286 <legend><?php echo $strLimitNumRows; ?></legend>
287 <input type="text" size="4" name="session_max_rows"
288 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
289 </fieldset>
291 <fieldset id="fieldset_display_order">
292 <legend><?php echo $strDisplayOrder; ?></legend>
293 <select name="orderField" style="vertical-align: middle">
294 <option value="--nil--"></option>
295 <?php
296 foreach ($fields_list as $each_field) {
297 echo ' '
298 .'<option value="' . htmlspecialchars($each_field) . '">'
299 .htmlspecialchars($each_field) . '</option>' . "\n";
300 } // end for
302 </select>
303 <?php
304 $choices = array(
305 'ASC' => $strAscending,
306 'DESC' => $strDescending
308 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
309 unset($choices);
311 </fieldset>
312 <br style="clear: both;"/>
313 </div>
314 </fieldset>
315 <fieldset class="tblFooters">
316 <input type="hidden" name="max_number_of_fields"
317 value="<?php echo $fields_cnt; ?>" />
318 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
319 </fieldset>
320 </form>
321 <?php
322 require_once './libraries/footer.inc.php';
327 * Selection criteria have been submitted -> do the work
329 else {
330 // Builds the query
332 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
334 // if all fields were selected to display, we do a SELECT *
335 // (more efficient and this helps prevent a problem in IE
336 // if one of the rows is edited and we come back to the Select results)
338 if (count($param) == $max_number_of_fields) {
339 $sql_query .= '* ';
340 } else {
341 $param = PMA_backquote($param);
342 $sql_query .= implode(', ', $param);
343 } // end if
345 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
346 // to 'tbl_select.php'
347 unset($param);
349 $sql_query .= ' FROM ' . PMA_backquote($table);
351 // The where clause
352 if (trim($where) != '') {
353 $sql_query .= ' WHERE ' . $where;
354 } else {
355 $w = $charsets = array();
356 $cnt_func = count($func);
357 reset($func);
358 while (list($i, $func_type) = each($func)) {
359 list($charsets[$i]) = explode('_', $collations[$i]);
360 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
361 $fields[$i] = '';
362 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
364 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
365 if (!empty($fields[$i])) {
366 if (!is_array($fields[$i])) {
367 $fields[$i] = explode(',', $fields[$i]);
369 $enum_selected_count = count($fields[$i]);
370 if ($func_type == '=' && $enum_selected_count > 1) {
371 $func_type = $func[$i] = 'IN';
372 $parens_open = '(';
373 $parens_close = ')';
375 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
376 $func_type = $func[$i] = 'NOT IN';
377 $parens_open = '(';
378 $parens_close = ')';
380 } else {
381 $parens_open = '';
382 $parens_close = '';
384 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
385 for ($e = 1; $e < $enum_selected_count; $e++) {
386 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
389 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
392 } elseif ($fields[$i] != '') {
393 // For these types we quote the value. Even if it's another type (like INT),
394 // for a LIKE we always quote the value. MySQL converts strings to numbers
395 // and numbers to strings as necessary during the comparison
396 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
397 $quot = '\'';
398 } else {
399 $quot = '';
402 // LIKE %...%
403 if ($func_type == 'LIKE %...%') {
404 $func_type = 'LIKE';
405 $fields[$i] = '%' . $fields[$i] . '%';
407 if ($func_type == 'REGEXP ^...$') {
408 $func_type = 'REGEXP';
409 $fields[$i] = '^' . $fields[$i] . '$';
412 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
413 $func_type = str_replace(' (...)', '', $func_type);
415 // quote values one by one
416 $values = explode(',', $fields[$i]);
417 foreach ($values as &$value)
418 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
420 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
421 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
422 else
423 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
425 else {
426 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
429 } // end if
430 } // end for
432 if ($w) {
433 $sql_query .= ' WHERE ' . implode(' AND ', $w);
435 } // end if
437 if ($orderField != '--nil--') {
438 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
439 } // end if
441 require './sql.php';