Jump to content

[SOLVED] Counting on with numbers...


joshgarrod

Recommended Posts

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

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;

 

 

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.