Translation update done using Pootle.
[phpmyadmin/crack.git] / tbl_select.php
blob99936f36ea1756c84d5ac10711dd12126112b602
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-ui-1.8.custom.js';
23 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
24 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
25 $titles['Browse'] =
26 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
27 .'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
28 . __('Browse foreign values') . '" />';
30 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
31 $titles['Browse'] .= __('Browse foreign values');
33 } else {
34 $titles['Browse'] = __('Browse foreign values');
37 /**
38 * Not selection yet required -> displays the selection form
40 if (!isset($param) || $param[0] == '') {
41 // Gets some core libraries
42 require_once './libraries/tbl_common.php';
43 //$err_url = 'tbl_select.php' . $err_url;
44 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
46 /**
47 * Gets tables informations
49 require_once './libraries/tbl_info.inc.php';
51 /**
52 * Displays top menu links
54 require_once './libraries/tbl_links.inc.php';
56 if (!isset($goto)) {
57 $goto = $GLOBALS['cfg']['DefaultTabTable'];
59 // Defines the url to return to in case of error in the next sql statement
60 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
62 // Gets the list and number of fields
63 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
64 $fields_cnt = PMA_DBI_num_rows($result);
65 // rabue: we'd better ensure, that all arrays are empty.
66 $fields_list = $fields_null = $fields_type = $fields_collation = array();
67 while ($row = PMA_DBI_fetch_assoc($result)) {
68 $fields_list[] = $row['Field'];
69 $type = $row['Type'];
70 // reformat mysql query output
71 if (strncasecmp($type, 'set', 3) == 0
72 || strncasecmp($type, 'enum', 4) == 0) {
73 $type = str_replace(',', ', ', $type);
74 } else {
76 // strip the "BINARY" attribute, except if we find "BINARY(" because
77 // this would be a BINARY or VARBINARY field type
78 if (!preg_match('@BINARY[\(]@i', $type)) {
79 $type = preg_replace('@BINARY@i', '', $type);
81 $type = preg_replace('@ZEROFILL@i', '', $type);
82 $type = preg_replace('@UNSIGNED@i', '', $type);
84 $type = strtolower($type);
86 if (empty($type)) {
87 $type = '&nbsp;';
89 $fields_null[] = $row['Null'];
90 $fields_type[] = $type;
91 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
92 ? $row['Collation']
93 : '';
94 } // end while
95 PMA_DBI_free_result($result);
96 unset($result, $type);
98 // retrieve keys into foreign fields, if any
99 // check also foreigners even if relwork is FALSE (to get
100 // foreign keys from innodb)
101 $foreigners = PMA_getForeigners($db, $table);
103 <script type="text/javascript">
104 // <![CDATA[
105 function PMA_tbl_select_operator(f, index, multiple) {
106 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
107 <?php
108 reset($GLOBALS['cfg']['UnaryOperators']);
109 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
110 echo ' case "' . $operator . "\":\r\n";
113 bDisabled = true;
114 break;
116 default:
117 bDisabled = false;
119 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
121 // ]]>
122 </script>
123 <form method="post" action="tbl_select.php" name="insertForm">
124 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
125 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
126 <input type="hidden" name="back" value="tbl_select.php" />
128 <fieldset id="fieldset_table_search">
130 <fieldset id="fieldset_table_qbe">
131 <legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
132 <table class="data">
133 <thead>
134 <tr><th><?php echo __('Column'); ?></th>
135 <th><?php echo __('Type'); ?></th>
136 <th><?php echo __('Collation'); ?></th>
137 <th><?php echo __('Operator'); ?></th>
138 <th><?php echo __('Value'); ?></th>
139 </tr>
140 </thead>
141 <tbody>
142 <?php
143 $odd_row = true;
145 for ($i = 0; $i < $fields_cnt; $i++) {
147 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
148 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
149 <td><?php echo $fields_type[$i]; ?></td>
150 <td><?php echo $fields_collation[$i]; ?></td>
151 <td><select name="func[]">
152 <?php
153 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
154 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
155 echo "\n" . ' '
156 . '<option value="' . htmlspecialchars($fc) . '">'
157 . htmlspecialchars($fc) . '</option>';
159 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
160 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
161 echo "\n" . ' '
162 . '<option value="' . htmlspecialchars($fc) . '">'
163 . htmlspecialchars($fc) . '</option>';
165 } else {
166 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
167 echo "\n" . ' '
168 . '<option value="' . htmlspecialchars($fc) . '">'
169 . htmlspecialchars($fc) . '</option>';
171 } // end if... else...
172 if ($fields_null[$i]) {
173 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
174 echo "\n" . ' '
175 . '<option value="' . htmlspecialchars($fc) . '">'
176 . htmlspecialchars($fc) . '</option>';
181 </select>
182 </td>
183 <td>
184 <?php
185 $field = $fields_list[$i];
187 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
189 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
190 // f o r e i g n k e y s
191 echo ' <select name="fields[' . $i . ']">' . "\n";
192 // go back to first row
194 // here, the 4th parameter is empty because there is no current
195 // value of data for the dropdown (the search page initial values
196 // are displayed empty)
197 echo PMA_foreignDropdown($foreignData['disp_row'],
198 $foreignData['foreign_field'],
199 $foreignData['foreign_display'],
200 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
201 echo ' </select>' . "\n";
202 } elseif ($foreignData['foreign_link'] == true) {
204 <input type="text" name="fields[<?php echo $i; ?>]"
205 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
206 class="textfield" />
207 <script type="text/javascript">
208 // <![CDATA[
209 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>');
210 // ]]>
211 </script>
212 <?php
213 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
214 // e n u m s
215 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
216 $cnt_enum_value = count($enum_value);
217 echo ' <select name="fields[' . $i . '][]"'
218 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
219 for ($j = 0; $j < $cnt_enum_value; $j++) {
220 echo ' <option value="' . $enum_value[$j] . '">'
221 . $enum_value[$j] . '</option>';
222 } // end for
223 echo ' </select>' . "\n";
224 } else {
225 // o t h e r c a s e s
226 echo ' <input type="text" name="fields[' . $i . ']"'
227 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
229 $type = $fields_type[$i];
230 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
232 <script type="text/javascript">
233 //<![CDATA[
234 $(function() {
235 $('#field_<?php echo $i; ?>').datepicker({
236 duration: '',
237 time24h: true,
238 stepMinutes: 1,
239 stepHours: 1,
240 <?php echo ($type == 'date' ? "showTime: false,":"showTime: true,"); ?>
241 altTimeField: '',
242 constrainInput: false
245 //]]>
246 </script>
247 <?php
250 <input type="hidden" name="names[<?php echo $i; ?>]"
251 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
252 <input type="hidden" name="types[<?php echo $i; ?>]"
253 value="<?php echo $fields_type[$i]; ?>" />
254 <input type="hidden" name="collations[<?php echo $i; ?>]"
255 value="<?php echo $fields_collation[$i]; ?>" />
256 </td>
257 </tr>
258 <?php
259 } // end for
261 </tbody>
262 </table>
263 </fieldset>
264 <?php
265 PMA_generate_slider_effect('searchoptions', __('Options'));
267 <fieldset id="fieldset_select_fields">
268 <legend><?php echo __('Select columns (at least one):'); ?></legend>
269 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
270 multiple="multiple">
271 <?php
272 // Displays the list of the fields
273 foreach ($fields_list as $each_field) {
274 echo ' '
275 .'<option value="' . htmlspecialchars($each_field) . '"'
276 .' selected="selected">' . htmlspecialchars($each_field)
277 .'</option>' . "\n";
280 </select>
281 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
282 <label for="oDistinct">DISTINCT</label>
283 </fieldset>
285 <fieldset id="fieldset_search_conditions">
286 <legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
287 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
289 <input type="text" name="where" class="textfield" size="64" />
290 </fieldset>
292 <fieldset id="fieldset_limit_rows">
293 <legend><?php echo __('Number of rows per page'); ?></legend>
294 <input type="text" size="4" name="session_max_rows"
295 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
296 </fieldset>
298 <fieldset id="fieldset_display_order">
299 <legend><?php echo __('Display order:'); ?></legend>
300 <select name="orderField">
301 <option value="--nil--"></option>
302 <?php
303 foreach ($fields_list as $each_field) {
304 echo ' '
305 .'<option value="' . htmlspecialchars($each_field) . '">'
306 .htmlspecialchars($each_field) . '</option>' . "\n";
307 } // end for
309 </select>
310 <?php
311 $choices = array(
312 'ASC' => __('Ascending'),
313 'DESC' => __('Descending')
315 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
316 unset($choices);
318 </fieldset>
319 <br style="clear: both;"/>
320 </div>
321 </fieldset>
322 <fieldset class="tblFooters">
323 <input type="hidden" name="max_number_of_fields"
324 value="<?php echo $fields_cnt; ?>" />
325 <input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
326 </fieldset>
327 </form>
328 <?php
329 require_once './libraries/footer.inc.php';
334 * Selection criteria have been submitted -> do the work
336 else {
337 // Builds the query
339 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
341 // if all fields were selected to display, we do a SELECT *
342 // (more efficient and this helps prevent a problem in IE
343 // if one of the rows is edited and we come back to the Select results)
345 if (count($param) == $max_number_of_fields) {
346 $sql_query .= '* ';
347 } else {
348 $param = PMA_backquote($param);
349 $sql_query .= implode(', ', $param);
350 } // end if
352 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
353 // to 'tbl_select.php'
354 unset($param);
356 $sql_query .= ' FROM ' . PMA_backquote($table);
358 // The where clause
359 if (trim($where) != '') {
360 $sql_query .= ' WHERE ' . $where;
361 } else {
362 $w = $charsets = array();
363 $cnt_func = count($func);
364 reset($func);
365 while (list($i, $func_type) = each($func)) {
366 list($charsets[$i]) = explode('_', $collations[$i]);
367 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
368 $fields[$i] = '';
369 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
371 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
372 if (!empty($fields[$i])) {
373 if (!is_array($fields[$i])) {
374 $fields[$i] = explode(',', $fields[$i]);
376 $enum_selected_count = count($fields[$i]);
377 if ($func_type == '=' && $enum_selected_count > 1) {
378 $func_type = $func[$i] = 'IN';
379 $parens_open = '(';
380 $parens_close = ')';
382 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
383 $func_type = $func[$i] = 'NOT IN';
384 $parens_open = '(';
385 $parens_close = ')';
387 } else {
388 $parens_open = '';
389 $parens_close = '';
391 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
392 for ($e = 1; $e < $enum_selected_count; $e++) {
393 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
396 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
399 } elseif ($fields[$i] != '') {
400 // For these types we quote the value. Even if it's another type (like INT),
401 // for a LIKE we always quote the value. MySQL converts strings to numbers
402 // and numbers to strings as necessary during the comparison
403 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
404 $quot = '\'';
405 } else {
406 $quot = '';
409 // LIKE %...%
410 if ($func_type == 'LIKE %...%') {
411 $func_type = 'LIKE';
412 $fields[$i] = '%' . $fields[$i] . '%';
414 if ($func_type == 'REGEXP ^...$') {
415 $func_type = 'REGEXP';
416 $fields[$i] = '^' . $fields[$i] . '$';
419 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
420 $func_type = str_replace(' (...)', '', $func_type);
422 // quote values one by one
423 $values = explode(',', $fields[$i]);
424 foreach ($values as &$value)
425 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
427 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
428 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
429 else
430 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
432 else {
433 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
436 } // end if
437 } // end for
439 if ($w) {
440 $sql_query .= ' WHERE ' . implode(' AND ', $w);
442 } // end if
444 if ($orderField != '--nil--') {
445 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
446 } // end if
448 require './sql.php';