...

View Full Version : Update not working



smarlar
08-15-2006, 05:38 PM
I have missed a closing statement somewhere and cannot figure it out.

This page is simply listing data and inputting a quantity filed to update.

The error is "Parse error: parse error, unexpected $ in /home/trailerr/public_html/UpdateQnty3.php on line 168"

Line 168 is the closing html statement" </html>".



<?php

require_once('init.php');

require_once('settings.inc');

if (isset($_POST['Update'])){

if ($_POST['Update']=="Home") header('Location: CustomerSearch.php');

if ($_POST['Update']=="Save") {
$link = mysql_connect($hostname, $login, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$cid=0;
$type="TL";
foreach ($_POST["Qnty"] as $value) {
if ($cid>=$_POST["RN"]) $type="TT";

mysql_select_db($dbname);
if ($type=="TL") {

$result = mysql_query('
SELECT * from trailertype WHERE ID='.$_POST["rid"][$cid]
) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result));

mysql_query("UPDATE trailertype
SET
Qnty='".$_POST["Qnty"][$cid]."'
WHERE ID=".$_POST["rid"][$cid])
or die("Query failed : " . mysql_error());
}

else
{

$result = mysql_query('
SELECT * from tractortype WHERE ID='.$_POST["rid"][$cid]
) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result));

mysql_query("UPDATE tractortype
SET
Qnty='".$_POST["Qnty"][$cid]."'
WHERE ID=".$_POST["rid"][$cid])
or die("Query failed : " . mysql_error());
}

?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="griffin.css" type="text/css">
<title>Update Quantity</title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>

<?php
include ('logo_header.php');
?>
<table border=0 width=800><tr><td>
<form name=UpdateQnty action="UpdateQnty.php" method=post>
<a class=titler>Update Quantity </a> as of <?php echo date("F d, Y"); ?>
</td></tr></table><br>

<table border=0 width=800 align=center bgcolor=#000000 cellpadding=0 cellspacing=0><tr ><td>
<table border=0 width=100% align=center cellpadding=3 cellspacing=1><tr bgcolor=cdcdcd align=center >

<td><b>Ref. No. </b></td>
<td width=60><b> Available Date </b></td>
<td><b> Year </b></td>
<td><b> Equipment Type </b></td>
<td><b> Qty Update</b></td>
<td width= 100><b> Company </b></td>
<td><b>Status Date</b></td></tr>



<?php

mysql_select_db($dbname);

$result = mysql_query('
SELECT * from trailertype tl, customerinfo c
WHERE tl.RefNo=c.RefNo and tl.status="Sell" '.$wh)
or die("Query failed : " . mysql_error());

$TL=mysql_num_rows($result);
$TLNum=$TL;
$TLa=array($TLNum);

while ($row = mysql_fetch_assoc($result))
{

$TLa[$row['ID']]=$row['SELL_STATUS'];

echo "<tr bgcolor=white>
<td> TL$row[ID]
<input type=hidden name=rid[] value=$row[ID]>
<input type=hidden name=RN value=$TL>
</td>
<td>".date("F",mktime(0,0,0,$row['aMonth'],1,2005))."&nbsp;".$row['aYear']."</td>
<td> $row[Year] </td>
<td> $row[TL] <input type=hidden name=EQ value=TL></td>
<td> <input type=text name=Qnty[] size=1 value=".$row['Quantity']." class=TxtBut></td>
<td> $row[Company] </td>
<td>".strftime("%m/%d/%y",strtotime("$row[Status_Date]"))." </td>
</tr>";
}

$result = mysql_query('
SELECT * from tractortype tt, customerinfo c
WHERE tt.RefNo=c.RefNo and tt.status="Sell" '.$wh)
or die("Query failed : " . mysql_error());

$TTNum=mysql_num_rows($result);
$TTa=array($TTNum);
while ($row = mysql_fetch_assoc($result))
{
$TTa[$row['ID']]=$row['SELL_STATUS'];
echo "<tr bgcolor=white>
<td> TT$row[ID] <input type=hidden name=rid[] value=$row[ID]></td>
<td>".date("F",mktime(0,0,0,$row['aMonth'],1,2005))."&nbsp;".$row['aYear']."</td>
<td> $row[Year] </td>
<td> $row[TT] <input type=hidden name=EQ value=TT></td>
<td> <input type=text name=Qnty[] size=1 value=".$row['Quantity']." class=TxtBut></td>
<td> $row[Company] </td>
<td>".strftime("%m/%d/%y",strtotime("$row[Status_Date]"))." </td>
</tr>";
}


?>


</table>
</td></tr></table>

<center><br>
<input type=button name="Update" value="Print" class=TxtBut onClick="window.print()">&nbsp;&nbsp;&nbsp;
<input type=submit name="Update" value="Save" class=TxtBut>&nbsp;&nbsp;&nbsp;
<input type=submit name="Update" value="Home" class=TxtBut>
</form>
<br></center>

<?php include ('logo_footer.php');?>

</body>

</html>

I have narrowed it down to the following section but, can't figure it out.

$cid=0;
$type="TL";
foreach ($_POST["Qnty"] as $value) {
if ($cid>=$_POST["RN"]) $type="TT";

mysql_select_db($dbname);
if ($type=="TL") {

$result = mysql_query('
SELECT * from trailertype WHERE ID='.$_POST["rid"][$cid]
) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result));

mysql_query("UPDATE trailertype
SET
Qnty='".$_POST["Qnty"][$cid]."'
WHERE ID=".$_POST["rid"][$cid])
or die("Query failed : " . mysql_error());
}

else
{

$result = mysql_query('
SELECT * from tractortype WHERE ID='.$_POST["rid"][$cid]
) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result));

mysql_query("UPDATE tractortype
SET
Qnty='".$_POST["Qnty"][$cid]."'
WHERE ID=".$_POST["rid"][$cid])
or die("Query failed : " . mysql_error());
}

arnyinc
08-15-2006, 06:57 PM
That error usually means you missed a '}'

Do you ever close the very first if-statement?

if (isset($_POST['Update'])){

guelphdad
08-15-2006, 07:37 PM
If you use a decent text editor then it should be relatively easy to find mis-matched { or }.

smarlar
08-15-2006, 07:54 PM
If you use a decent text editor then it should be relatively easy to find mis-matched { or }.

Can you recommend one?

smarlar
08-15-2006, 09:15 PM
I have closed all the If statements and now no more error, but I get no updated information. I tried to echo $_POST[Qnty] in various locations but it never displays the information.

My question is am I closing the IF statment too soon thus not inserting the new information into the table?


<?php

require_once('init.php');

require_once('settings.inc');

if (isset($_POST['Update'])){

if ($_POST['Update']=="Home") header('Location: CustomerSearch.php');
if ($_POST['Update']=="Save") {


$link = mysql_connect($hostname, $login, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$cid=0;
$type="TL";
foreach ($_POST["Qnty"] as $value) {
if ($cid>=$_POST["RN"]) $type="TT";

mysql_select_db($dbname);
if ($type=="TL") {

$result = mysql_query('
SELECT * from trailertype WHERE ID='.$_POST["rid"][$cid]
) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result));

mysql_query("INSERT INTO trailertype
(Quantity)
VALUES
('$_POST[Qnty]')
")or die("Query failed : " . mysql_error());
}
else
{

$result = mysql_query('
SELECT * from tractortype WHERE ID='.$_POST["rid"][$cid]
) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result));

mysql_query("INSERT INTO tractortype
(Quantity)
VALUES
('$_POST[Qnty]')
")or die("Query failed : " . mysql_error());
}
}}}

?>

Fumigator
08-15-2006, 09:44 PM
Can you recommend one?

I like Crimson Editor (http://www.crimsoneditor.com). It's even free.

Regarding your code... there are a few syntax problems you need to resolve.

Your select queries are missing a closing single quote around id='<value>'.

You are misusing the statement "while ($row = mysql_fetch_assoc($result));". There is no need to enclose the mysql_fetch_assoc() function inside a while loop that doesn't have anything inside its loop!

I lose track of your foreach() loop, I'm not sure you've enclosed it properly, but it's hard to tell because you aren't properly indenting your code. It's a personal preference I know, but without proper indenting it makes life hell when you have multiple nested "if" statements and loops within those "if" statements.

Fumigator
08-15-2006, 09:49 PM
On second glance I take back what I said about the id='<value>' stuff; as long as your ID column is an integer your code will work fine.

smarlar
08-16-2006, 08:35 PM
I have re-worked my code to only pass the information over for an indiviual record, but running into a problem submitting the changes.

I think I have everything right as the out put works ok and I can change the amounts. Again the problem is submitting it does nothing!


<?php

require_once('init.php');

require_once('settings.inc');

if (isset($_GET['ID'])){

$link = mysql_connect($hostname, $login, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db($dbname);

$CustID=$_GET['ID'];

if ($_GET['type']=="TT")
{
$Ttable="tractortype";
}
else
{
$Ttable="trailertype";
}
$result2=mysql_query("SELECT * FROM ".$Ttable." WHERE ID=".$_GET['ID']) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_array($result2))
{
$AY=$row['aYear'];
$Y =$row['Year'];
$Q =$row['Quantity'];
$P =$row['SellingPrice'];
$AM=$row['aMonth'];
}
}
?>

<?php
if (isset($_POST['Update'])){
if ($_POST['Update']=="Back") header('Location: CustomerInfo.php');
if ($_POST['Update']=="Save")
{
$link = mysql_connect($hostname, $login, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname);

mysql_query("INSERT INTO ".$Ttable."
(Quantity, SELLING_PRICE)
VALUES
('$Q','$P')")
or die("Query failed : " . mysql_error());
}
if ($_POST['Update']=="Save") header('Location: UpdateQnty.php');
}


?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="griffin.css" type="text/css">
<title>Equipment Update</title>
</head>

<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>

<?php
include ('logo_header.php');
?>

<?php
if (isset($_GET['type'])) {
if ($_GET['type']=="TL") {
echo "<td>&nbsp;&nbsp;<b>Year:</b>";
echo " ".$Y."";
echo "";
echo "&nbsp;&nbsp;<b>Quantity:</b> <input type=text name=TLqty size=10 class=TxtBut";
if (isset($Q)) echo " value=\"".$Q."\"";
echo "&nbsp;&nbsp;<b>Price:</b> <input type=text name=TLprice size=10 class=TxtBut";
if (isset($P)) echo " value=\"".$P."\" </td></tr>";
echo "<tr><td colspan=3>&nbsp;</td></tr>";
}
}

if (!isset($_GET['type'])) {

echo "<td>&nbsp;&nbsp;<b>Year:</b>";
echo "&nbsp;&nbsp;<b>Quantity:</b> <input type=text name=TLqty size=10 class=TxtBut";
echo "&nbsp;&nbsp;<b>Price:</b> <input type=text name=TLprice size=10 class=TxtBut";
echo "</td></tr>";
echo "<tr><td colspan=3>&nbsp;</td></tr>";
}
?>

<?php

if (isset($_GET['type'])) {
if ($_GET['type']=="TT") {
echo "<td>&nbsp;&nbsp;<b>Year:</b>" ;
echo "&nbsp;&nbsp;<b>Quantity:</b> <input type=text name=TTqty size=10 class=TxtBut";
if (isset($Q)) echo " value=\"".$Q."\"";
echo "&nbsp;&nbsp;<b>Price:</b> <input type=text name=TTprice size=10 class=TxtBut";
if (isset($P)) echo " value=\"".$P."\" </td></tr>";
echo "<tr><td colspan=3>&nbsp;</td></tr>";
}
}

if (!isset($_GET['type']))
{
echo "<td>&nbsp;&nbsp;<b>Year:</b>";
echo "&nbsp;&nbsp;<b>Quantity:</b> <input type=text name=TTqty size=10 class=TxtBut";
echo "&nbsp;&nbsp;<b>Price:</b> <input type=text name=TTprice size=10 class=TxtBut";
echo "</td></tr>";
echo "<tr><td colspan=3>&nbsp;</td></tr>";
}
echo "</table>";


?>

<center><br>
<input type=hidden name=ID value="<?php echo $_GET['ID']; ?>">
<input type=hidden name=q value="<?php echo $_GET['q']; ?>">
<input type=hidden name=p value="<?php echo $_GET['p']; ?>">
<input type=submit name="Update" value="Back" class=TxtBut>&nbsp;&nbsp;&nbsp;
<input type=submit name="Update" value="Save" class=TxtBut>
</form>
<br></center>

<?php include ('logo_footer.php');?>

</body>

</html>

Fumigator
08-16-2006, 08:55 PM
You have no <form> tag.

smarlar
08-16-2006, 10:35 PM
I hate that, you tend to miss the simple things... Ok now that it submits I am lacking in the update logic. I replace insert into with udate and having a small problem.

The error "Query failed : 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 'SET Quantity='', SellingPrice='', WHERE I"

I am testing over and over on mysql server but don't get the same results..

Before

mysql_select_db($dbname);
mysql_query("INSERT INTO ".$Ttable."
(Quantity, SellingPrice)
VALUES
('$Q','$P')")
or die("Query failed : " . mysql_error());

After using the Update command instead


mysql_select_db($dbname);

mysql_query("UPDATE ".$Ttable."
SET
Quantity='".$Q."',
SellingPrice='".$P."',
WHERE ID=".$_GET["ID"] )
or die("Query failed : " . mysql_error());

Fumigator
08-16-2006, 11:49 PM
Whenever I have a SQL syntax error I echo out the query string and if I can't see anything wrong by doing that, I copy/paste it into an adhoc query engine (such as PHPMyAdmin) and tweak it there to see if I can find the problem.

In your case you are putting the string directly into your mysql_query() function, so in order to debug you'll need to break the string out with a $query = "UPDATE blah blah blah"; statement and then a echo $query statement.

smarlar
08-17-2006, 12:03 AM
I trying to narrow it down and did not fully understand your comment of echoing It out. Are you reffering to the following?


$query= "UPDATE ".$Ttable."
SET
Quantity='".$Q."',
SellingPrice='".$P."',
WHERE ID=".$_GET["ID"]

Echo "$query"

I am wondering if the variables are correctly coded, the error makes be believe that its how I am setting them.


Quantity='".$Q."',
SellingPrice='".$P."',

Given that I am setting the constants below using inputs from the EquipmentUpd.php page.


echo "&nbsp;&nbsp;<b>Quantity:</b> <input type=text name=TLqty size=10 class=TxtBut";
if (isset($Q)) echo " value=\"".$Q."\"";
echo "&nbsp;&nbsp;<b>Price:</b> <input type=text name=TLprice size=10 class=TxtBut";
if (isset($P)) echo " value=\"".$P."\" </td></tr>";

It's a bit fuzzy for me, the code is'nt given me the problem just the results.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum