How to do error feedback when adding to indexed table?

From: Date: Sun, 24 Aug 2003 18:20:02 +0000
Subject: How to do error feedback when adding to indexed table?
Groups: php.db 
Request: Send a blank email to php-db+get-30075@lists.php.net to get a copy of this message
I'm not sure how to explain this, I'm totally new to MySQL and SQL as a whole, this is my first ever code using SQL at all...so plz bare with me here... I'm working with PHP 4.2.3 and MySQL 4.something (not certain what version the end-server runs). 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'. 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. I want it to give a feedback message that the data is already in the table, and bump ppl back to the edit form with that message. For the other things the script checks, all it does is load the variable $error with the error-message and set a phase-flag to the right value, and then the page generation part knows that there's been an error, and regenerates the entry form with the submitted data. So what I want, is to have the add data part to just change the two variables needed for the error system to see there's something wrong, but I've got no idea what I need to change in the code to do that. I've searched google and the archives, but come up with nothing... Here's the add-data part of the code...the variables in the comment is just so that I know what kind of data are in each variable, rather than what they're actually set to in the DB: elseif ($phase == 3) { /* num / id = int country = string nw = int gov = string strat = string ally = string spies = int totdef = int totoff = int owner = string */ mysql_connect($host,$username,$password); @mysql_select_db($database) or die('Unable to select database'); $query = "INSERT INTO ally2ally VALUES ('".$_POST['num']."','".$_POST['country']."','".$_POST['nw']."','".$_POST['gov']."','".$_POST['strat']."','".$_POST['spies']."','".$_POST['totdef']."','".$_POST['totoff']."','".$_POST['ally']."','".$_POST['owner']."')"; mysql_query($query); mysql_close(); } the error message part is simply this: <?php // if there is an error in the submitted data, display the error message if (isset($error)) { ?> <table> <tr>
    <td><?php echo($error); ?></td>
</tr> </table><br> <br> <?php } ?> 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). TIA Rene -- Rene Brehmer aka Metalbunny http://metalbunny.net/ References, tools, and other useful stuff...

« previous php.db (#30075) next »