PHP: Form is not sending data correctly











up vote
0
down vote

favorite












Here is the website I am creating: http://cosc304.ok.ubc.ca/20647160/304_lab7/listprod.php
http://cosc304.ok.ubc.ca/20647160/304_lab7/showcart.php



I have never used PHP before, any guidance would be helpful. The code below showcases a website that is supposed to add to a cart but currently in the show cart there is a weird space between the first product and second product. It is not adding correctly. The first page list the products, the second link shows their cart.



<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Raph & Mishal's Grocery</title>
</head>
<body>

<h1>Search for the products you want to buy:</h1>

<form method="get" action="listprod.php">
<input type="text" name="productName" size="50">
<input type="submit" value="Submit"><input type="reset" value="Reset"> (Leave blank for all products)
</form>

<?php
/** If you're developing locally on WINDOWS, uncomment the following line **/
//include 'includemoney_format_windows.php';
include 'include/db_credentials.php';

/** Get product name to search for **/

if (isset($_GET['productName'])){
$name = $_GET['productName'];
}

/** $name now contains the search string the user entered
Use it to build a query and print out the results. **/
/** Create and validate connection **/
global $username;
global $password;
global $database;
global $server;
global $connectionInfo;

$con = sqlsrv_connect($server, $connectionInfo);
if( $con === false ) {
die( print_r( sqlsrv_errors(), true));
}

/** Print out the ResultSet **/
$sql = "SELECT productId, productName,price FROM product";
// $preparepreparedStatement =null;
$name =null;
if(isset($_GET['productName'])){
$name = $_GET[urlencode('productName')];
$sql = $sql ." WHERE productName LIKE ?";
$name = "%". $name. "%";
//$preparedStatement = sqlsrv_prepare($con, $sql,$pName);
}

$results = sqlsrv_query($con, $sql, array($name));
echo("<table><tr><th></th><th>ProductName</th><th>Price</th></tr>");
while ($row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC )) {
echo( "<tr><td><a href=addcart.php?id=".$row['productId']."&name=".$row['productName']."&price=".$row['price'].">Add to Cart</h></td><td>" . $row['productName'] . "</td><td>" . $row['price'] . "</td></tr>");
}
echo("</table>");

sqlsrv_close( $con );
/**
For each product create a link of the form
addcart.php?id=<productId>&name=<productName>&price=<productPrice>
Note: As some product names contain special characters, you may need to encode URL parameter for product name like this: urlencode($productName)
**/

?>

</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title>Your Shopping Cart</title>
</head>
<body>

<?php
// include 'include/money_format_windows.php'; //Only required on windows PCs
// Get the current list of products
session_start();
$productList = null;
if (isset($_SESSION['productList'])){
$productList = $_SESSION['productList'];
echo("<h1>Your Shopping Cart</h1>");
echo("<table><tr><th>Product Id</th><th>Product Name</th><th>Quantity</th>");
echo("<th>Price</th><th>Subtotal</th></tr>");

$total =0;
foreach ($productList as $id => $prod) {
echo("<tr><td>". $prod['id'] . "</td>");
echo("<td>" . $prod['name'] . "</td>");

echo("<td align="center">". $prod['quantity'] . "</td>");
$price = $prod['price'];

echo("<td align="right">".str_replace("USD","$",money_format('%i',$price))."</td>");
echo("<td align="right">" . str_replace("USD","$",money_format('%i',$prod['quantity']*$price)) . "</td></tr>");
echo("</tr>");
$total = $total +$prod['quantity']*$price;
}
echo("<tr><td colspan="4" align="right"><b>Order Total</b></td><td align="right">".str_replace("USD","$",money_format('%i',$total))."</td></tr>");
echo("</table>");

echo("<h2><a href="checkout.php">Check Out</a></h2>");
} else{
echo("<H1>Your shopping cart is empty!</H1>");
}
?>
<h2><a href="listprod.php">Continue Shopping</a></h2>
</body>
</html>









share|improve this question




















  • 2




    You're mixing different apis here.
    – Funk Forty Niner
    Nov 10 at 22:49






  • 1




    Are you using mysql or sqlsrv? $name should be inside the array() in the sqlsrv_query call
    – user3783243
    Nov 10 at 22:49












  • $mysqli where is that defined? You have too many errors here and the api to connect with is unknown.
    – Funk Forty Niner
    Nov 10 at 22:50










  • Do you have a column named like a link? I think you want echo '<tr><td><a href="href=addcart.php?id=' . $row['id'] . '&name=' $row['....'] . etc
    – user3783243
    Nov 10 at 22:53















up vote
0
down vote

favorite












Here is the website I am creating: http://cosc304.ok.ubc.ca/20647160/304_lab7/listprod.php
http://cosc304.ok.ubc.ca/20647160/304_lab7/showcart.php



I have never used PHP before, any guidance would be helpful. The code below showcases a website that is supposed to add to a cart but currently in the show cart there is a weird space between the first product and second product. It is not adding correctly. The first page list the products, the second link shows their cart.



<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Raph & Mishal's Grocery</title>
</head>
<body>

<h1>Search for the products you want to buy:</h1>

<form method="get" action="listprod.php">
<input type="text" name="productName" size="50">
<input type="submit" value="Submit"><input type="reset" value="Reset"> (Leave blank for all products)
</form>

<?php
/** If you're developing locally on WINDOWS, uncomment the following line **/
//include 'includemoney_format_windows.php';
include 'include/db_credentials.php';

/** Get product name to search for **/

if (isset($_GET['productName'])){
$name = $_GET['productName'];
}

/** $name now contains the search string the user entered
Use it to build a query and print out the results. **/
/** Create and validate connection **/
global $username;
global $password;
global $database;
global $server;
global $connectionInfo;

$con = sqlsrv_connect($server, $connectionInfo);
if( $con === false ) {
die( print_r( sqlsrv_errors(), true));
}

/** Print out the ResultSet **/
$sql = "SELECT productId, productName,price FROM product";
// $preparepreparedStatement =null;
$name =null;
if(isset($_GET['productName'])){
$name = $_GET[urlencode('productName')];
$sql = $sql ." WHERE productName LIKE ?";
$name = "%". $name. "%";
//$preparedStatement = sqlsrv_prepare($con, $sql,$pName);
}

$results = sqlsrv_query($con, $sql, array($name));
echo("<table><tr><th></th><th>ProductName</th><th>Price</th></tr>");
while ($row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC )) {
echo( "<tr><td><a href=addcart.php?id=".$row['productId']."&name=".$row['productName']."&price=".$row['price'].">Add to Cart</h></td><td>" . $row['productName'] . "</td><td>" . $row['price'] . "</td></tr>");
}
echo("</table>");

sqlsrv_close( $con );
/**
For each product create a link of the form
addcart.php?id=<productId>&name=<productName>&price=<productPrice>
Note: As some product names contain special characters, you may need to encode URL parameter for product name like this: urlencode($productName)
**/

?>

</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title>Your Shopping Cart</title>
</head>
<body>

