Jump to content

[SOLVED] Trying to Subtract - Getting a Parse Error on the last line - the ?> Line


Recommended Posts

Im trying to develop a PHP form that allows the user to input a value on the form, then the $_POST value will be subtracted from a QTY value that I get from searching the INV database for the Product Number selected in the same php form as the subtracting amount.  Everything works if I eliminate the subtraction (the query pulls the proper database value, and the history table is updated with the proper value minus the new QTY on Hand).  Here is the code I have for the php portion:

 

<?php
// Connect to mysql database

$con = mysql_connect("localhost","user","password") or die(`Connection: ` . mysql_error());;
mysql_select_db("logdata", $con) or die(`Database: ` . mysql_error());

mysql_select_db("logdata", $con);

$consumed=$_POST['consumed'];
$ProdNum=$_POST['ProdNum'];

$copy = mysql_fetch_array(mysql_query("SELECT * FROM `SNAInv` WHERE `ProdNum`= `$ProdNum`")) or die(mysql_error());

$subtract = $copy[`QtyOnHand`] - $consumed;

$sql = mysql_query("UPDATE `SNAInv` SET `QtyOnHand` = '$subtract'  WHERE `ProdNum` = '$ProdNum'");

$sqlrun=mysql_query($sql) or die(mysql_error());

$hist = @mysql_query("INSERT INTO `SNAInvHist` (`ProdNum`, `ProdDesc`, `QtyOnHand`, `Consumed`, `RecvDate`, `ConSumDate`) VALUES (`" . $copy[`ProdNum`] . "`, `" . $copy[`ProdDesc`] . "`, $subtract, `". $_POST[Consumed] ."`, `". $copy[RecvDate] ."`, Now())") or die(mysql_error());

echo "Inventory For Part Number:\n" .$copy[`ProdNum`] ." has been modified and a Transaction has been recorded ";
echo "new Quantity is:\n $subtract;

mysql_close($sqlrun,$con);
?>

 

Here is the code for the form:

 

<html>
<head>
<title>Card Inventory</title>
</head>

<body bgcolor="#C0C0C0">
<b><img border="0" src="logo1.jpg" width="78" height="77"> 
<font face="Arial" color="#000080">SNA Inventory - Managing Inventory Input</font></b><font face="Arial" color="#000080">
<P>

<form method="POST" name="Recv" action="queryme.php">

<table width="550" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td colspan="2" bgcolor="#000000"><span class="style1">Managing Inventory Control</span></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Part Number: </td>
    <td bgcolor="#CCCCCC"><input name="ProdNum" type="text" id="ProdNum"></td>
  </tr>
  <tr>
    <td width="159" bgcolor="#FFCC66">Part Description:</td>
    <td width="373" bgcolor="#CCCCCC"><input name="ProdDesc" type="text" id="ProdDesc"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Amount Consumed: </td>
    <td bgcolor="#CCCCCC"><input name="Consumed" type="text" id="Consumed"></td>
  </tr>
   <tr>
    <td> </td>
    <td><input type="submit" value="Submit Modifications" /></td>
  </tr>
</table>

</form>
</body>
</html>

 

I get the following error:

 

Parse error: parse error in /var/www/htdocs/sna/queryme.php on line 26

 

Which is the last line of the php code - the ?> value... I know I have som wrong syntax, but for the life of me I cannot find it... I was hoping someone out there could see it and let me know where I screwed up.

 

Thanks in advance,

 

Texman

 

you forgot a quote at the bottom

<?php
// Connect to mysql database

$con = mysql_connect("localhost","user","password") or die(`Connection: ` . mysql_error());;
mysql_select_db("logdata", $con) or die(`Database: ` . mysql_error());

mysql_select_db("logdata", $con);

$consumed=$_POST['consumed'];
$ProdNum=$_POST['ProdNum'];

$copy = mysql_fetch_array(mysql_query("SELECT * FROM `SNAInv` WHERE `ProdNum`= `$ProdNum`")) or die(mysql_error());

$subtract = $copy[`QtyOnHand`] - $consumed;

$sql = mysql_query("UPDATE `SNAInv` SET `QtyOnHand` = '$subtract'  WHERE `ProdNum` = '$ProdNum'");

$sqlrun=mysql_query($sql) or die(mysql_error());

$hist = @mysql_query("INSERT INTO `SNAInvHist` (`ProdNum`, `ProdDesc`, `QtyOnHand`, `Consumed`, `RecvDate`, `ConSumDate`) VALUES (`" . $copy[`ProdNum`] . "`, `" . $copy[`ProdDesc`] . "`, $subtract, `". $_POST[Consumed] ."`, `". $copy[RecvDate] ."`, Now())") or die(mysql_error());

echo "Inventory For Part Number:\n" .$copy[`ProdNum`] ." has been modified and a Transaction has been recorded ";
echo "new Quantity is:\n". $subtract;

mysql_close($sqlrun,$con);
?>

I believe you are talking about this following:

 

echo "new Quantity is:\n". $subtract;

 

If so, I added it and I get the same error.  Now the php code takes my QtyOnHand to "0" for the ProdNum I select... fun, huh? 

 

If you can offer any assistance I would be grateful... Trying to learn this stuff as fast as possible... after 20 years in infrastructure and executive management this is more fun, but a real booger to figure out without manuals and training.

 

I take my hat off to you all.

 

Texman

<?php
// Connect to mysql database

