Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: tbl_select.php,v 2.37 2006/01/17 17:02:29 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5  
6 /**
7 * Gets some core libraries
8 */
9 require_once('./libraries/common.lib.php');
10 require_once('./libraries/relation.lib.php'); // foreign keys
11 require_once('./libraries/mysql_charsets.lib.php');
12  
13 if ( $GLOBALS['cfg']['PropertiesIconic'] == true ) {
14 $titles['Browse'] =
15 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
16 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
17 .$strBrowseForeignValues . '" />';
18  
19 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
20 $titles['Browse'] .= $strBrowseForeignValues;
21 }
22 } else {
23 $titles['Browse'] = $strBrowseForeignValues;
24 }
25  
26 /**
27 * Not selection yet required -> displays the selection form
28 */
29 if (!isset($param) || $param[0] == '') {
30 // Gets some core libraries
31 require_once('./libraries/tbl_properties_common.php');
32 //$err_url = 'tbl_select.php' . $err_url;
33 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
34  
35 /**
36 * Gets tables informations
37 */
38 require_once('./libraries/tbl_properties_table_info.inc.php');
39  
40 /**
41 * Displays top menu links
42 */
43 require_once('./libraries/tbl_properties_links.inc.php');
44  
45 if (!isset($goto)) {
46 $goto = $GLOBALS['cfg']['DefaultTabTable'];
47 }
48 // Defines the url to return to in case of error in the next sql statement
49 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
50  
51 // Gets the list and number of fields
52 $result = PMA_DBI_query('SHOW' . (PMA_MYSQL_INT_VERSION >= 40100 ? ' FULL' : '') . ' FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
53 $fields_cnt = PMA_DBI_num_rows($result);
54 // rabue: we'd better ensure, that all arrays are empty.
55 $fields_list = $fields_null = $fields_type = $fields_collation = array();
56 while ($row = PMA_DBI_fetch_assoc($result)) {
57 $fields_list[] = $row['Field'];
58 $type = $row['Type'];
59 // reformat mysql query output - staybyte - 9. June 2001
60 if (strncasecmp($type, 'set', 3) == 0
61 || strncasecmp($type, 'enum', 4) == 0) {
62 $type = str_replace(',', ', ', $type);
63 } else {
64  
65 // strip the "BINARY" attribute, except if we find "BINARY(" because
66 // this would be a BINARY or VARBINARY field type
67 if (!preg_match('@BINARY[\(]@i', $type)) {
68 $type = preg_replace('@BINARY@i', '', $type);
69 }
70 $type = preg_replace('@ZEROFILL@i', '', $type);
71 $type = preg_replace('@UNSIGNED@i', '', $type);
72  
73 $type = strtolower($type);
74 }
75 if (empty($type)) {
76 $type = '&nbsp;';
77 }
78 $fields_null[] = $row['Null'];
79 $fields_type[] = $type;
80 $fields_collation[] = PMA_MYSQL_INT_VERSION >= 40100 && !empty($row['Collation']) && $row['Collation'] != 'NULL'
81 ? $row['Collation']
82 : '';
83 } // end while
84 PMA_DBI_free_result($result);
85 unset($result, $type);
86  
87 // <markus@noga.de>
88 // retrieve keys into foreign fields, if any
89 $cfgRelation = PMA_getRelationsParam();
90 // check also foreigners even if relwork is FALSE (to get
91 // foreign keys from innodb)
92 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
93 $foreigners = PMA_getForeigners($db, $table);
94 ?>
95 <script type="text/javascript" language="javascript">
96 // <![CDATA[
97 function PMA_tbl_select_operator(f, index, multiple) {
98 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
99 <?php
100 reset( $GLOBALS['cfg']['UnaryOperators'] );
101 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
102 echo ' case "' . $operator . "\":\r\n";
103 }
104 ?>
105 bDisabled = true;
106 break;
107  
108 default:
109 bDisabled = false;
110 }
111 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
112 }
113 // ]]>
114 </script>
115 <form method="post" action="tbl_select.php" name="insertForm">
116 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
117 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
118 <input type="hidden" name="back" value="tbl_select.php" />
119  
120 <fieldset id="fieldset_table_search">
121  
122 <fieldset id="fieldset_select_fields">
123 <legend><?php echo $strSelectFields; ?></legend>
124 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
125 multiple="multiple">
126 <?php
127 // Displays the list of the fields
128 foreach ( $fields_list as $each_field ) {
129 echo ' '
130 .'<option value="' . htmlspecialchars( $each_field ) . '"'
131 .' selected="selected">' . htmlspecialchars( $each_field )
132 .'</option>' . "\n";
133 }
134 ?>
135 </select>
136 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
137 <label for="oDistinct">DISTINCT</label>
138 </fieldset>
139  
140 <fieldset id="fieldset_limit_rows">
141 <legend><?php echo $strLimitNumRows; ?></legend>
142 <input type="text" size="4" name="session_max_rows"
143 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
144 </fieldset>
145  
146 <fieldset id="fieldset_display_order">
147 <legend><?php echo $strDisplayOrder; ?></legend>
148 <select name="orderField" style="vertical-align: middle">
149 <option value="--nil--"></option>
150 <?php
151 foreach ( $fields_list as $each_field ) {
152 echo ' '
153 .'<option value="' . htmlspecialchars( $each_field ) . '">'
154 .htmlspecialchars( $each_field ) . '</option>' . "\n";
155 } // end for
156 ?>
157 </select>
158  
159 <div class="formelement">
160 <input type="radio" name="order" value="ASC" checked="checked" id="sortASC" />
161 <label for="sortASC"><?php echo $strAscending; ?></label>
162 </div>
163  
164 <div class="formelement">
165 <input type="radio" name="order" value="DESC" id="sortDESC" />
166 <label for="sortDESC"><?php echo $strDescending; ?></label>
167 </div>
168 </fieldset>
169  
170 <br class="clearfloat" />
171 <?php echo $strAddSearchConditions; ?>
172 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
173  
174 <input type="text" name="where" class="textfield" size="64" />
175  
176 </fieldset>
177 <fieldset class="tblFooters">
178 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
179 </fieldset>
180  
181 <fieldset id="fieldset_table_qbe">
182 <legend><?php echo '<em>' . $strOr . '</em> ' . $strDoAQuery; ?></legend>
183 <table class="data">
184 <thead>
185 <tr><th><?php echo $strField; ?></th>
186 <th><?php echo $strType; ?></th>
187 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<th>' . $strCollation . '</th>' . "\n" : ''; ?>
188 <th><?php echo $strOperator; ?></th>
189 <th><?php echo $strValue; ?></th>
190 </tr>
191 </thead>
192 <tbody>
193 <?php
194 $odd_row = true;
195 for ($i = 0; $i < $fields_cnt; $i++) {
196 ?>
197 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
198 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
199 <td><?php echo $fields_type[$i]; ?></td>
200 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<td>'
201 . $fields_collation[$i] . '</td>' . "\n" : ''; ?>
202 <td><select name="func[]">
203 <?php
204 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
205 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
206 echo "\n" . ' '
207 . '<option value="' . htmlspecialchars($fc) . '">'
208 . htmlspecialchars($fc) . '</option>';
209 }
210 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
211 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
212 echo "\n" . ' '
213 . '<option value="' . htmlspecialchars($fc) . '">'
214 . htmlspecialchars($fc) . '</option>';
215 }
216 } else {
217 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
218 echo "\n" . ' '
219 . '<option value="' . htmlspecialchars($fc) . '">'
220 . htmlspecialchars($fc) . '</option>';
221 }
222 } // end if... else...
223 if ($fields_null[$i]) {
224 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
225 echo "\n" . ' '
226 . '<option value="' . htmlspecialchars($fc) . '">'
227 . htmlspecialchars($fc) . '</option>';
228 }
229 }
230 ?>
231  
232 </select>
233 </td>
234 <td>
235 <?php
236 // <markus@noga.de>
237 $field = $fields_list[$i];
238  
239 // do not use require_once here
240 require('./libraries/get_foreign.lib.php');
241  
242 // we got a bug report: in some cases, even if $disp is true,
243 // there are no rows, so we add a fetch_array
244  
245 if ($foreigners && isset($foreigners[$field]) && isset($disp_row) && is_array($disp_row)) {
246 // f o r e i g n k e y s
247 echo ' <select name="fields[' . $i . ']">' . "\n";
248 // go back to first row
249  
250 // here, the 4th parameter is empty because there is no current
251 // value of data for the dropdown (the search page initial values
252 // are displayed empty)
253 echo PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display,
254 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
255 echo ' </select>' . "\n";
256 } elseif (isset($foreign_link) && $foreign_link == true) {
257 ?>
258 <input type="text" name="fields[<?php echo $i; ?>]"
259 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
260 class="textfield" />
261 <script type="text/javascript" language="javascript">
262 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>');
263 </script>
264 <?php
265 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
266 // e n u m s
267 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
268 $cnt_enum_value = count($enum_value);
269 echo ' <select name="fields[' . $i . '][]"'
270 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
271 for ($j = 0; $j < $cnt_enum_value; $j++) {
272 echo ' <option value="' . $enum_value[$j] . '">'
273 . $enum_value[$j] . '</option>';
274 } // end for
275 echo ' </select>' . "\n";
276 } else {
277 // o t h e r c a s e s
278 echo ' <input type="text" name="fields[' . $i . ']"'
279 .' size="40" class="textfield" />' . "\n";
280 }
281  
282 ?>
283 <input type="hidden" name="names[<?php echo $i; ?>]"
284 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
285 <input type="hidden" name="types[<?php echo $i; ?>]"
286 value="<?php echo $fields_type[$i]; ?>" />
287 <input type="hidden" name="collations[<?php echo $i; ?>]"
288 value="<?php echo $fields_collation[$i]; ?>" />
289 </td>
290 </tr>
291 <?php
292 } // end for
293 ?>
294 </tbody>
295 </table>
296 </fieldset>
297 <fieldset class="tblFooters">
298 <input type="hidden" name="max_number_of_fields"
299 value="<?php echo $fields_cnt; ?>" />
300 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
301 </fieldset>
302 </form>
303 <?php
304 require_once('./libraries/footer.inc.php');
305 }
306  
307  
308 /**
309 * Selection criteria have been submitted -> do the work
310 */
311 else {
312 // Builds the query
313  
314 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
315  
316 // if all fields were selected to display, we do a SELECT *
317 // (more efficient and this helps prevent a problem in IE
318 // if one of the rows is edited and we come back to the Select results)
319  
320 if (count($param) == $max_number_of_fields) {
321 $sql_query .= '* ';
322 } else {
323 $param = PMA_backquote( $param );
324 $sql_query .= implode( ', ', $param );
325 unset( $param );
326 } // end if
327  
328 $sql_query .= ' FROM ' . PMA_backquote($table);
329  
330 // The where clause
331 if (trim($where) != '') {
332 $sql_query .= ' WHERE ' . $where;
333 } else {
334 $w = $charsets = array();
335 $cnt_func = count($func);
336 reset($func);
337 while (list($i, $func_type) = each($func)) {
338 if (PMA_MYSQL_INT_VERSION >= 40100) {
339 list($charsets[$i]) = explode('_', $collations[$i]);
340 }
341 if (@$GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
342 $fields[$i] = '';
343 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type;
344  
345 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
346 if (!empty($fields[$i])) {
347 if (!is_array($fields[$i])) {
348 $fields[$i] = explode(',', $fields[$i]);
349 }
350 $enum_selected_count = count($fields[$i]);
351 if ($func_type == '=' && $enum_selected_count > 1) {
352 $func_type = $func[$i] = 'IN';
353 $parens_open = '(';
354 $parens_close = ')';
355  
356 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
357 $func_type = $func[$i] = 'NOT IN';
358 $parens_open = '(';
359 $parens_close = ')';
360  
361 } else {
362 $parens_open = '';
363 $parens_close = '';
364 }
365 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
366 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
367 $enum_where = 'CONVERT(_utf8 ' . $enum_where . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
368 }
369 for ($e = 1; $e < $enum_selected_count; $e++) {
370 $enum_where .= ', ';
371 $tmp_literal = '\'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
372 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
373 $tmp_literal = 'CONVERT(_utf8 ' . $tmp_literal . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
374 }
375 $enum_where .= $tmp_literal;
376 unset($tmp_literal);
377 }
378  
379 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
380 }
381  
382 } elseif ($fields[$i] != '') {
383 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i])) {
384 $quot = '\'';
385 } else {
386 $quot = '';
387 }
388  
389 // Make query independant from the selected connection charset.
390 // But if the field's type is VARBINARY, it has no charset
391 // and $charsets[$i] is empty, so we cannot generate a CONVERT
392  
393 if (PMA_MYSQL_INT_VERSION >= 40101 && !empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) {
394 $prefix = 'CONVERT(_utf8 ';
395 $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
396 } else {
397 $prefix = $suffix = '';
398 }
399  
400 // LIKE %...%
401 if ($func_type == 'LIKE %...%') {
402 $func_type = 'LIKE';
403 $fields[$i] = '%' . $fields[$i] . '%';
404 }
405 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix;
406  
407 } // end if
408 } // end for
409  
410 if ($w) {
411 $sql_query .= ' WHERE ' . implode(' AND ', $w);
412 }
413 } // end if
414  
415 if ($orderField != '--nil--') {
416 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
417 } // end if
418 include('./sql.php');
419 }
420  
421 ?>