<?php
// include 'include/money_format_windows.php'; //Only required on windows PCs
// Get the current list of products
session_start();
$productList = null;
if (isset($_SESSION['productList'])){
$productList = $_SESSION['productList'];
echo("<h1>Your Shopping Cart</h1>");
echo("<table><tr><th>Product Id</th><th>Product Name</th><th>Quantity</th>");
echo("<th>Price</th><th>Subtotal</th></tr>");

$total =0;
foreach ($productList as $id => $prod) {
echo("<tr><td>". $prod['id'] . "</td>");
echo("<td>" . $prod['name'] . "</td>");

echo("<td align="center">". $prod['quantity'] . "</td>");
$price = $prod['price'];

echo("<td align="right">".str_replace("USD","$",money_format('%i',$price))."</td>");
echo("<td align="right">" . str_replace("USD","$",money_format('%i',$prod['quantity']*$price)) . "</td></tr>");
echo("</tr>");
$total = $total +$prod['quantity']*$price;
}
echo("<tr><td colspan="4" align="right"><b>Order Total</b></td><td align="right">".str_replace("USD","$",money_format('%i',$total))."</td></tr>");
echo("</table>");

echo("<h2><a href="checkout.php">Check Out</a></h2>");
} else{
echo("<H1>Your shopping cart is empty!</H1>");
}
?>
<h2><a href="listprod.php">Continue Shopping</a></h2>
</body>
</html>









share|improve this question




















  • 2




    You're mixing different apis here.
    – Funk Forty Niner
    Nov 10 at 22:49






  • 1




    Are you using mysql or sqlsrv? $name should be inside the array() in the sqlsrv_query call
    – user3783243
    Nov 10 at 22:49












  • $mysqli where is that defined? You have too many errors here and the api to connect with is unknown.
    – Funk Forty Niner
    Nov 10 at 22:50










  • Do you have a column named like a link? I think you want echo '<tr><td><a href="href=addcart.php?id=' . $row['id'] . '&name=' $row['....'] . etc
    – user3783243
    Nov 10 at 22:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Here is the website I am creating: http://cosc304.ok.ubc.ca/20647160/304_lab7/listprod.php
http://cosc304.ok.ubc.ca/20647160/304_lab7/showcart.php



I have never used PHP before, any guidance would be helpful. The code below showcases a website that is supposed to add to a cart but currently in the show cart there is a weird space between the first product and second product. It is not adding correctly. The first page list the products, the second link shows their cart.



<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Raph & Mishal's Grocery</title>
</head>
<body>

<h1>Search for the products you want to buy:</h1>

<form method="get" action="listprod.php">
<input type="text" name="productName" size="50">
<input type="submit" value="Submit"><input type="reset" value="Reset"> (Leave blank for all products)
</form>

<?php
/** If you're developing locally on WINDOWS, uncomment the following line **/
//include 'includemoney_format_windows.php';
include 'include/db_credentials.php';

/** Get product name to search for **/

if (isset($_GET['productName'])){
$name = $_GET['productName'];
}

/** $name now contains the search string the user entered
Use it to build a query and print out the results. **/
/** Create and validate connection **/
global $username;
global $password;
global $database;
global $server;
global $connectionInfo;

$con = sqlsrv_connect($server, $connectionInfo);
if( $con === false ) {
die( print_r( sqlsrv_errors(), true));
}

/** Print out the ResultSet **/
$sql = "SELECT productId, productName,price FROM product";
// $preparepreparedStatement =null;
$name =null;
if(isset($_GET['productName'])){
$name = $_GET[urlencode('productName')];
$sql = $sql ." WHERE productName LIKE ?";
$name = "%". $name. "%";
//$preparedStatement = sqlsrv_prepare($con, $sql,$pName);
}

$results = sqlsrv_query($con, $sql, array($name));
echo("<table><tr><th></th><th>ProductName</th><th>Price</th></tr>");
while ($row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC )) {
echo( "<tr><td><a href=addcart.php?id=".$row['productId']."&name=".$row['productName']."&price=".$row['price'].">Add to Cart</h></td><td>" . $row['productName'] . "</td><td>" . $row['price'] . "</td></tr>");
}
echo("</table>");

sqlsrv_close( $con );
/**
For each product create a link of the form
addcart.php?id=<productId>&name=<productName>&price=<productPrice>
Note: As some product names contain special characters, you may need to encode URL parameter for product name like this: urlencode($productName)
**/

?>

</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title>Your Shopping Cart</title>
</head>
<body>

