Re: How to do error feedback when adding to indexed table?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry if I was being vague. But naturally, about half an hour after I posted this, I found a simple and fool-proof way to do what I wanted: Simply just have MySQL lookup the ID after all the other error checking was done, like this:

  if (! isset($error)) {
  // DB check to ensure number is not already in DB
  // only do this if no other errors are present, because it consumes more
  // resources and time.
    $link = mysql_connect($host,$username,$password)
        or die('Could not connect : '.mysql_error());
    // echo('Connected successfully');
    mysql_select_db($database) or die('Could not select database');

    $query = 'SELECT * FROM ally2ally WHERE id='.$_POST['num'];
    $result = mysql_query($query);
    $numrows = mysql_numrows($result);

if ($numrows > 0) {
$error = 'The country number you entered already exists in the database. To edit an existing entry,
please use the edit tool/function.';
}


Well, this is a simple way to me atleast. This is run after initial prost procedure, and after all the other error checkers, so that it can bump it back to the edit form without going through the preview first.

Thus I managed to figure it out on myself, although going about it quite differently from what I originally thought I had to. So, I'm so sorry for wasting everybody's time with this.

Should perhaps be said that the "edit tool" mentioned in the code above isn't coded yet, so I might come back to you nice people once I get to that.

But, to answer Sebastian's questions:

At 19:15 24-08-2003, Sebastian Haag wrote:
> For a game clan I'm in (MPE 1A in Earth: 2025 - 1a), I'm working on a DB
> to
> store some rather game-specific info. It's only a single table in the DB,
> but the table is indexed by the field 'id'.
>

Is that your Primary Key? Does this ID have a meaning to you, or is it
random?

Yes it is primary, and yes it has meaning. Each player in the game have a single country. Each country have a unique number, generated by the game. We use these numbers to indetify the countries, and thus it's only natural to use it for the same purpose in my database.


> Whenever someone wants to add data to the table, they fill out a form, hit
> submit and either get an error about wrong or missing data, or if ok get a
> preview before being able to finally add it to the table. As the script is
> now, it will attempt to add the data to the table, but if already there,
> nothing happens, and there's no response to the user about it.
>

Do you mean if the ID is already there?

Yes.


> The rest of the code is not essential in this aspect. I just need some
> help
> to figure out how to make the script tell the user that it can't add the
> data because the ID is already in the database...(that's the first field
> btw).

I'm still not sure what exactly you want to do. But usually you make your
ID an AUTO_INCREMENT. That way you don't have to worry about giving the
same ID twice.

I am aware that :-). Problem here is that the information stored in the database must match the content of the game, and therefor also use the same ID numbers. I should probably have mentioned that in my initial posting, sorry. The country we store information about, may only exist once in the database, therefor I use the country's number as ID. If I used an auto-incremented ID, it would be possible to enter the same information as many times as one would like, which is not desirable.


If you don't know the game, I do realise that understanding what I talk about may be a bit difficult. I apologize for that. But if anyone have anymore questions to this project, I'll happily ask them. If it's about the game, better keep it offlist, thank you.

Thanks for the help

Rene

--
Rene Brehmer
aka Metalbunny

http://metalbunny.net/
References, tools, and other useful stuff...

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux