2007-07-02 05:51:26 +00:00
< ? php
2009-07-11 19:45:57 +00:00
$where = ( isset ( $_GET [ " select " ]) ? ( count ( $_POST [ " check " ]) == 1 ? where_check ( $_POST [ " check " ][ 0 ]) : " " ) : where ( $_GET ));
2009-06-07 23:53:51 +00:00
$update = ( $where && ! $_POST [ " clone " ]);
2007-07-25 15:18:59 +00:00
$fields = fields ( $_GET [ " edit " ]);
foreach ( $fields as $name => $field ) {
2009-07-23 15:49:59 +00:00
if (( isset ( $_GET [ " default " ]) ? $field [ " auto_increment " ] || ereg ( 'text|blob' , $field [ " type " ]) : ! isset ( $field [ " privileges " ][ $update ? " update " : " insert " ])) || ! strlen ( adminer_field_name ( $field ))) {
2007-07-25 15:18:59 +00:00
unset ( $fields [ $name ]);
2007-07-09 06:12:22 +00:00
}
2007-07-05 10:41:53 +00:00
}
2008-10-01 11:39:13 +00:00
if ( $_POST && ! $error && ! isset ( $_GET [ " select " ])) {
2009-06-21 23:20:32 +00:00
$location = ( $_POST [ " insert " ] ? $_SERVER [ " REQUEST_URI " ] : $SELF . ( isset ( $_GET [ " default " ]) ? " table= " : " select= " ) . urlencode ( $_GET [ " edit " ])); // "insert" to continue edit or insert
2007-07-02 05:51:26 +00:00
if ( isset ( $_POST [ " delete " ])) {
2009-07-11 19:45:57 +00:00
query_redirect ( " DELETE FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE $where LIMIT 1 " , $location , lang ( 'Item has been deleted.' ));
2007-07-02 05:51:26 +00:00
} else {
2008-09-02 08:42:14 +00:00
$set = array ();
2007-07-09 06:12:22 +00:00
foreach ( $fields as $name => $field ) {
2007-07-09 14:45:11 +00:00
$val = process_input ( $name , $field );
2009-03-04 17:22:28 +00:00
if ( ! isset ( $_GET [ " default " ])) {
if ( $val !== false || ! $update ) {
2009-06-05 08:30:21 +00:00
$set [] = " \n " . idf_escape ( $name ) . " = " . ( $val !== false ? $val : " '' " );
2009-03-04 17:22:28 +00:00
}
} elseif ( $val !== false ) {
2009-03-09 15:17:36 +00:00
if ( $field [ " type " ] == " timestamp " && $val != " NULL " ) { //! doesn't allow DEFAULT NULL and no ON UPDATE
2009-06-05 08:30:21 +00:00
$set [] = " \n MODIFY " . idf_escape ( $name ) . " timestamp " . ( $field [ " null " ] ? " NULL " : " " ) . " DEFAULT $val " . ( $_POST [ " on_update " ][ bracket_escape ( $name )] ? " ON UPDATE CURRENT_TIMESTAMP " : " " );
2007-07-25 15:18:59 +00:00
} else {
2009-06-05 08:30:21 +00:00
$set [] = " \n ALTER " . idf_escape ( $name ) . ( $val == " NULL " ? " DROP DEFAULT " : " SET DEFAULT $val " );
2007-07-25 15:18:59 +00:00
}
2007-07-02 05:51:26 +00:00
}
}
2008-09-02 08:42:14 +00:00
if ( ! $set ) {
redirect ( $location );
}
2007-07-09 06:12:22 +00:00
if ( isset ( $_GET [ " default " ])) {
2008-09-02 08:42:14 +00:00
query_redirect ( " ALTER TABLE " . idf_escape ( $_GET [ " edit " ]) . implode ( " , " , $set ), $location , lang ( 'Default values has been set.' ));
2008-09-27 08:52:18 +00:00
} elseif ( $update ) {
2009-07-13 22:20:46 +00:00
query_redirect ( " UPDATE " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set ) . " \n WHERE $where\nLIMIT 1 " , $location , lang ( 'Item has been updated.' ));
2007-07-02 05:51:26 +00:00
} else {
2009-06-05 08:30:21 +00:00
query_redirect ( " INSERT INTO " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set ), $location , lang ( 'Item has been inserted.' ));
2007-07-02 05:51:26 +00:00
}
}
}
2009-07-11 19:45:57 +00:00
$table_name = adminer_table_name ( table_status ( $_GET [ " edit " ]));
page_header (
( isset ( $_GET [ " default " ]) ? lang ( 'Default values' ) : ( $_GET [ " where " ] || ( isset ( $_GET [ " select " ]) && ! $_POST [ " clone " ]) ? lang ( 'Edit' ) : lang ( 'Insert' ))),
$error ,
array (( isset ( $_GET [ " default " ]) ? " table " : " select " ) => array ( $_GET [ " edit " ], $table_name )),
$table_name
);
2007-07-02 05:51:26 +00:00
2008-07-10 15:39:24 +00:00
unset ( $row );
2009-06-07 23:53:51 +00:00
if ( $_POST [ " save " ]) {
2007-07-16 14:01:28 +00:00
$row = ( array ) $_POST [ " fields " ];
2007-07-02 05:51:26 +00:00
} elseif ( $where ) {
2007-07-06 13:48:25 +00:00
$select = array ();
2007-07-02 05:51:26 +00:00
foreach ( $fields as $name => $field ) {
2009-06-13 17:00:48 +00:00
if ( isset ( $field [ " privileges " ][ " select " ])) {
$select [] = ( $_POST [ " clone " ] && $field [ " auto_increment " ] ? " '' AS " : ( $field [ " type " ] == " enum " || $field [ " type " ] == " set " ? " 1* " . idf_escape ( $name ) . " AS " : " " )) . idf_escape ( $name );
2007-07-02 05:51:26 +00:00
}
}
2008-10-09 13:23:54 +00:00
$row = array ();
2007-07-10 13:30:42 +00:00
if ( $select ) {
2009-07-11 19:45:57 +00:00
$result = $dbh -> query ( " SELECT " . implode ( " , " , $select ) . " FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE $where LIMIT 1 " );
2007-07-16 14:01:28 +00:00
$row = $result -> fetch_assoc ();
2008-09-01 16:09:04 +00:00
$result -> free ();
2007-07-10 13:30:42 +00:00
}
2007-07-02 05:51:26 +00:00
}
?>
2007-07-09 14:45:11 +00:00
2007-07-09 06:12:22 +00:00
< form action = " " method = " post " enctype = " multipart/form-data " >
2007-07-02 05:51:26 +00:00
< ? php
2007-07-10 20:26:17 +00:00
if ( $fields ) {
2007-07-25 17:10:03 +00:00
unset ( $create );
2009-05-01 10:41:33 +00:00
echo " <table cellspacing='0'> \n " ;
2007-07-10 20:26:17 +00:00
foreach ( $fields as $name => $field ) {
2009-07-23 15:49:59 +00:00
echo " <tr><th> " . adminer_field_name ( $field );
2009-06-30 10:27:12 +00:00
$value = ( isset ( $row )
? ( strlen ( $row [ $name ]) && ( $field [ " type " ] == " enum " || $field [ " type " ] == " set " ) ? intval ( $row [ $name ]) : $row [ $name ])
2009-07-15 13:41:36 +00:00
: ( $_POST [ " clone " ] && $field [ " auto_increment " ] ? " " : ( isset ( $_GET [ " select " ]) ? false : $field [ " default " ]))
2009-06-07 23:53:51 +00:00
);
2009-07-17 14:06:30 +00:00
$function = ( $_POST [ " save " ] ? ( string ) $_POST [ " function " ][ $name ] : ( $where && $field [ " on_update " ] == " CURRENT_TIMESTAMP " ? " now " : ( $value === false ? null : ( isset ( $value ) ? '' : 'NULL' ))));
2009-07-23 14:42:38 +00:00
input ( $field , $value , $function );
2007-07-25 17:10:03 +00:00
if ( isset ( $_GET [ " default " ]) && $field [ " type " ] == " timestamp " ) {
if ( ! isset ( $create ) && ! $_POST ) {
2007-08-11 06:43:01 +00:00
//! disable sql_mode NO_FIELD_OPTIONS
2009-05-08 05:23:03 +00:00
$create = $dbh -> result ( $dbh -> query ( " SHOW CREATE TABLE " . idf_escape ( $_GET [ " edit " ])), 1 );
2007-07-25 17:10:03 +00:00
}
$checked = ( $_POST ? $_POST [ " on_update " ][ bracket_escape ( $name )] : preg_match ( " ~ \n \\ s* " . preg_quote ( idf_escape ( $name ), '~' ) . " timestamp.* on update CURRENT_TIMESTAMP~i " , $create ));
2009-07-11 20:30:40 +00:00
echo '<label><input type="checkbox" name="on_update[' . htmlspecialchars ( bracket_escape ( $name )) . ']" value="1"' . ( $checked ? ' checked="checked"' : '' ) . '>' . lang ( 'ON UPDATE CURRENT_TIMESTAMP' ) . '</label>' ;
2007-07-25 17:10:03 +00:00
}
2009-07-11 20:30:40 +00:00
echo " \n " ;
2007-07-02 05:51:26 +00:00
}
2007-07-10 20:26:17 +00:00
echo " </table> \n " ;
2007-07-02 05:51:26 +00:00
}
?>
2007-07-09 06:12:22 +00:00
< p >
2009-07-11 20:30:40 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " >
< input type = " hidden " name = " save " value = " 1 " >
2008-10-01 11:39:13 +00:00
< ? php
if ( isset ( $_GET [ " select " ])) {
2008-10-09 13:23:54 +00:00
hidden_fields ( array ( " check " => ( array ) $_POST [ " check " ], " clone " => $_POST [ " clone " ], " all " => $_POST [ " all " ]));
2008-10-01 11:39:13 +00:00
}
if ( $fields ) {
2009-07-11 20:30:40 +00:00
echo " <input type='submit' value=' " . lang ( 'Save' ) . " '> \n " ;
2009-06-07 23:24:37 +00:00
if ( ! isset ( $_GET [ " default " ]) && ! isset ( $_GET [ " select " ])) {
2009-07-11 20:30:40 +00:00
echo " <input type='submit' name='insert' value=' " . ( $update ? lang ( 'Save and continue edit' ) : lang ( 'Save and insert next' )) . " '> \n " ;
2009-06-07 23:24:37 +00:00
}
}
if ( $update ) {
2009-07-11 20:30:40 +00:00
echo " <input type='submit' name='delete' value=' " . lang ( 'Delete' ) . " ' $confirm > \n " ;
2009-06-07 23:24:37 +00:00
}
?>
2007-07-02 05:51:26 +00:00
</ form >