<?php
// include 'include/money_format_windows.php'; //Only required on windows PCs
// Get the current list of products
session_start();
$productList = null;
if (isset($_SESSION['productList'])){
$productList = $_SESSION['productList'];
echo("<h1>Your Shopping Cart</h1>");
echo("<table><tr><th>Product Id</th><th>Product Name</th><th>Quantity</th>");
echo("<th>Price</th><th>Subtotal</th></tr>");

$total =0;
foreach ($productList as $id => $prod) {
echo("<tr><td>". $prod['id'] . "</td>");
echo("<td>" . $prod['name'] . "</td>");

echo("<td align="center">". $prod['quantity'] . "</td>");
$price = $prod['price'];

echo("<td align="right">".str_replace("USD","$",money_format('%i',$price))."</td>");
echo("<td align="right">" . str_replace("USD","$",money_format('%i',$prod['quantity']*$price)) . "</td></tr>");
echo("</tr>");
$total = $total +$prod['quantity']*$price;
}
echo("<tr><td colspan="4" align="right"><b>Order Total</b></td><td align="right">".str_replace("USD","$",money_format('%i',$total))."</td></tr>");
echo("</table>");

echo("<h2><a href="checkout.php">Check Out</a></h2>");
} else{
echo("<H1>Your shopping cart is empty!</H1>");
}
?>
<h2><a href="listprod.php">Continue Shopping</a></h2>
</body>
</html>









share|improve this question















Here is the website I am creating: http://cosc304.ok.ubc.ca/20647160/304_lab7/listprod.php
http://cosc304.ok.ubc.ca/20647160/304_lab7/showcart.php



I have never used PHP before, any guidance would be helpful. The code below showcases a website that is supposed to add to a cart but currently in the show cart there is a weird space between the first product and second product. It is not adding correctly. The first page list the products, the second link shows their cart.



<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Raph & Mishal's Grocery</title>
</head>
<body>

<h1>Search for the products you want to buy:</h1>

<form method="get" action="listprod.php">
<input type="text" name="productName" size="50">
<input type="submit" value="Submit"><input type="reset" value="Reset"> (Leave blank for all products)
</form>

<?php
/** If you're developing locally on WINDOWS, uncomment the following line **/
//include 'includemoney_format_windows.php';
include 'include/db_credentials.php';

/** Get product name to search for **/

if (isset($_GET['productName'])){
$name = $_GET['productName'];
}

/** $name now contains the search string the user entered
Use it to build a query and print out the results. **/
/** Create and validate connection **/
global $username;
global $password;
global $database;
global $server;
global $connectionInfo;

$con = sqlsrv_connect($server, $connectionInfo);
if( $con === false ) {
die( print_r( sqlsrv_errors(), true));
}

/** Print out the ResultSet **/
$sql = "SELECT productId, productName,price FROM product";
// $preparepreparedStatement =null;
$name =null;
if(isset($_GET['productName'])){
$name = $_GET[urlencode('productName')];
$sql = $sql ." WHERE productName LIKE ?";
$name = "%". $name. "%";
//$preparedStatement = sqlsrv_prepare($con, $sql,$pName);
}

$results = sqlsrv_query($con, $sql, array($name));
echo("<table><tr><th></th><th>ProductName</th><th>Price</th></tr>");
while ($row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC )) {
echo( "<tr><td><a href=addcart.php?id=".$row['productId']."&name=".$row['productName']."&price=".$row['price'].">Add to Cart</h></td><td>" . $row['productName'] . "</td><td>" . $row['price'] . "</td></tr>");
}
echo("</table>");

sqlsrv_close( $con );
/**
For each product create a link of the form
addcart.php?id=<productId>&name=<productName>&price=<productPrice>
Note: As some product names contain special characters, you may need to encode URL parameter for product name like this: urlencode($productName)
**/

?>

</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title>Your Shopping Cart</title>
</head>
<body>

