joshgarrod Posted June 26, 2007 Share Posted June 26, 2007 Hi, I have a situation, when we add a new stock item people forget what the next id number to be assigned is. Is there a way, (im sure there is) to see what the next id to be assigned is and automatically enter it, here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Add stock</title> </head> <body> <font face="Arial, Helvetica, sans-serif"> <? # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> <HTML> <HEAD> </font> <TITLE>Add stock</TITLE> <font face="Arial, Helvetica, sans-serif"> </HEAD> <BODY BGCOLOR="#FFFFFF"> </font> <P><FONT SIZE=5 face="Arial, Helvetica, sans-serif"><B> Add stock </B> </FONT></P> <font face="Arial, Helvetica, sans-serif"> <? # this is processed when the form is submitted # back on to this page (POST METHOD) if ($REQUEST_METHOD=="POST") { # double-up apostrophes $StockDescription = str_replace("'","''",$StockDescription); $StockNumber = str_replace("'","''",$StockNumber); # setup SQL statement $SQL = " INSERT INTO stock "; $SQL = $SQL . " (ID, StockNumber, ItemPrice, StockDescription) VALUES "; $SQL = $SQL . " ('$ID', '$StockNumber','$ItemPrice','$StockDescription') "; #execute SQL statement $result = mysql_db_query($db,"$SQL",$cid); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P><B>New stock item added</B></P>\n"); } ?> </font> <FORM NAME="fa" ACTION="addStock.php" METHOD="POST"> <TABLE> <TR><TD><font face="Arial, Helvetica, sans-serif"><B>Stock ID: </B> </font></TD><TD><font face="Arial, Helvetica, sans-serif"> <INPUT TYPE="text" NAME="ID" SIZE=40> </font></TD></TR> <TR><TD><font face="Arial, Helvetica, sans-serif"><B>Stock Number:</B> </font></TD><TD><font face="Arial, Helvetica, sans-serif"> <INPUT TYPE="text" NAME="StockNumber" SIZE=40> </font></TD></TR> <TR><TD><font face="Arial, Helvetica, sans-serif"><B>Item Price: </B> </font></TD><TD><font face="Arial, Helvetica, sans-serif"> <INPUT TYPE="text" NAME="ItemPrice" VALUE="£" SIZE=40> </font></TD></TR> <TR><TD VALIGN=TOP><font face="Arial, Helvetica, sans-serif"><B>Description: </B> </font></TD><TD><font face="Arial, Helvetica, sans-serif"> <textarea name="StockDescription" rows=5 cols=40></textarea> </font></TD> </TR> <TR><TH COLSPAN=2><P><font face="Arial, Helvetica, sans-serif"> <INPUT TYPE="submit" VALUE="Add stock"> </font></P></TH></TR> </TABLE> <p align="center"><font face="Arial, Helvetica, sans-serif"><a href="sparesAdminPage.html">Back</a></font></p> </FORM> <font face="Arial, Helvetica, sans-serif"> <? mysql_close($cid); ?> </font> </BODY> </HTML> </body> </html> Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/ Share on other sites More sharing options...
Solarpitch Posted June 26, 2007 Share Posted June 26, 2007 I take it your talking about from a database field. Just do a select like.. $query = ("SELECT MAX(stock_id) `stock_id` FROM `products`"); ..... $row = $result->fetch_assoc(); $get_val = $row['stock_id']; $get_val++; echo $get_val; Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283122 Share on other sites More sharing options...
joshgarrod Posted June 26, 2007 Author Share Posted June 26, 2007 ok, so what does that replace or where go please? Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283124 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Your much better off making your ID filed an auto incrementing field on the datbase end and letting mysql deal with it. Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283129 Share on other sites More sharing options...
Solarpitch Posted June 26, 2007 Share Posted June 26, 2007 Yeah . . auto incrementing.. I was wondering why you were not using that in the first place? Save's alot of hassle Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283130 Share on other sites More sharing options...
joshgarrod Posted June 26, 2007 Author Share Posted June 26, 2007 lol, wot the hell is that, (noob) Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283133 Share on other sites More sharing options...
Illusion Posted June 26, 2007 Share Posted June 26, 2007 what about this, after SQL query has got executed If(isset($_POST['id']) { $id=$_POST['id']+1; } else { $id=""; } also INPUT should be like this <INPUT TYPE="text" NAME="ID" value="<?php echo $id; ?>" SIZE=40> and also I don't see any code to get the posted values and when to display the form and when not. Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283138 Share on other sites More sharing options...
Solarpitch Posted June 26, 2007 Share Posted June 26, 2007 When you create a row in the database for your stock_id.. CREATE TABLE Products( stock_id int NOT NULL AUTO_INCREMENT, ... now everytime a new record is added to Products... MySQL will set the stock_id to the next value! Link to comment https://forums.phpfreaks.com/topic/57280-solved-counting-on-with-numbers/#findComment-283139 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.