Define headers which to trust in configuration, thanks for help with this to Christia...
[phpmyadmin/crack.git] / pmd_relation_new.php
blob4a031e9c96ae48a657dec321d6a323423123d375
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 include_once 'pmd_session.php';
6 $die_save_pos = 0;
7 include_once 'pmd_save_pos.php';
8 require_once './libraries/relation.lib.php';
9 extract($_POST);
10 PMA_getRelationsParam();
12 $tables = PMA_DBI_get_tables_full($db, $T1);
13 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
14 $tables = PMA_DBI_get_tables_full($db, $T2);
15 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
17 if ($type_T1 == 'INNODB' and $type_T2 == 'INNODB') {
18 // relation exists?
19 $existrel_innodb = PMA_getForeigners($db, $T2, '', 'innodb');
20 if (isset($existrel_innodb[$F2])
21 && isset($existrel_innodb[$F2]['constraint'])) {
22 PMD_return(0,'ERROR : Relation already exists!');
24 // note: in InnoDB, the index does not requires to be on a PRIMARY
25 // or UNIQUE key
26 // improve: check all other requirements for InnoDB relations
27 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
28 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
29 while ($row = PMA_DBI_fetch_assoc($result))
30 $index_array1[$row['Column_name']] = 1;
31 PMA_DBI_free_result($result);
33 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
34 $index_array2 = array(); // will be used to emphasis prim. keys in the table view
35 while ($row = PMA_DBI_fetch_assoc($result))
36 $index_array2[$row['Column_name']] = 1;
37 PMA_DBI_free_result($result);
39 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
40 $upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
41 . ' ADD FOREIGN KEY ('
42 . PMA_backquote($F2) . ')'
43 . ' REFERENCES '
44 . PMA_backquote($db) . '.'
45 . PMA_backquote($T1) . '('
46 . PMA_backquote($F1) . ')';
48 if ($on_delete != 'nix') {
49 $upd_query .= ' ON DELETE ' . $on_delete;
51 if ($on_update != 'nix') {
52 $upd_query .= ' ON UPDATE ' . $on_update;
54 PMA_DBI_try_query($upd_query) or PMD_return(0,'ERROR : Relation not added!!!');
55 PMD_return(1,$strInnoDBRelationAdded);
57 } else {
58 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
59 $pk_array1 = array(); // will be use to emphasis prim. keys in the table view
60 while ($row = PMA_DBI_fetch_assoc($result)) {
61 if ($row['Key_name'] == 'PRIMARY') {
62 $pk_array1[$row['Column_name']] = 1;
65 PMA_DBI_free_result($result);
67 if (! isset($pk_array1[$F1])) {
68 PMD_return(0,'ERROR : Relation not added! First field is not Primary Key!');
70 $q = "INSERT INTO ".PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])."
71 (master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
72 VALUES ('".$db."','".$T2."','$F2','".
73 $db."','".$T1."','$F1')";
74 PMA_query_as_cu( $q , true, PMA_DBI_QUERY_STORE);// or PMD_return(0,'ERROR : Relation not added!');
76 PMD_return(1, $strInternalRelationAdded);
79 function PMD_return($b,$ret)
81 global $db,$T1,$F1,$T2,$F2;
82 die('<root act="relation_new" return="'.$ret.'" b="'.$b.'" DB1="'.$db.'" T1="'.$T1.'" F1="'.$F1.'" DB2="'.$db.'" T2="'.$T2.'" F2="'.$F2.'"></root>');