program logic.
Posted: Tue Nov 25, 2008 9:45 am
Hi,
I have two PHP files below and they don't quite do what I want. The first is select.php this displays the data that is in my database, it has a link to submit data from a form. The data stored is stock levels, the first file (select.php) allows user to send details of the current stock levels, the next file result.php, where the items have been successfully added. The two files don't produce errors but the program logic is not right. When a user submits a number on the first page this is added to the current number of the stock level, which is wrong. I need the number to write over the number sent and not to be added on. Hope this makes sense any one know how to do this?
The stock level is added to all amounts, which is wrong.
SHOULD BE...
Select.php - Displays stock levels say 2, 5, 3, 5 etc.
User enters new stock levels 2, 7, 1, 10 etc
All details updated (previous stock level overwritten, NOT ADDED).
update.php - confirmation of update.
I have two PHP files below and they don't quite do what I want. The first is select.php this displays the data that is in my database, it has a link to submit data from a form. The data stored is stock levels, the first file (select.php) allows user to send details of the current stock levels, the next file result.php, where the items have been successfully added. The two files don't produce errors but the program logic is not right. When a user submits a number on the first page this is added to the current number of the stock level, which is wrong. I need the number to write over the number sent and not to be added on. Hope this makes sense any one know how to do this?
Code: Select all
select.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>PHP Select</title>
<META NAME="Generator" CONTENT="">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="?">
<META NAME="Description" CONTENT="?">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white">
<h1>Update stock levels</h1>
<?php
include("connection.php");
?>
<?php
//connect to db
if(!($dblink=mysql_connect($server,$user,$password))) {
print "Fail: mysql_connect failed" ;
exit;
}
//error if can't select db
if(!mysql_select_db($db,$dblink)) {
print "Fail: can't select database" ;
exit;
}
// quuery db to get data
$sql = "select * from goods";
if(!($result = mysql_query($sql,$dblink))) {
print "Fail: invalid query";
exit;
}
//get data onto html page, display stock number
printf("<form action=\"%s\" method=\"%s\">\n", 'update.php', 'post');
while ($row = mysql_fetch_row($result))
{
printf("<input type=\"text\" name=\"editSTOCK[%d]\" value=\"%d\" /> Desc: %s\n", $row[0], $row[1], $row[2]);
}
print "<input type=\"submit\" name=\"mysubmit\" value=\"Update Stock\" />";
print "</form>";
?>
<p><a href="unit6.html">BACK</a>
</body>
</html>
Code: Select all
update.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Update PHP</title>
<META NAME="Generator" CONTENT="">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="?">
<META NAME="Description" CONTENT="?">
</head>
<body bgcolor="white">
<h2>stock level</h2>
<?php
include("connection.php");
?>
<?php
//connect to db
if(!($dblink=mysql_connect($server,$user,$password))) {
print "Fail: mysql_connect failed" ;
exit;
}
//error if can't select db
if(!mysql_select_db($db,$dblink)) {
print "Fail: can't select database" ;
exit;
}
// update table "goods" contents
$aEditStock = $_POST['editSTOCK'];
if (is_array($aEditStock))
{
foreach ($aEditStock AS $id => $value)
{
$id = (int)$id;
$value = (int)$value;
$sql = "UPDATE goods SET stock = stock + $value WHERE id = $id";
$res = mysql_query($sql);
if (!$res)
{
die(mysql_error());
}
}
}
?>
<p><a href="select.php">Back to stock</a>
</body>
</html>
SHOULD BE...
Select.php - Displays stock levels say 2, 5, 3, 5 etc.
User enters new stock levels 2, 7, 1, 10 etc
All details updated (previous stock level overwritten, NOT ADDED).
update.php - confirmation of update.