Hello,

I'm trying to modify an existing php page by adding a submit button, to allow the user to change the unit cost of a product in the inventory. I can get the product informations from the database, but nothing happens when I press the submit button. The fist submit button in the code allows to retrieve the product information by entering the serial number, this one works.

Many thanks for your help!

Yanoli

Here is php code:

<?php
/* $Revision: 1.6 $ */

$PageSecurity = 3;

include('includes/session.inc');
$title = _('Serial Item Research');
include('includes/header.inc');


//validate the submissions
if (isset($_POST['serialno'])) {
    $SN = trim($_POST['serialno']);
} elseif(isset($_GET['serialno'])) {
    $SN = trim($_GET['serialno']);
} else {
    $SN = '';
}
$SN = $SN;

if (isset($_POST['submitcost'])) {
    $sql = "UPDATE purchorderdetails SET unitprice='" . $_POST['unitcost'] . "'";
}


?>
<div class="centre">
<br>
<form name=SNRESEARCH method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo _('Serial Number') ?>: <input ID="serialno" name="serialno" size=21 maxlength=20 VALUE="<?php echo $SN; ?>"> &nbsp; 
<input type=submit name=submit>
</form>

<form name=unitcost method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo _('New unit cost') ?>: <input ID="submitcost" name="submitcost" size=21 maxlength=20 VALUE=""> &nbsp; 
<input type=submit name=submitcost>
</form>


<SCRIPT>
document.getElementById('serialno').focus();
</SCRIPT>

<?php

if ($SN!='') {
    //the point here is to allow a semi fuzzy search, but still keep someone from killing the db server
    if (strstr($SN,'%')){
        while(strstr($SN,'%%')) {
            $SN = str_replace('%%','%',$SN);
        }
        if (strlen($SN) < 11){
            $SN = str_replace('%','',$SN);
            prnMsg('You can not use LIKE with short numbers. It has been removed.','warn');
        }
    }

    $SQL = "SELECT  stockserialitems.stockid,
        stockserialitems.serialno AS serialno,
        stockmaster.description,
        stockserialmoves.ordernossm,
        purchorderdetails.unitprice AS unitcost,
        stockcategory.categorydescription,
        stockmaster.categoryid
FROM    stockserialitems,
        stockmaster,
    stockserialmoves,
    purchorderdetails,
    stockcategory
WHERE stockserialitems.stockid=stockmaster.stockid
AND stockserialitems.serialno=stockserialmoves.serialno
AND stockserialmoves.ordernossm=purchorderdetails.orderno
AND stockserialmoves.stockid=purchorderdetails.itemcode
AND stockmaster.categoryid=stockcategory.categoryid
AND stockserialitems.serialno like '$SN'
ORDER BY stockmaster.description";

    $result = DB_query($SQL,$db);
    $myrow = DB_fetch_array($result);

    $_POST['stockid']  = $myrow['stockid'];
    $_POST['serialno']  = $myrow['serialno'];
    $_POST['description']  = $myrow['description'];
    $_POST['unitcost']  = $myrow['unitcost'];

    echo '<table>';

    echo '<tr><td>' . _('Stock ID') . ':</td><td><input type="text" name="stockid" VALUE="' . $_POST['stockid'] . '" size=42 maxlength=40></td></tr>';
    echo '<tr><td>' . _('Serial Number') . ':</td><td><input type="text" name="serialno" VALUE="' . $_POST['serialno'] . '" size=42 maxlength=40></td></tr>';
    echo '<tr><td>' . _('Description') . ':</td><td><input type="text" name="description" VALUE="' . $_POST['description'] . '" size=42 maxlength=40></td></tr>';
    echo '<tr><td>' . _('Unit Cost') . ':</td><td><input type="text" name="unitcost" VALUE="' . $_POST['unitcost'] . '" size=42 maxlength=40></td></tr>';


    echo '</table>';
}//END OF POST IS SET


echo '</div>';

include('includes/footer.inc');
?>

Your coding was slightly incorrect. Your input field AND your submit button have the same names (name="submitcost").

This works:

<?php

	if (isset($_POST['submit'])) {
		$price = $_POST['submitcost'];
		echo $price ."<br>";
                echo "it works";
	}


?>

<form name="unitcost" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input ID="submitcost" name="submitcost" size="21" maxlength="20" VALUE=""> &nbsp; 
    <input type="submit" name="submit">
</form>

You should remember to put ALL your html property values in double quotes. And next time please post your code in [#code] tags!

Hi,

The second button works now, but it does not update the database, I think there is a mistake in the update command:

if (isset($_POST['submitcost'])) {
        $price = $_POST['submitcost'];
        echo $price ."<br>";
        $sql = "UPDATE purchorderdetails SET unitprice='" . $price . "'";
}

Thanks,
Yanoli

yes, it is an incorrect command. It needs to be in this format:

$sql = mysql_query("UPDATE purchorderdetails SET unitprice='$price' WHERE record_number='$record_number'") or die(mysql_error());

I presume you have an 'ID' primary key field in your table? If not, it would be wise to create one, Otherwise you won't be able to limit which record(s) you are updating!

See here for an example of the Mysql UPDATE query http://www.tizag.com/mysqlTutorial/mysqlupdate.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.