michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2003-10-25 23:18
Návrat hore
Odpovedať s citátom
|
triggers are kind of mystery to me. however i do not see no "return 'true';" nor "return 'false';" (which would stop the insert process.
some time ago, i made a litle patch to control/change the user input. a user defined function is eval-ed after reading the input. this has the same effect as before trigger, but you do not need that many small files.
other important change is that the insert query is constructed from an array completely after the before trigger.
the patch:
| Kód: |
--- phpMyEdit.class.-submited-output-control-260803.php Tue Aug 26 23:12:02 2003
+++ ../phpMyEdit-5.2/phpMyEdit.class.php Tue Sep 23 12:51:15 2003
@@ -20,6 +20,7 @@
*/
/* $Platon: phpMyEdit/phpMyEdit.class.php,v 1.94 2003/08/18 20:31:58 nepto Exp $ */
+/* version submitted to pme forum */
/* This is a generic table editing program. The table and fields to be
edited are defined in the calling program.
@@ -2230,35 +2231,50 @@
function do_add_record() /* {{{ */
{
// Preparing query
- $query = '';
$key_col_val = '';
- for ($k = 0; $k < $this->num_fds; $k++) {
- if ($this->processed($k)) {
- }
- }
- $vals_quoted = array();
$newvals = array();
- for ($k = 0; $k < $this->num_fds; $k++) {
+ for ($k = 0; $k < $this->num_fds; $k++) { // inserting cgi values into row[]
if ($this->processed($k)) {
- $fd = $this->fds[$k];
if ($this->readonly($k)) {
$fn = (string) @$this->fdd[$k]['default'];
} else {
$fn = $this->get_cgi_var($this->fds[$k]);
}
+ $newvals[$this->fds[$k]] = is_array($fn) ? join(',',$fn) : $fn;
if ($fd == $this->key) {
$key_col_val = addslashes($fn);
}
- if ($query == '') {
- $query = 'INSERT INTO '.$this->tb.' ('.$fd; // )
+ }
+ }
+ $row = $newvals;
+ $this->input_error = ''; // you can insert error message here
+ $row_current = array(); // array to be later inserted in form colname => value
+ for ($k = 0; $k < $this->num_fds; $k++) { // applying input_functions and creating row_current
+ if ($this->processed($k)) {
+ $fd = $this->fds[$k]; $name = $fd;
+ if ($this->col_has_input_function($k)) {
+ $value = $row[$fd];
+ $work = eval ("return ".$this->fdd[$k]['input_function'].";");
+ if ($this->input_error != '') {
+ echo $this->input_error;
+ return false;
+ }
+ if (is_array($work)) { echo ' array ';
+ foreach ( $work as $col_name => $value ) {
+ if ($value != NULL) {
+ $row_current[$col_name] = $value;
+ }
+ }
+ } else {
+ if ($work != NULL ) $row_current[$fd] = $work;
+ }
} else {
- $query .= ','.$fd;
+ if ($row[$fd] != NULL ) $row_current[$fd] = $row[$fd]; // MI added condition, important
+ echo $name."->".$row[$name];
}
- $newvals[$this->fds[$k]] = is_array($fn) ? join(',',$fn) : $fn;
- $vals_quoted[$k] = addslashes($newvals[$this->fds[$k]]);
- $vals_quoted[$k] = "'".$vals_quoted[$k]."'";
}
}
+ $newvals = $row_current; // now newvals has the values to be inserted, for compatibility
// Creating array of changed keys ($changed)
$changed = array_keys($newvals);
// Before trigger
@@ -2269,8 +2285,24 @@
}
}
// Real query (no additional query in this method)
- $query .= ') VALUES ('.join(',',$vals_quoted).')'; // )
+ $query = '';
+ foreach ($row_current as $col_name => $value ){
+ echo $col_name.'=>'.$value.'<br>';
+ if($value == NULL ) continue;
+ $value = "'".addslashes($value)."'";
+ if ($query == '') {
+ $query = 'INSERT INTO '.$this->tb.' (`'.$col_name.'`';
+ $query2 = ') VALUES ('.$value;
+ } else {
+ $query .= ', `'.$col_name.'`';
+ $query2 .= ', '.$value;
+ }
+ }
+ $query .= $query2; $query .= ')'; echo '<br>'.$query;
+ //$query .= ') VALUES ('.join(',',$vals_quoted).')'; // )
$res = $this->myquery($query, __LINE__);
+
+
$this->message = @mysql_affected_rows($this->dbh).' '.$this->labels['record added'];
if (! $res) {
return false;
|
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2003-10-25 23:19
Návrat hore
Odpovedať s citátom
|
sorry for duplication
anyway, insert function (for this patch defined as 'input_function' => 'my_function($value)' in your configuration file) is defining control per column, while insert trigger is defining it per table. in other notions, they should behave equally (i hope:)).
Naposledy upravil michal dňa 2003-10-25 23:28, celkom upravené 1 krát
|
nepto
Založený: 06.01.2003
Príspevky: 320
Bydlisko: Phoenix, Arizona (USA)
Zaslal: 2003-12-04 20:56
Návrat hore
Odpovedať s citátom
|
Michal,
There is no "return" statement, because there is "exit" statement. When entered data are not valid, Doug's trigger will simply print out the error message and exit immidiately. Than user have to go back and corrects data as required.
_________________ Ondrej Jombik (Nepto)
Visit my
homepage
, review my recent
CV
or
projects
and give me some
feedback
.
|
mcozeti
Založený: 12.03.2007
Príspevky: 2
Zaslal: 2007-03-13 01:35
Návrat hore
Odpovedať s citátom
|
| michal Napísal: |
triggers are kind of mystery to me. however i do not see no "return 'true';" nor "return 'false';" (which would stop the insert process.
some time ago, i made a litle patch to control/change the user input. a user defined function is eval-ed after reading the input. this has the same effect as before trigger, but you do not need that many small files.
other important change is that the insert query is constructed from an array completely after the before trigger.
the patch:
| Kód: |
--- phpMyEdit.class.-submited-output-control-260803.php Tue Aug 26 23:12:02 2003
+++ ../phpMyEdit-5.2/phpMyEdit.class.php Tue Sep 23 12:51:15 2003
@@ -20,6 +20,7 @@
*/
/* $Platon: phpMyEdit/phpMyEdit.class.php,v 1.94 2003/08/18 20:31:58 nepto Exp $ */
+/* version submitted to pme forum */
/* This is a generic table editing program. The table and fields to be
edited are defined in the calling program.
@@ -2230,35 +2231,50 @@
function do_add_record() /* {{{ */
{
// Preparing query
- $query = '';
$key_col_val = '';
- for ($k = 0; $k < $this->num_fds; $k++) {
- if ($this->processed($k)) {
- }
- }
- $vals_quoted = array();
$newvals = array();
- for ($k = 0; $k < $this->num_fds; $k++) {
+ for ($k = 0; $k < $this->num_fds; $k++) { // inserting cgi values into row[]
if ($this->processed($k)) {
- $fd = $this->fds[$k];
if ($this->readonly($k)) {
$fn = (string) @$this->fdd[$k]['default'];
} else {
$fn = $this->get_cgi_var($this->fds[$k]);
}
+ $newvals[$this->fds[$k]] = is_array($fn) ? join(',',$fn) : $fn;
if ($fd == $this->key) {
$key_col_val = addslashes($fn);
}
- if ($query == '') {
- $query = 'INSERT INTO '.$this->tb.' ('.$fd; // )
+ }
+ }
+ $row = $newvals;
+ $this->input_error = ''; // you can insert error message here
+ $row_current = array(); // array to be later inserted in form colname => value
+ for ($k = 0; $k < $this->num_fds; $k++) { // applying input_functions and creating row_current
+ if ($this->processed($k)) {
+ $fd = $this->fds[$k]; $name = $fd;
+ if ($this->col_has_input_function($k)) {
+ $value = $row[$fd];
+ $work = eval ("return ".$this->fdd[$k]['input_function'].";");
+ if ($this->input_error != '') {
+ echo $this->input_error;
+ return false;
+ }
+ if (is_array($work)) { echo ' array ';
+ foreach ( $work as $col_name => $value ) {
+ if ($value != NULL) {
+ $row_current[$col_name] = $value;
+ }
+ }
+ } else {
+ if ($work != NULL ) $row_current[$fd] = $work;
+ }
} else {
- $query .= ','.$fd;
+ if ($row[$fd] != NULL ) $row_current[$fd] = $row[$fd]; // MI added condition, important
+ echo $name."->".$row[$name];
}
- $newvals[$this->fds[$k]] = is_array($fn) ? join(',',$fn) : $fn;
- $vals_quoted[$k] = addslashes($newvals[$this->fds[$k]]);
- $vals_quoted[$k] = "'".$vals_quoted[$k]."'";
}
}
+ $newvals = $row_current; // now newvals has the values to be inserted, for compatibility
// Creating array of changed keys ($changed)
$changed = array_keys($newvals);
// Before trigger
@@ -2269,8 +2285,24 @@
}
}
// Real query (no additional query in this method)
- $query .= ') VALUES ('.join(',',$vals_quoted).')'; // )
+ $query = '';
+ foreach ($row_current as $col_name => $value ){
+ echo $col_name.'=>'.$value.'<br>';
+ if($value == NULL ) continue;
+ $value = "'".addslashes($value)."'";
+ if ($query == '') {
+ $query = 'INSERT INTO '.$this->tb.' (`'.$col_name.'`';
+ $query2 = ') VALUES ('.$value;
+ } else {
+ $query .= ', `'.$col_name.'`';
+ $query2 .= ', '.$value;
+ }
+ }
+ $query .= $query2; $query .= ')'; echo '<br>'.$query;
+ //$query .= ') VALUES ('.join(',',$vals_quoted).')'; // )
$res = $this->myquery($query, __LINE__);
+
+
$this->message = @mysql_affected_rows($this->dbh).' '.$this->labels['record added'];
if (! $res) {
return false;
|
|
Should I paste where? What does mean this (+/-)?
|
nepto
Založený: 06.01.2003
Príspevky: 320
Bydlisko: Phoenix, Arizona (USA)
Zaslal: 2007-03-13 05:32
Návrat hore
Odpovedať s citátom
|
That +/- is an unified diff.
However patch will certainly very old and not applicable.
_________________ Ondrej Jombik (Nepto)
Visit my
homepage
, review my recent
CV
or
projects
and give me some
feedback
.
|
|
Odoslať novú tému
Odpovedať na tému
|
|