<?php
// include 'include/money_format_windows.php'; //Only required on windows PCs
// Get the current list of products
session_start();
$productList = null;
if (isset($_SESSION['productList'])){
$productList = $_SESSION['productList'];
echo("<h1>Your Shopping Cart</h1>");
echo("<table><tr><th>Product Id</th><th>Product Name</th><th>Quantity</th>");
echo("<th>Price</th><th>Subtotal</th></tr>");

$total =0;
foreach ($productList as $id => $prod) {
echo("<tr><td>". $prod['id'] . "</td>");
echo("<td>" . $prod['name'] . "</td>");

echo("<td align="center">". $prod['quantity'] . "</td>");
$price = $prod['price'];

echo("<td align="right">".str_replace("USD","$",money_format('%i',$price))."</td>");
echo("<td align="right">" . str_replace("USD","$",money_format('%i',$prod['quantity']*$price)) . "</td></tr>");
echo("</tr>");
$total = $total +$prod['quantity']*$price;
}
echo("<tr><td colspan="4" align="right"><b>Order Total</b></td><td align="right">".str_replace("USD","$",money_format('%i',$total))."</td></tr>");
echo("</table>");

echo("<h2><a href="checkout.php">Check Out</a></h2>");
} else{
echo("<H1>Your shopping cart is empty!</H1>");
}
?>
<h2><a href="listprod.php">Continue Shopping</a></h2>
</body>
</html>






php html sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:52

























asked Nov 10 at 22:41









Mishal Hasan

92




92








  • 2




    You're mixing different apis here.
    – Funk Forty Niner
    Nov 10 at 22:49






  • 1




    Are you using mysql or sqlsrv? $name should be inside the array() in the sqlsrv_query call
    – user3783243
    Nov 10 at 22:49












  • $mysqli where is that defined? You have too many errors here and the api to connect with is unknown.
    – Funk Forty Niner
    Nov 10 at 22:50










  • Do you have a column named like a link? I think you want echo '<tr><td><a href="href=addcart.php?id=' . $row['id'] . '&name=' $row['....'] . etc
    – user3783243
    Nov 10 at 22:53














  • 2




    You're mixing different apis here.
    – Funk Forty Niner
    Nov 10 at 22:49






  • 1




    Are you using mysql or sqlsrv? $name should be inside the array() in the sqlsrv_query call
    – user3783243
    Nov 10 at 22:49












  • $mysqli where is that defined? You have too many errors here and the api to connect with is unknown.
    – Funk Forty Niner
    Nov 10 at 22:50










  • Do you have a column named like a link? I think you want echo '<tr><td><a href="href=addcart.php?id=' . $row['id'] . '&name=' $row['....'] . etc
    – user3783243
    Nov 10 at 22:53








2




2




You're mixing different apis here.
– Funk Forty Niner
Nov 10 at 22:49




You're mixing different apis here.
– Funk Forty Niner
Nov 10 at 22:49




1




1




Are you using mysql or sqlsrv? $name should be inside the array() in the sqlsrv_query call
– user3783243
Nov 10 at 22:49






Are you using mysql or sqlsrv? $name should be inside the array() in the sqlsrv_query call
– user3783243
Nov 10 at 22:49














$mysqli where is that defined? You have too many errors here and the api to connect with is unknown.
– Funk Forty Niner
Nov 10 at 22:50




$mysqli where is that defined? You have too many errors here and the api to connect with is unknown.
– Funk Forty Niner
Nov 10 at 22:50












Do you have a column named like a link? I think you want echo '<tr><td><a href="href=addcart.php?id=' . $row['id'] . '&name=' $row['....'] . etc
– user3783243
Nov 10 at 22:53




Do you have a column named like a link? I think you want echo '<tr><td><a href="href=addcart.php?id=' . $row['id'] . '&name=' $row['....'] . etc
– user3783243
Nov 10 at 22:53

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244142%2fphp-form-is-not-sending-data-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244142%2fphp-form-is-not-sending-data-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python