Instead of passing the entire SQL query as a string, try setting into a multiline variable like this:
Code: Select all
<?php
$sql = "SELECT *
FROM `mytable`
WHERE `myfield` = '$queryvar'
LIMIT 10";
?>Moderator: General Moderators
Code: Select all
<?php
$sql = "SELECT *
FROM `mytable`
WHERE `myfield` = '$queryvar'
LIMIT 10";
?>I guess I do not follow? I can run the commands in MySQL to subtract (I have to use a whole number not the call from the form as I have in my current code) and it works fine... basically everything works fine except this subtract function. The Parse Error I receive is a clueless error, much like I have bee seeing in this path of learning php. What sucks is earlier on before I required all the other data to be queried and loaded to additional tables the subtract WORKED... so I cannot figure out what syntax I have applied that this system just does not like.That means you SQL is jacked up.
Code: Select all
<?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());
// Insert form values into database
/**
* Something to note... you really should be checking this POST variable for:
* - isset
* - is valid
* - is clean
*/
$sql = "SELECT * FROM `SNAInv` WHERE `ProdNum`= '$_POST[ProdNum]'";
$result = mysql_query($sql) or die('Query Error: ' . mysql_error());
$copy = mysql_fetch_array($result);
// Now to the next query
/**
* Looking at this now, you really don't the first query at all
* Same rules for the POST var though
* And I think you problem was with the lack of quotes around your post var
* and the use of backticks where you should not have had them
*/
$sql = "UPDATE `SNAInv`
SET `QtyOnHand` = `QtyOnHand` - $_POST[Consumed]
WHERE `ProdNum` = '$_POST[ProdNum]";
$result = mysql_query($sql) or die('SQL error" ' . mysql_error());
// You should check affeceted rows when doing an insert/update/delete query
if (mysql_affected_rows() == 0) {
// There was a problem
}
/**
* Ok, from the query below it looks like you need the new quantity so you will
* have to think of a better way to get to it.
*
* Perhaps somewhere above you could make a var called $newquant and set it
* equal to `QtyOnHand` - $_POST[Consumed] and use that in your update query
* above and in this query below.
*/
/**
* Ok, you did need the first query for this one here
*/
$sql = "INSERT INTO `SNAInvHist` (
`ProdNum`,
`ProdDesc`,
`QtyOnHand`,
`Consumed`,
`RecvDate`,
`ConSumDate`
)
VALUES (
'$copy[ProdNum]',
'$copy[ProdDesc]',
'$newquant', '$_POST[Consumed]',
'$copy[RecvDate]',
Now())";
$result = mysql_query($sql) 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$newquant";
mysql_close($con);
?>
<p>Return to Manage Inventory Control</p>I have no idea what that is. I'm searching that as I type this. Still learning the lingo so I do apologize.Crap. You are right weirdan. Don't forget the escaping of vars pushed to your queries.
Code: Select all
$sql = "SELECT * FROM `SNAInv` WHERE `ProdNum`= '$_POST[ProdNum]'";
$result = mysql_query($sql) or die('Query Error: ' . mysql_error());
$copy = mysql_fetch_array($result);
$newquant = $sql[QtyOnHand] - $_POST[Consumed];
It should tell you everything you need to correct your error. It tells you that it's a syntax error, rather than a logic error or failure to connect, or whatever. It tells you which line of your script the syntax error is on. It tells you the exact place in your SQL statement where it failed to understand you. And it even recommends that you read the appropriate manual to learn the correct syntax. What else did you expect it to tell you? (And this is no doubt the most common error that you will ever see in a script that includes MySQL commands.)texmansru47 wrote:Will do. I corrected the error and now I get a very unfriendly error message... have you seen this one before?
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
This really tells me nothing.
Texman
Well, it tells anything but that. That's why it's recommended to add __FILE__ . ':' . __LINE__ to the error message.califdon wrote:It tells you which line of your script the syntax error is on.
Code: Select all
<?phpIs the exact error I get. There is no line or file information at all oustide of the "near "1" at line 1" BS.You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1