Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

url encoding works but form doesn't

Status
Not open for further replies.

mancroft

Programmer
Oct 26, 2002
267
0
0
GB
Hello

I am trying to get data from a php form to a mysql database and it just is not working. It works OK when the data is put in URL encoded attached to a URL (so this works...
Code:
[URL unfurl="true"]https://secure4.i.com/~k/ShoppingBag.php?[/URL] 
action=add_item&id=123456&QTY=1
)

but not when it is put in via a form

I'm sorry, but this is driving me mental.

Can anyone suggest what the problem might be?

Thanks.
Code:
<form action=&quot;[URL unfurl="true"]https://secure4.i.com/~k/ShoppingBag.php&quot;[/URL] method=&quot;POST&quot;>
<input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;123456&quot;>
<table border=&quot;0&quot; width=&quot;126&quot;>
<tr align=&quot;right&quot;> 
<td width=&quot;57&quot;><strong>QTY </strong></td>
</tr>
<tr align=&quot;right&quot;> 
<td><select name=&quot;QTY&quot; size=&quot;1&quot;>
<option
selected>1</option>
<option>2</option>
<option>3 </option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8 </option>
<option>9</option>
</select></td>
</tr>
<tr align=&quot;right&quot;> 
<td colspan=&quot;2&quot;><input
type=&quot;image&quot; name=&quot;I1&quot; src=&quot;../IMAGES/ADD-TO-CART.jpg&quot;
align=&quot;bottom&quot; border=&quot;0&quot; width=&quot;126&quot; height=&quot;45&quot;> </td>
</tr>
</table>
</form> 




<?php

include(&quot;db.php&quot;);

switch($_POST&quot;action&quot;)
{
case &quot;add_item&quot;:
{
AddItem($_POST&quot;id&quot;, $_POST&quot;QTY&quot;);
ShowCart();
break;
}
case &quot;update_item&quot;:
{
UpdateItem($_POST&quot;id&quot;, $_POST&quot;QTY&quot;);
ShowCart();
break;
}
case &quot;remove_item&quot;:
{
RemoveItem($_POST&quot;id&quot;);
ShowCart();
break;
}
default:
{
ShowCart();
}
}

function AddItem($ITEMID, $QTY)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
//echo (&quot;ID = &quot; . $ITEMID);
global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error()); 

// Check if this item already exists in the users cart table
$result = mysql_query(&quot;select count(*) from cart where cookieId = '&quot; . GetCartId() . &quot;' and ITEMID = $ITEMID&quot;) or die(mysql_error()); 
$row = mysql_fetch_row($result) or die(mysql_error());
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query(&quot;insert into cart(cookieId, ITEMID, QTY) values('&quot; . GetCartId() . &quot;', $ITEMID, $QTY)&quot;);
}
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($ITEMID, $QTY);
}
}





function UpdateItem($ITEMID, $QTY)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());

if($QTY == 0)
{
// Remove the item from the users cart
RemoveItem($ITEMID);
}
else
{
mysql_query(&quot;update cart set QTY = $QTY where cookieId = '&quot; . GetCartId() . &quot;' and ITEMID = $ITEMID&quot;) or die(mysql_error());
}
}

function RemoveItem($ITEMID)
{
// Uses an SQL delete statement to remove an item from
// the users cart

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());

mysql_query(&quot;delete from cart where cookieId = '&quot; . GetCartId() . &quot;' and ITEMID = $ITEMID&quot;) or die(mysql_error());
}

function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());

$totalCost = 0;
$result = mysql_query(&quot;select * from cart inner join items on cart.ITEMID = items.ITEMID where cart.cookieId = '&quot; . GetCartId() . &quot;' order by items.ITEMNAME asc&quot;) or die(mysql_error());
?>

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>Shopping cart</title>

<script language=&quot;JavaScript&quot;>

function UpdateQTY(item)
{
ITEMID = item.name;
newQTY = item.options[item.selectedIndex].text;

document.location.href = 'ShoppingBag.php?action=update_item&id='+ITEMID+'&QTY='+newQTY;
}

</script>

<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body background=&quot;../images/BACK.jpg&quot; topmargin=&quot;0&quot;>

<form name=&quot;frmCart&quot; method=&quot;post&quot; action=&quot;&quot;>


<div align=&quot;center&quot;><img src=&quot;../images/SHOPPING.jpg&quot; width=&quot;695&quot; height=&quot;134&quot;><br>
<table width=&quot;700&quot; border=&quot;0&quot; align=&quot;center&quot; cellspacing=&quot;1&quot;><!--DWLayoutTable-->
<tr>
<td colspan=&quot;6&quot; align=&quot;center&quot; bgcolor=&quot;#FF0000&quot;><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;><strong>Bag
Content</strong></font></td>
</tr>
<tr>
<td width=&quot;84&quot; bgcolor=&quot;#990000&quot;><strong><font color=&quot;#FFFFFF&quot; size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Item</font></strong></td>
<td colspan=&quot;2&quot; bgcolor=&quot;#990000&quot;><strong><font color=&quot;#FFFFFF&quot; size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Product</font></strong><strong></strong></td>
<td width=&quot;105&quot; bgcolor=&quot;#990000&quot;><strong><font color=&quot;#FFFFFF&quot; size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Price
Each</font></strong></td>
<td width=&quot;53&quot; bgcolor=&quot;#990000&quot;><strong><font color=&quot;#FFFFFF&quot; size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>QTY</font></strong></td>
<td width=&quot;48&quot; bgcolor=&quot;#990000&quot;><strong><font color=&quot;#FFFFFF&quot; size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Remove</font></strong></td>
</tr>
<?php

while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row&quot;QTY&quot; * $row&quot;ITEMPRICE&quot;) or die(mysql_error()); 
?>

<tr>
<td height=&quot;25&quot;><font size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;><?php echo $row['ITEMID']; ?></font></td>
<td height=&quot;25&quot;><?php echo '<img src=&quot;../images/' .$row['ITEMID'].'.png&quot; />'; ?></td>
<td height=&quot;25&quot;><font size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;><?php echo $row&quot;ITEMNAME&quot;; ?></font></td>
<td height=&quot;25&quot;><font size=&quot;1&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>$<?php echo number_format($row&quot;ITEMPRICE&quot;, 2, &quot;.&quot;, &quot;,&quot;); ?></font></td>
<td height=&quot;25&quot;><select name=&quot;<?php echo $row&quot;ITEMID&quot;; ?>&quot; onChange=&quot;UpdateQTY(this)&quot;>
<?php

for($i = 1; $i <= 20; $i++)
{
echo &quot;<option &quot;;
if($row&quot;QTY&quot; == $i)
{
echo &quot; SELECTED &quot;;
}
echo &quot;>&quot; . $i . &quot;</option>&quot;;
}
?>
</select>

</td>
<td height=&quot;25&quot;><a href=&quot;ShoppingBag.php?action=remove_item&id=<?php echo $row&quot;ITEMID&quot;; ?>&quot;><strong><img src=&quot;../images/Del.jpg&quot; width=&quot;15&quot; height=&quot;15&quot; align=&quot;top&quot; border=&quot;0&quot;></strong></a></td>
</tr>





<?php
}

?>




<tr>
<td height=&quot;21&quot; colspan=&quot;6&quot; align=&quot;center&quot; valign=&quot;top&quot;><hr align=&quot;center&quot; width=&quot;700&quot; size=&quot;1&quot; color=&quot;red&quot; noshade></td>
</tr>
<tr>
<td height=&quot;43&quot; colspan=&quot;5&quot; align=&quot;right&quot; valign=&quot;bottom&quot;><font size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;><strong>Total</strong></font><br>
<font size=&quot;-2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Item price
includes shipping and handling.<br>
Tax will be added for FL. State residents </font></td>
<td align=&quot;center&quot; valign=&quot;middle&quot; bgcolor=&quot;#990000&quot;><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;><strong>$<?php echo number_format($totalCost, 2, &quot;.&quot;, &quot;,&quot;); ?> <input type=&quot;hidden&quot; name=&quot;thetotal&quot; value=&quot;<?php echo $totalCost; ?>&quot;></strong></font></td>
</tr>
<tr>
<td height=&quot;43&quot; colspan=&quot;3&quot;> </td>
<td height=&quot;43&quot; colspan=&quot;2&quot; align=&quot;left&quot; valign=&quot;bottom&quot;><a href=&quot;[URL unfurl="true"]http://www.actuacosmetics.com/Actua.htm&quot;><im...[/URL] src=&quot;../images/ContinueShopping.jpg&quot; width=&quot;150&quot; height=&quot;40&quot; border=&quot;0&quot;></a></td>
<td> </td>
</tr>
<tr>
<td height=&quot;43&quot; colspan=&quot;3&quot;> </td>
<td height=&quot;43&quot; colspan=&quot;2&quot; align=&quot;left&quot; valign=&quot;bottom&quot;>
<a href=&quot;Checkout.php&quot;><img src=&quot;../images/ProceedToCheckout.jpg&quot; width=&quot;150&quot; height=&quot;40&quot; border=&quot;0&quot;></a>


</td>
<td> </td>
</tr>
<tr>
<td height=&quot;43&quot;> </td>
<td width=&quot;54&quot; height=&quot;43&quot;> </td>
<td width=&quot;341&quot; height=&quot;43&quot;> </td>
<td height=&quot;43&quot; colspan=&quot;3&quot; align=&quot;left&quot; valign=&quot;bottom&quot;><img src=&quot;../images/LogoSmall.jpg&quot; width=&quot;145&quot; height=&quot;60&quot;><br>
<font size=&quot;-2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>lic </font></td>
</tr>
</table>
</div>
</form>
</body>
</html>
<?php
}

?>
 
I may be wrong but your form doesnt wont post a variable called action. To avoid confusion I suggest you call it something else and add the line.

<input type=&quot;hidden&quot; name=&quot;jobtodo&quot; value=&quot;add_item&quot;>

I only say this since the form also has its own action which may be why you have missed the posted variable in the first place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top