Jump to content


Photo

2 Echo tags not working


  • Please log in to reply
2 replies to this topic

#1 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 28 September 2006 - 06:37 PM

Hi,

I'm playing around with a shopping script, but cannot seem to get two echo functions to work.
I have a form with 13 fields, 11 of them pick up the data from the database ok, but orderid and products_purchased dont work. (Script is below)

<?php
session_start();
$connect = mysql_connect ("localhost", "mysqlusername", "mysqlpassword") or
die ("Connection to Database Failed.");

mysql_select_db("testdb");

//Let's make the variables easy to access in our queries
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$city = $_POST['city'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
$total = $_POST['total'];
$sessid = session_id();
$today = date("Y-m-d");

//1) Assign Customer Number to new Customer, or find existing customer number
$query = "SELECT * FROM customers WHERE
          (customers_firstname = '$firstname' AND
          customers_lastname = '$lastname' AND
          customers_add1 = '$add1' AND
          customers_add2 = '$add2' AND
          customers_city = '$city')";
$results = mysql_query($query)
  or (mysql_error());
$rows = mysql_num_rows($results);

if ($rows < 1) {
  //assign new custnum
  $query2 = "INSERT INTO customers (
             customers_firstname, customers_lastname, customers_add1,
             customers_add2, customers_city, customers_county, 
             customers_postcode, customers_phone, customers_fax, 
             customers_email)
             VALUES (
            '$firstname',
            '$lastname',
            '$add1',
            '$add2',
            '$city',
            '$county',
            '$postcode',
            '$phone',
            '$fax',
            '$email')";
  $insert = mysql_query($query2)
    or (mysql_error());
  $custid = mysql_insert_id();
}

//If custid exists, we want to make it equal to custnum
//Otherwise we will use the existing custnum
if ($custid) {
  $customers_custnum = $custid;
}

//2) Insert Info into ordermain
//determine shipping costs based on order total (25% of total)
$shipping = $total * 0.25;

$query3 = "INSERT INTO ordermain (
           ordermain_orderdate, ordermain_custnum,
           ordermain_subtotal,ordermain_shipping,
           ordermain_shipfirst, ordermain_shiplast,
           ordermain_shipadd1, ordermain_shipadd2,
           ordermain_shipcity, ordermain_shipcounty,
           ordermain_shippostcode, ordermain_shipphone,
           ordermain_shipemail)
           VALUES (
           '$today',
           '$customers_custnum',
           '$total',
           '$shipping'
           '$shipfirst',
           '$shiplast',
           '$shipadd1',
           '$shipadd2',
           '$shipcity',
           '$shipcounty',
           '$shippostcode',
           '$shipphone',
           '$shipemail')";
$insert2 = mysql_query($query3)
  or (mysql_error());
$orderid = mysql_insert_id();

//3) Insert Info into orderdet
//find the correct cart information being temporarily stored
$query = "SELECT * FROM carttemp WHERE carttemp_sess='$sessid'";
$results = mysql_query($query)
  or (mysql_error());

//put the data into the database one row at a time
while ($row = mysql_fetch_array($results)) {
  extract($row);
  $query4 = "INSERT INTO orderdet (
             orderdet_ordernum, orderdet_qty, orderdet_prodnum)
             VALUES (
             '$orderid',
             '$carttemp_quan',
             '$carttemp_prodnum')";
  $insert4 = mysql_query($query4)
    or (mysql_error());
}

//4)delete from temporary table
$query = "DELETE FROM carttemp WHERE carttemp_sess='$sessid'";
$delete = mysql_query($query);


//5)email confirmations to us and to the customer
/* recipients */
$to = "<" . $email .">";

/* subject */
$subject = "Order Confirmation";

/* message */
  /* top of message */
  $message = "
    <html>
    <head>
    <title>Order Confirmation</title>
    </head>
    <body>
    Here is a recap of your order:<br><br>
    Order date: ";
  $message .= $today;
  $message .= "
    <br>
    Order Number: ";
  $message .= $orderid;
  $message .= "
    <table width=\"50%\" border=\"0\">
      <tr>
        <td>
          <p>Bill to:<br>";
  $message .= $firstname;
  $message .= " ";
  $message .= $lastname;
  $message .= "<br>";
  $message .= $add1;
  $message .= "<br>";
  if ($add2) {
    $message .= $add2 . "<br>";
  }
  $message .= $city . ", " . $county . "  " . $postcode;
  $message .= "</p></td>
    <td>
      <p>Ship to:<br>";
  $message .= $shipfirst . " " . $shiplast;
  $message .= "<br>";
  $message .= $shipadd1 . "<br>";
  if ($shipadd2) {
    $message .= $shipadd2 . "<br>";
  }
  $message .= $shipcity . ", " . $shipcounty . "  " . $shippostcode;
  $message .= "</p>
        </td>
      </tr>
    </table>
    <hr width=\"250px\" align=\"left\">
    <table cellpadding=\"5\">";

//grab the contents of the order and insert them
//into the message field

$query = "SELECT * FROM orderdet WHERE orderdet_ordernum = '$orderid'";
$results = mysql_query($query)
  or die (mysql_query());
while ($row = mysql_fetch_array($results)) {
  extract($row);
  $prod = "SELECT * FROM products
           WHERE products_prodnum = '$orderdet_prodnum'";
  $prod2 = mysql_query($prod);
  $prod3 = mysql_fetch_array($prod2);
  extract($prod3);
  $message .= "<tr><td>";
  $message .= $orderdet_qty;
  $message .= "</td>";
  $message .="<td>";
  $message .= $products_name;
  $message .= "</td>";
  $message .= "<td align=\"right\">";
  $message .= $products_price;
  $message .= "</td>";
  $message .= "<td align=\"right\">";
  //get extended price
  $extprice = number_format($products_price * $orderdet_qty, 2);
  $message .= $extprice;
  $message .= "</td>";
  $message .= "</tr>";
}

  $message .= "<tr>
    <td colspan=\"3\" align=\"right\">
      Your total before shipping is:
    </td>
    <td align=\"right\">";
  $message .= number_format($total, 2);
  $message .= "
      </td>
    </tr>
    <tr>
      <td colspan=\"3\" align=\"right\">
        Shipping Costs:
      </td>
      <td align=\"right\">";
  $message .= number_format($shipping, 2);
  $message .= "
      </td>
    </tr>
    <tr>
      <td colspan=\"3\" align=\"right\">
        Your final total is:
      </td>
      <td align=\"right\"> ";
  $message .= number_format(($total + $shipping), 2);
  $message .= "
        </td>
      </tr>
    </table>
    </body>
    </html>";

/* headers */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: <storeemail@email.com>\r\n";
$headers .= "Cc: <storeemail@email.com>\r\n";
$headers .= "X-Mailer: PHP / ".phpversion()."\r\n";

/* mail it */
mail($to, $subject, $message, $headers);


//6)show them their order & give them an order number

echo "Step 1 - Please Enter Billing and Shipping Information<br>";
echo "Step 2 - Please Verify Accuracy and Make Any Necessary Changes<br>";
echo "<strong>Step 3 - Order Confirmation and Receipt</strong><br><br>";

echo $message;

?>


<form action="action.php" method=POST>
      OrderID <input type="text" name="cartId" value="<?php echo $_POST['orderid']; ?>"><br />
      Names<input name="name" type="text" value="<?php echo $_POST['firstname']; ?> <?php echo $_POST['lastname']; ?>" maxlength="500"><br />
	  Products Purchased<input type="text" name="desc" value="<?php echo $_POST['products_name']; ?>"><br />
	    Email <input name="email" type="text" value="<?php echo $_POST['email']; ?>" maxlength="50">    </td><br />
	  <input type=submit value="Confirm Order">
</form>

I've cut out some of the form which is working ok, but orderid and products_name isnt being echoed into the form from above, however when it runs orderid and products_name work ok in the section within the first large php tag.
Anyone able to see what I'm doing wrong? The only thing I can think of is that its looking at the wrong database table, therefor not finding it?

Any help greatly appreciated.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 September 2006 - 07:06 PM

Unless I'm mistaken, those values are in the $_POST array.  I only saw those in your database.

Info: PHP Manual


#3 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 28 September 2006 - 08:11 PM

The post array is there to pass the data onto a payment system (such as World Pay etc) but all I'm doing is passing it onto a new page, all of the items post correctly apart from orderid and product_name fields.
i.e all what I'm doing at the bottom is summing it up ready to pass on.

However on that page it shows the user their "order" and the orderid and product_name work ok  until it gets to the bottom form ready to be passed onto the next stage ???


I've taken a screenshot of the results that it produces, you'll see that it has an order number and the product name is listed in the top half, but the form at the bottom, which is collecting the data ready to pass on it isnt showing, where as everything else is working:

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users