PHP:窗体未正确发送数据

时间:2018-11-10 22:41:54

标签: php html sql

这是我正在创建的网站:http://cosc304.ok.ubc.ca/20647160/304_lab7/listprod.php http://cosc304.ok.ubc.ca/20647160/304_lab7/showcart.php

我以前从未使用过PHP,任何指导都将有所帮助。下面的代码展示了一个应该添加到购物车中的网站,但是当前在购物车中,第一个产品和第二个产品之间存在一个奇怪的空间。它添加不正确。第一页列出产品,第二个链接显示其购物车。

<!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 'include\money_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> 

0 个答案:

没有答案