|
Posted by Breklin on 10/17/06 20:59
Very good. Glad we are on the same page. ;)
Steve wrote:
> "Breklin" <breklin@sbcglobal.net> wrote in message
> news:YNaZg.14902$vJ2.2648@newssvr12.news.prodigy.com...
> | Steve, If you know so much, why don't just help her?? Jesus. However,
> | thank you. You taught me something I can take with me about the
> | ID/Auto_increment deal. Much appreciated.
>
>
> i have to laugh, breklin. i'm not trying to bait you. ;^)
>
> my first post to her was a chunk of template code that will suffice for her
> needs nicely if she just changes the appropriate sql statements to match her
> table structure. then in came ikciu calling her stupid which got us off in
> another tangent.
>
> what did i teach you btw?
>
> | Linda,
> |
> | Run a select statement using some identifiable value in your form to
> | check for duplicates. Perhaps the Name of the item or your product code,
> | etc.
>
> exactly...there you go!
>
> | Using that, you can see if any rows were returned. If not, then you can
> | proceed with your insert query.
>
>
> viola!
>
>
> | See example:
> | _______________________________________________________
> | $is_error_exists = false;
> |
> | $product_id = htmlspecialchars($_POST['product_id_field'])
> |
> | $query = "SELECT product_id, name
> | FROM table_name
> | WHERE product_id = $product_id";
> |
> | $result = mysql_query($query);
> |
> | $numrows = mysql_num_rows($result);
> |
> | if($numrows == 0)
> | // Execute insert query here
> | else
> | $is_error_exists = true;
> |
> | if($is_error_exists)
> | echo "There is a record for this product already in the database.";
> |
> | ___________________________________________________________
> |
> | Now, if Steve has any better ideas to help you instead of nit-picking my
> | code in lieu of his preference, please allow him to take it away! ;)
>
> ok...sorry to be so picky. i do have to point out that since product id is
> user generated, she does run the risk of overwritting data if she doesn't
> check correctly when editing a record. meaning, that the original product id
> must not be changed to an already existing product id....hence, an
> overwrite. again, the example i gave covers this possiblity as well.
>
> | There are many ways to do things, Linda. Just find your own special way
> | and learn to develop it into a clean style along the way.
> |
> | I personally use MYSQL wrapper classes to execute my queries. They are
> | very neat and don't clutter your code. You agree, Stevo?
>
>
> i agree completely...and don't mean to piss you off either. here's my
> wrapper for mysql...it is implemented in a shared interface...but that's a
> bit ot for linda, i think (assumes php 5):
>
> <?
> class db
> {
> static private $_instance = null;
> static private $_lastStatement = '';
>
> private function __clone(){}
>
> private function __construct(){}
>
> static function connect($server, $user, $password, $catalog = null)
> {
> try
> {
> mysql_connect($server, $user, $password);
> if (!is_null($catalog)){ mysql_select_db($catalog); }
> } catch (exception $ex) {
> print "<pre>\r\n" . $ex->getMessage() . "\r\n" .
> ' in file ' . $ex->getFile() . "\r\n" .
> ' on line ' . $ex->getLine() . "\r\n" .
> '</pre>';
> return false;
> }
> return true;
> }
>
> static function getInstance()
> {
> if (is_null(self::$_instance)){ self::$_instance = new db(); }
> return self::$_instance;
> }
>
> static function getLastStatement(){ return self::$_lastStatement; }
>
> static function decode($string)
> {
> $translation = get_html_translation_table(HTML_ENTITIES);
> $translation = array_flip($translation);
> $string = strtr($string, $translation);
> return $string;
> }
>
> static function describe($table)
> {
> $columns = array();
> $records = self::execute('DESCRIBE ' . $table);
> foreach ($records as $record)
> {
> foreach ($record as $column => $property)
> {
> if ($column == 'FIELD'){ continue; }
> $columns[$record['FIELD']][$column] = $property;
> }
> }
> return $columns;
> }
>
> static function encode($string)
> {
> $translation = get_html_translation_table(HTML_ENTITIES);
> $string = strtr($string, $translation);
> return $string;
> }
>
> static function execute($sql, $decode = false, $returnNewId = false)
> {
> self::$_lastStatement = $sql;
> $array = array();
> $key = 0;
> $records = mysql_query($sql);
> $fieldCount = @mysql_num_fields($records);
> $translation = get_html_translation_table(HTML_ENTITIES);
> $translation = array_flip($translation);
> while ($row = @mysql_fetch_array($records, MYSQL_NUM))
> {
> for ($i = 0; $i < $fieldCount; $i++)
> {
> $value = $row[$i];
> if ($decode){ $value = strtr($value, $translation); }
> $array[$key][strtoupper(@mysql_field_name($records, $i))] = $value;
> }
> $key++;
> }
> if ($returnNewId)
> {
> $array = array();
> $array[0]['ID'] = mysql_insert_id();
> }
> @mysql_free_result($records);
> return $array;
> }
>
> static function prepare($string, $encode = false)
> {
> if ($encode){ $string = self::encode($string); }
> $string = stripslashes(str_replace("'", "''", $string));
> return $string;
> }
> }
> ?>
>
>
>
Navigation:
[Reply to this message]
|