You are here: Re: on input form showing error message for duplicate entry. « All PHP « IT news, forums, messages
Re: on input form showing error message for duplicate entry.

Posted by Breklin on 10/17/06 20:11

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.

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.

Using that, you can see if any rows were returned. If not, then you can
proceed with your insert query.

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! ;)

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?

Steve wrote:
> "Breklin" <breklin@sbcglobal.net> wrote in message
> news:vtaZg.632$T_1.76@newssvr14.news.prodigy.com...
> | Linda,
> |
> | This is how I would write that:
> |
> | [php]
> | <?php
> |
> | //login details
> |
> |
> | @mysql_connect($host,$user,$password)
> | or die("Unable to connect with server!");
> | @mysql_select_db($database)
> | or die("Unable to connect database, please try later!");
> |
> | -- No need for a form set ID value. Use the Auto-Increment feature in
> MySQL
> | -- I have never used the mysql_real_escape_string function. A variation is
> below.
> |
> | // Set Form Data into PHP Vars and format against injection attack
> | $name = htmlspecialchars($_POST['name']);
> | $info = htmlspecialchars($_POST['info']);
> | $pic = htmlspecialchars($_POST['pic']);
> | $lg_pic = htmlspecialchars($_POST['lg_pic']);
> |
> | -- QUESTION: What data time is 'Price': int, varchar, float, etc?
> |
> | $price = htmlspecialchars($_POST['price']);
> |
> | -- When using this type of insert, you must make sure you are placing data
> values in every field of the table. If you are not listing even one
> field in the VALUES portion of the query, you will get a query error.
> |
> | $query = "INSERT INTO floral VALUES
> ('','$name','$info','$pic','$lg_pic','$price')";
>
>
> NOT true. (but not to be too picky)
>
> INSERT INTO florals
> (
> name ,
> info ,
> pic ,
> lg_pic ,
> price
> )
> VALUES
> (
> '$name' ,
> '$info' ,
> '$pic, ,
> '$lg_pic' ,
> '$price'
> )
>
> will insert just fine without having to put '' for the id. PLUS it is *bad*
> form to have any sql statement on one line (my opinion). AND (common
> opinion), all inserts and selects should *always* list the columns being
> updated. the above sql is much more manageable than the latter...you can see
> exactly what columns are being used and their associated php variables at a
> glance.
>
> i have to say though, either way, this does not address her
> problem/question. how does she detect the insertion of DUPLICATE data and if
> duplicated, how does she display an error to that effect to the user?
>
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация