I'm writing a mailing list manager in an effort to learn mysql and php and I've run into a bit of a problem extremely early on. . . I'm trying to get the database to be updateable by the user so that the user can add and remove addresses as needed. In order to do this I'm using autoincrement to make an id for each row, which is storing the values (id, name, address, status). The last being the users status on the list (banned, active, inactive). My question is this: is it possible to use autoincrement to generate the id's and still keep them sequential when I delete say, item 3 of 5. As it stands right now it just drops 3 and the ids go to 1,2,4,5, which throws the whole program for a loop. Here's all the code I've got so far (it's not very pretty, but I've just started with php so bear with me):
index.php (the main file): <html> <link rel=stylesheet href=standard.css> <head> <title>Mailing List Manager</title> </head> <body> <?php $j = 0; $k = 0; //connect to server localhost $link = mysql_connect("localhost") or die("Could not connect."); //select database test mysql_select_db("test") or die("Could not select database"); echo "<div>Currently Subscribed Addresses:</div>"; //select all rows in table list $query = "select * from list"; $result = mysql_query($query) or die("Query failed."); //select name column from table list $nameQuery = "select id from list"; $nameResult = mysql_query($nameQuery) or die("Query failed."); //display results of query echo "<form action=remove.php method=post name=formRemove>"; echo "<table>\n"; echo "\t<tr>\n\t\t<td colspan=2><div>ID</div></td>\n\t\t<td><div>Name</div></td>\n\t\t<td><div>Add ress</div></td>\n\t\t<td><div>Status</div></td>\n\t</tr>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { $nameLine = mysql_fetch_array($nameResult, MYSQL_NUM); echo "\t<tr>\n\t\t<td><input type=checkbox name=$nameLine[$j]><input type=hidden name=hidden$k value=$nameLine[$j]></td>\n"; $k = $k + 1; foreach ($line as $i) { echo "\t\t<td>$i</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; echo "<input type=hidden value=$k; name=max>"; echo "<div><input type=submit value='Remove Address' name=delete></div>"; echo "</form>"; //insert into test id mediumint not null auto_increment; //free results mysql_free_result($result); //closing connection mysql_close($link); ?> <div> <form action=add.php method=post> <?php echo "<input type=hidden value=$k; name=maxAdd>"; ?> <div>E-mail:<input type=text name=addressAdd></div> <div>Name..:<input type=text name=nameAdd></div> <input type=submit name=add value="Add an Address"> </form> </div> </body> </html> This isn't using autoincrement because I removed it at the last minute to try and make things more modular, but it's time for me to leave work, so I can't bother with it any more. . . add.php: <html> <body> <?php $num = $HTTP_POST_VARS["maxAdd"] + 1; $address = $HTTP_POST_VARS["addressAdd"]; $name = $HTTP_POST_VARS["nameAdd"]; echo "$address $name $num"; $dblocalhost = mysql_connect("localhost") or die("Could not connect."); mysql_select_db("test") or die("Could not find test."); $query = "insert into list values('$num','$name','$address','active');"; $result = mysql_query($query) or die("Could not finish query."); ?> <div> <form action=index.php> <input type=submit value="OK"> </form> </div> </body> </html> remove.php: <html> <head> <title>Remove Address</title> </head> <body> <?php $a = $HTTP_POST_VARS['max']; $a = $a + 1; $b = 1; $removeConnect = mysql_connect("localhost") or die("Could not connect."); mysql_select_db("test") or die("Could not select database."); $removeQuery = "select * from list"; $removeResult = mysql_query($removeQuery) or die("Query failed"); echo "<table>"; while ($removeLine = mysql_fetch_array($removeResult, MYSQL_ASSOC)) { $test = $HTTP_POST_VARS["$b"]; echo "<tr>"; foreach ($removeLine as $c) { echo "<td>$c</td>"; } if (isset($test)) { $d = $b - 1; $removeQuery = "delete from list where id = $d"; $removeRes = mysql_query($removeQuery) or die("Query failed."); echo "<td>Removed.</td>"; } echo "</tr>"; $b++; } echo "</table>"; ?> <form action=index.php> <input type=submit value=OK> </form> </body> </html> So: What am I doing right (if anything), and what am I doing wrong? Thanks in advance. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php