$con = mysql_connect("localhost","user","password") or die(`Connection: ` . mysql_error());;
mysql_select_db("logdata", $con) or die(`Database: ` . mysql_error());

mysql_select_db("logdata", $con);

$consumed=$_POST['consumed'];
$ProdNum=$_POST['ProdNum'];

$copy = mysql_fetch_array(mysql_query("SELECT * FROM `SNAInv` WHERE `ProdNum`= `$ProdNum`")) or die(mysql_error());

$subtract = $copy[`QtyOnHand`] - $consumed;

$sql = mysql_query("UPDATE `SNAInv` SET `QtyOnHand` = '$subtract'  WHERE `ProdNum` = '$ProdNum'");

$sqlrun=mysql_query($sql) or die(mysql_error());

$hist = @mysql_query("INSERT INTO `SNAInvHist` (`ProdNum`, `ProdDesc`, `QtyOnHand`, `Consumed`, `RecvDate`, `ConSumDate`) VALUES (`$copy[ProdNum]`, `$copy[ProdDesc]`, `$subtract`, `$_POST[Consumed]`, `$copy[RecvDate]`, `Now()`)") or die(mysql_error());

echo "Inventory For Part Number:\n" .$copy[`ProdNum`] ." has been modified and a Transaction has been recorded ";
echo "new Quantity is:\n" . $subtract;

mysql_close($sqlrun,$con);
?>

 

Does that work?

No.

 

I got an syntax error then I made this change per the error message:

 

$copy = mysql_fetch_array(mysql_query("SELECT * FROM `SNAInv` WHERE `ProdNum` = `$_POST[ProdNum]`")) or die(mysql_error());

 

Now I get the following error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/htdocs/sna/invtest.php on line 10

Unknown column '2002228' in 'where clause'

 

the '2002228'  is the $_POST[ProdNum] from the html form.

 

Any ideas?

The code I changed was:

 

$copy = mysql_fetch_array(mysql_query("SELECT * FROM `SNAInv` WHERE `ProdNum` = `$_POST[ProdNum]`")) or die(mysql_error());

 

For some reason it is not showing.

Okay, try this

 

<?php
// Connect to mysql database

$con = mysql_connect('localhost', 'user', 'password') or die('Connection: ' . mysql_error());
mysql_select_db('logdata', $con) or die('Database: ' . mysql_error());

$consumed = $_POST['consumed'];
$ProdNum = $_POST['ProdNum'];

$result = mysql_query("SELECT * FROM SNAInv WHERE ProdNum = '$ProdNum'");

$copy = mysql_fetch_array($result) or die(mysql_error());

$subtract = ( $copy['QtyOnHand'] - $consumed );

$sqlrun = mysql_query("UPDATE SNAInv SET QtyOnHand = '$subtract'  WHERE ProdNum = '$ProdNum'") or die(mysql_error());

$hist = mysql_query("INSERT INTO SNAInvHist ('ProdNum', 'ProdDesc', 'QtyOnHand', 'Consumed', 'RecvDate', 'ConSumDate') VALUES (`$copy[ProdNum]', '$copy[ProdDesc]', '$subtract', '$_POST[Consumed]', '$copy[RecvDate]', 'Now()')") or die(mysql_error());

echo 'Inventory For Part Number: ' . $copy['ProdNum'] . ' has been modified and a Transaction has been recorded. New Quantity is: ' . $subtract;

mysql_close($con);
?>

Dont put backticks around your SQL data variables.

 

No.

 

I got an syntax error then I made this change per the error message:

 

$copy = mysql_fetch_array(mysql_query("SELECT * FROM `SNAInv` WHERE `ProdNum` = `$_POST[ProdNum]`")) or die(mysql_error());

 

Now I get the following error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/htdocs/sna/invtest.php on line 10

Unknown column '2002228' in 'where clause'

 

the '2002228'  is the $_POST[ProdNum] from the html form.

 

Any ideas?

most always....well everytime it's happened to me

when you get an error at the very end of your script...it's because you're missing a curly brace

 

or you have one to many...which is pretty much the same as missing one

most always....well everytime it's happened to me

when you get an error at the very end of your script...it's because you're missing a curly brace

 

or you have one to many...which is pretty much the same as missing one

 

I have ran into that TOO many times myself.  I will start to count them.

 

Thanks,

 

Texman

Well,

 

I got it to run but a new problem as arisen.  For some reason I get a negative value of the data inputted into the form by the user.  So for example, I have a Qty On Hand of 8000 units...  I run the form and say I'm consuming 450 units.  The results are I will have a Qty on Hand of -450. 

 

To me it appears that this code:

 

$sql = "SELECT * FROM `SNAInv` WHERE `ProdNum`= '$_POST[ProdNum]'";
$result = mysql_query($sql) or die('Query Error: ' . mysql_error());
$copy = mysql_fetch_array($result);

 

Is not working... i.e. is not pulling the proper data required from the table in question.  OR my subtract statement of:

 

$newquant = $sql[QtyOnHand] - $_POST[Consumed];

 

is telling my QtyOnHand pulled from the initial query to hit the road jake and it over writes the QtyOn Hand as "0" or ignores it.

 

I cannot really tell... but from the initial query (listed in the first code string) is working since the secondary table I try to populate is collecting that data from that query and is inserting it... the only thing not working is the subtraction part... again.

 

Any ideas?

 

Texman

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.