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