6 * Gets the values of the variables posted or sent to this script and displays
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
14 * A query has been submitted -> executes it, else displays the headers
16 if (isset($submit_sql)) {
17 $goto = 'db_details.php3';
18 $zero_rows = htmlspecialchars($strSuccess);
19 $sql_query = urldecode($encoded_sql_query);
20 if (get_magic_quotes_gpc()) {
21 $sql_query = addslashes($sql_query);
23 include('./sql.php3');
26 include('./header.inc.php3');
31 * Initializes some variables
33 if (empty($Columns)) {
34 $Columns = 3; // Initial number of columns
36 if (!isset($Add_Col)) {
39 if (!isset($Add_Row)) {
45 if (!isset($InsCol)) {
48 if (!isset($DelCol)) {
51 if (!isset($prev_Criteria)) {
54 // workaround for a PHP3 problem
55 if (!isset($Criteria)) {
58 for ($i = 0; $i < $Columns; $i++
) {
62 if (!isset($InsRow)) {
65 for ($i = 0; $i < $Columns; $i++
) {
69 if (!isset($DelRow)) {
72 for ($i = 0; $i < $Columns; $i++
) {
76 if (!isset($AndOrRow)) {
79 for ($i = 0; $i < $Columns; $i++
) {
83 if (!isset($AndOrCol)) {
86 for ($i = 0; $i < $Columns; $i++
) {
92 $col = $Columns +
$Add_Col;
96 $row = $Rows +
$Add_Row;
105 $tbl_result = mysql_list_tables($db);
106 $tbl_result_cnt = mysql_num_rows($tbl_result);
110 // The tables list sent by a previously submitted form
111 if (!empty($TableList)) {
112 for ($x = 0; $x < sizeof($TableList); $x++
) {
113 $tbl_names[$TableList[$x]] = ' selected="selected"';
117 // The tables list gets from MySQL
118 while ($i < $tbl_result_cnt) {
119 $tbl = mysql_tablename($tbl_result, $i);
120 $fld_results = mysql_list_fields($db, $tbl);
121 $fld_results_cnt = mysql_num_fields($fld_results);
124 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
125 $tbl_names[$tbl] = '';
127 $tbl_names[$tbl] = ' selected="selected"';
130 // The fields list per selected tables
131 if ($tbl_names[$tbl] == ' selected="selected"') {
132 $fld[$k++
] = PMA_backquote($tbl) . '.*';
133 while ($j < $fld_results_cnt) {
134 $fld[$k] = mysql_field_name($fld_results, $j);
135 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
140 mysql_free_result($fld_results);
144 mysql_free_result($tbl_result);
152 <!-- Query by example form
-->
153 <form action
="tbl_qbe.php3" method
="post">
154 <table border
="<?php echo $cfgBorder; ?>">
158 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfgThBgcolor; ?>">
159 <b
><?php
echo $strField; ?
> 
;: 
;</b
>
163 for ($x = 0; $x < $col; $x++
) {
164 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
166 <td align
="center" bgcolor
="<?php echo $cfgBgcolorOne; ?>">
167 <select style
="width: <?php echo $widem; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
168 <option value
=""></option
>
171 for ($y = 0; $y < sizeof($fld); $y++
) {
172 if ($fld[$y] == '') {
173 $sel = ' selected="selected"';
178 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
188 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
192 <td align
="center" bgcolor
="<?php echo $cfgBgcolorOne; ?>">
193 <select style
="width: <?php echo $widem; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
194 <option value
=""></option
>
197 for ($y = 0; $y < sizeof($fld); $y++
) {
198 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
199 $curField[$z] = urldecode($Field[$x]);
200 $sel = ' selected="selected"';
205 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
219 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfgThBgcolor; ?>">
220 <b
><?php
echo $strSort; ?
> 
;: 
;</b
>
224 for ($x = 0; $x < $col; $x++
) {
225 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
227 <td align
="center" bgcolor
="<?php echo $cfgBgcolorTwo; ?>">
228 <select style
="width: <?php echo $widem; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
229 <option value
=""></option
>
230 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
231 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
239 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
243 <td align
="center" bgcolor
="<?php echo $cfgBgcolorTwo; ?>">
244 <select style
="width: <?php echo $widem; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
245 <option value
=""></option
>
248 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
249 $curSort[$z] = $Sort[$x];
250 $sel = ' selected="selected"';
255 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
256 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
257 $curSort[$z] = $Sort[$x];
258 $sel = ' selected="selected"';
263 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
276 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfgThBgcolor; ?>">
277 <b
><?php
echo $strShow; ?
> 
;: 
;</b
>
281 for ($x = 0; $x < $col; $x++
) {
282 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
284 <td align
="center" bgcolor
="<?php echo $cfgBgcolorOne; ?>">
285 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
292 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
295 if (isset($Show[$x])) {
296 $checked = ' checked="checked"';
297 $curShow[$z] = $Show[$x];
302 <td align
="center" bgcolor
="<?php echo $cfgBgcolorOne; ?>">
303 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
312 <!-- Criteria row
-->
314 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfgThBgcolor; ?>">
315 <b
><?php
echo $strCriteria; ?
> 
;: 
;</b
>
319 for ($x = 0; $x < $col; $x++
) {
320 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
322 <td align
="center" bgcolor
="<?php echo $cfgBgcolorTwo; ?>">
323 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="" style
="width: <?php echo $widem; ?>" size
="20" />
330 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
333 if (isset($Criteria[$x])) {
334 if (get_magic_quotes_gpc()) {
335 $stripped_Criteria = stripslashes($Criteria[$x]);
337 $stripped_Criteria = $Criteria[$x];
340 if ((empty($prev_Criteria) ||
!isset($prev_Criteria[$x]))
341 ||
urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
342 $curCriteria[$z] = $stripped_Criteria;
343 $encoded_Criteria = urlencode($stripped_Criteria);
345 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
346 $encoded_Criteria = $prev_Criteria[$x];
349 <td align
="center" bgcolor
="<?php echo $cfgBgcolorTwo; ?>">
350 <input type
="hidden" name
="prev_Criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
351 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" style
="width: <?php echo $widem; ?>" size
="20" />
360 <!-- And/Or columns
and rows
-->
363 for ($y = 0; $y <= $row; $y++
) {
364 $bgcolor = ($y %
2) ?
$cfgBgcolorOne : $cfgBgcolorTwo;
365 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
366 $chk['or'] = ' checked="checked"';
370 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
371 <!-- Row controls
-->
372 <table bgcolor
="<?php echo $bgcolor; ?>">
374 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
375 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
376 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
378 <td align
="<?php echo $cell_align_right; ?>">
379 <b
><?php
echo $strAnd; ?
> 
;:</b
>
382 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
387 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
388 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
389 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
391 <td align
="<?php echo $cell_align_right; ?>">
392 <b
><?php
echo $strOr; ?
> 
;:</b
>
395 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
403 for ($x = 0; $x < $col; $x++
) {
404 if ($InsCol[$x] == 'on') {
406 $or = 'Or' . $w . '[' . $z . ']';
408 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
409 <textarea cols
="20" rows
="2" style
="width: <?php echo $widem; ?>" name
="<?php echo $or; ?>"></textarea
>
414 if ($DelCol[$x] == 'on') {
419 $or = 'Or' . $w . '[' . $z . ']';
421 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
422 <textarea cols
="20" rows
="2" style
="width: <?php echo $widem; ?>" name
="<?php echo $or; ?>"></textarea
>
434 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
438 if (isset($AndOrRow[$y])) {
439 $curAndOrRow[$w] = $AndOrRow[$y];
441 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
442 $chk['and'] = ' checked="checked"';
445 $chk['or'] = ' checked="checked"';
451 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
452 <!-- Row controls
-->
453 <table bgcolor
="<?php echo $bgcolor; ?>">
455 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
456 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
457 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
459 <td align
="<?php echo $cell_align_right; ?>">
460 <b
><?php
echo $strAnd; ?
> 
;:</b
>
463 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
467 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
468 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
469 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
471 <td align
="<?php echo $cell_align_right; ?>">
472 <b
><?php
echo $strOr; ?
> 
;:</b
>
475 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
482 for ($x = 0; $x < $col; $x++
) {
483 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
485 $or = 'Or' . $w . '[' . $z . ']';
487 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
488 <textarea cols
="20" rows
="2" style
="width: <?php echo $widem; ?>" name
="<?php echo $or; ?>"></textarea
>
493 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
499 if (!isset($
{$or})) {
502 if (!empty($
{$or}) && isset($
{$or}[$x])) {
503 if (get_magic_quotes_gpc()) {
504 $stripped_or = stripslashes($
{$or}[$x]);
506 $stripped_or = $
{$or}[$x];
512 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
513 <textarea cols
="20" rows
="2" style
="width: <?php echo $widem; ?>" name
="Or<?php echo $w . '[' . $z . ']'; ?>"><?php
echo htmlspecialchars($stripped_or); ?
></textarea
>
516 if (!empty($
{$or}) && isset($
{$or}[$x])) {
517 $
{'cur' . $or}[$z] = $
{$or}[$x];
530 <!-- Modify columns
-->
532 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfgThBgcolor; ?>">
533 <b
><?php
echo $strModify; ?
> 
;: 
;</b
>
537 for ($x = 0; $x < $col; $x++
) {
538 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
539 $curAndOrCol[$z] = $AndOrCol[$y];
540 if ($AndOrCol[$z] == 'or') {
541 $chk['or'] = ' checked="checked"';
544 $chk['and'] = ' checked="checked"';
548 <td align
="center" bgcolor
="<?php echo $cfgBgcolorTwo; ?>">
549 <b
><?php
echo $strOr; ?
> 
;:</b
>
550 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
551  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
552 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
554 <?php
echo $strQBEIns . "\n"; ?
>
555 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
556  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
557 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
564 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
568 if (isset($AndOrCol[$y])) {
569 $curAndOrCol[$z] = $AndOrCol[$y];
571 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
572 $chk['or'] = ' checked="checked"';
575 $chk['and'] = ' checked="checked"';
579 <td align
="center" bgcolor
="<?php echo $cfgBgcolorTwo; ?>">
580 <b
><?php
echo $strOr; ?
> 
;:</b
>
581 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
582  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
583 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
585 <?php
echo $strQBEIns . "\n"; ?
>
586 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
587  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
588 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
599 <!-- Other controls
-->
603 <table border
="0" align
="<?php echo $cell_align_left; ?>">
605 <td rowspan
="4" valign
="top">
606 <?php
echo $strUseTables; ?
> 
;:
608 <select name
="TableList[]" size
="7" multiple
="multiple">
610 while (list($key, $val) = each($tbl_names)) {
612 echo '<option value="' . urlencode($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
617 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
618 <input type
="hidden" value
="<?php echo $db; ?>" name
="db" />
619 <input type
="hidden" value
="<?php echo $z; ?>" name
="Columns" />
623 <input type
="hidden" value
="<?php echo $w; ?>" name
="Rows" />
624 <?php
echo $strAddDeleteRow; ?
> 
;:
625 <select size
="1" name
="Add_Row">
626 <option value
="-3">-3</option
>
627 <option value
="-2">-2</option
>
628 <option value
="-1">-1</option
>
629 <option value
="0" selected
="selected">0</option
>
630 <option value
="1">1</option
>
631 <option value
="2">2</option
>
632 <option value
="3">3</option
>
637 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
638 <?php
echo $strAddDeleteColumn; ?
> 
;:
639 <select size
="1" name
="Add_Col">
640 <option value
="-3">-3</option
>
641 <option value
="-2">-2</option
>
642 <option value
="-1">-1</option
>
643 <option value
="0" selected
="selected">0</option
>
644 <option value
="1">1</option
>
645 <option value
="2">2</option
>
646 <option value
="3">3</option
>
650 <!-- Generates a query
-->
651 <tr align
="center" valign
="top">
653 <input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" />
654 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
655 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
658 <!-- Executes a query
-->
659 <tr align
="center" valign
="top">
661 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
667 <!-- Displays the current query
-->
668 <?php
echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?
><br
/>
669 <textarea cols
="30" rows
="7" name
="sql_query">
674 if (!isset($qry_select)) {
677 for ($x = 0; $x < $col; $x++
) {
678 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
682 $qry_select .= $curField[$x];
686 if (!empty($qry_select)) {
687 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
688 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
692 if (!isset($TableList)) {
693 $TableList = array();
695 if (!isset($qry_from)) {
698 for ($x = 0; $x < sizeof($TableList); $x++
) {
702 $qry_from .= PMA_backquote(urldecode($TableList[$x]));
704 if (!empty($qry_from)) {
705 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
706 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
712 for ($x = 0; $x < $col; $x++
) {
713 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
714 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
716 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
717 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
722 if ($criteria_cnt > 1) {
723 $qry_where = '(' . $qry_where . ')';
726 // OR rows ${"cur".$or}[$x]
727 if (!isset($curAndOrRow)) {
728 $curAndOrRow = array();
730 for ($y = 0; $y <= $row; $y++
) {
734 for ($x = 0; $x < $col; $x++
) {
735 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
736 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
738 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
739 $qry_orwhere .= '(' . $curField[$x]
741 . (get_magic_quotes_gpc() ?
stripslashes($
{'curOr' . $y}[$x]) : $
{'curOr' . $y}[$x])
747 if ($criteria_cnt > 1) {
748 $qry_orwhere = '(' . $qry_orwhere . ')';
750 if (!empty($qry_orwhere)) {
752 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
757 if (!empty($qry_where) && $qry_where != '()') {
758 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
759 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
764 if (!isset($qry_orderby)) {
767 for ($x = 0; $x < $col; $x++
) {
768 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
769 $qry_orderby .= ', ';
771 if (!empty($curField[$x]) && !empty($curSort[$x])) {
772 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
776 if (!empty($qry_orderby)) {
777 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
778 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
782 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
792 * Displays the footer
794 require('./footer.inc.php3');