note 84962 added to function.mysql-query

From: Date: Thu, 07 Aug 2008 15:07:44 +0000
Subject: note 84962 added to function.mysql-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-143277@lists.php.net to get a copy of this message
The function formToDB($table, $exceptions = '', $sql_type = 'insert', $sql_condition = NULL) is not working properly. Problem with exceptional fields is there. I modified this function and now it is working properly. Here is the changed function - function formToDB($table, $exceptions = '', $sql_type = 'insert', $sql_condition = NULL) { // define some vars $fields = ''; $values = ''; // format input fields into sql foreach ($_POST as $field => $value) { if (!strstr($exceptions,$field)) { $value = mysql_real_escape_string($value); if ($sql_type == 'insert') { $fields .= "$field, "; $values .= "'$value', "; } else { $fields .= "$field = '$value', "; } } } // remove trailing ", " from $fields and $values $fields = preg_replace('/, $/', '', $fields); $values = preg_replace('/, $/', '', $values); // create sql statement if ($sql_type == 'insert') { $sql = "INSERT INTO $table ($fields) VALUES ($values)"; } elseif ($sql_type == 'update') { if (!isset($sql_condition)) { echo 'ERROR: You must enter a sql condition!'; exit; } $sql = "UPDATE $table SET $fields WHERE $sql_condition"; } else { echo 'ERROR: Invalid input for argument $sql_type: must be "insert" or "update"'; exit; } // execute sql if (mysql_query($sql)) { return true; } else { //echo mysql_error(); return false; } } ---- Server IP: 203.199.69.133 Probable Submitter: 122.168.34.164 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-query.php Edit -- https://master.php.net/note/edit/84962 Del: integrated -- https://master.php.net/note/delete/84962/integrated Del: useless -- https://master.php.net/note/delete/84962/useless Del: bad code -- https://master.php.net/note/delete/84962/bad+code Del: spam -- https://master.php.net/note/delete/84962/spam Del: non-english -- https://master.php.net/note/delete/84962/non-english Del: in docs -- https://master.php.net/note/delete/84962/in+docs Del: other reasons-- https://master.php.net/note/delete/84962 Reject -- https://master.php.net/note/reject/84962 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143277) next »