joshgarrod Posted June 27, 2007 Share Posted June 27, 2007 Ok, I have an add stock form which is linked to my stock table. I want the ID number to automatically increase by one in the form everytime a new stock item is added. At the moment, I have set the field to auto increment and if you enter a stock item and click submit on reload the previous ID number remains in the box, like I said above I want this to increase by one here. But I also want it to load when you first select the page which it currently doesn't. please help me. <!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"> <? $usr = "styut"; $pwd = "ixtykk8"; $db = "sttkuyk1"; $host = "localhost"; # 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" value="<?php echo $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/57430-solved-help-with-this-code-please-sorry-im-a-regular/ Share on other sites More sharing options...
cooldude832 Posted June 27, 2007 Share Posted June 27, 2007 you say ID is auto incremented right? If so don't even insert an ID just leave it blank and it will handle it then do this $id=mysql_insert_id(); after the insertion and it will tell you the AutoKey num inserted in the last insert query on this doc's load Link to comment https://forums.phpfreaks.com/topic/57430-solved-help-with-this-code-please-sorry-im-a-regular/#findComment-284137 Share on other sites More sharing options...
joshgarrod Posted June 27, 2007 Author Share Posted June 27, 2007 sorry, please could you tell me where to place that? Link to comment https://forums.phpfreaks.com/topic/57430-solved-help-with-this-code-please-sorry-im-a-regular/#findComment-284138 Share on other sites More sharing options...
cooldude832 Posted June 27, 2007 Share Posted June 27, 2007 change these lines: $SQL = $SQL . " (ID, StockNumber, ItemPrice, StockDescription) VALUES "; $SQL = $SQL . " ('$ID', '$StockNumber','$ItemPrice','$StockDescription') "; to: $SQL = $SQL . " (StockNumber, ItemPrice, StockDescription) VALUES "; $SQL = $SQL . " ('$StockNumber','$ItemPrice','$StockDescription') "; Then after: #execute SQL statement $result = mysql_db_query($db,"$SQL",$cid); Say: $ID=mysql_insert_id(); say: Link to comment https://forums.phpfreaks.com/topic/57430-solved-help-with-this-code-please-sorry-im-a-regular/#findComment-284142 Share on other sites More sharing options...
joshgarrod Posted June 27, 2007 Author Share Posted June 27, 2007 Great thanks alot. Link to comment https://forums.phpfreaks.com/topic/57430-solved-help-with-this-code-please-sorry-im-a-regular/#findComment-284143 Share on other sites More sharing options...
cooldude832 Posted June 27, 2007 Share Posted June 27, 2007 just a note for linking you don't need to say $sql = $sql . you can say $sql = "something"; $sql .= " Some More"; //result $sql is equal to something Some More the period links values however if you miss a period it will redefine $sql Link to comment https://forums.phpfreaks.com/topic/57430-solved-help-with-this-code-please-sorry-im-a-regular/#findComment-284154 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.