Porblem with mysql update from php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ipodman
Forum Newbie
Posts: 3
Joined: Thu Feb 05, 2009 6:17 am
Contact:

Porblem with mysql update from php

Post by ipodman »

Hey guys,

Well my knowledge of php is expanding slow but I have now come accross a problem that I cannnot solve, after trying I cannot do it.

I have setup a little shopping cart and final.php is the form that submits the orders to the database, it submits the customer details but not the order details.

Below are 3 pages that are connected with the final.php

checkout.php

Code: Select all

<?php
session_id();
session_start();
include_once("config.php");
@include('header.php');
 
?>
<HTML>
<HEAD>
<TITLE>Check everything</TITLE>
</HEAD>
<BODY>
<div align="center"><br>
  
Please Confirm Order and Add Your Details </div>
<form method="post" action="final.php">
  <table width="300" border="1" align="left">
    <tr>
      <td width="50%">
        <div align="right">First Name</div>      </td>
      <td width="50%">
        <input type="text" name="firstName" maxlength="15"
          value="<?php echo $_POST['firstName']?> ">        </td>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">Last Name</div>      </td>
      <td width="50%">
        <input type="text" name="lastName" maxlength="50"
          value="<?php echo $_POST['lastName']?>">      </td>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">Address</div>      </td>
      <td width="50%">
        <input type="text" name="addy" maxlength="50"
           value="<?php echo $_POST['addy']?>">      </td>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">Town</div>      </td>
      <td width="50%">
        <input type="text" name="town" maxlength="50"
           value="<?php echo $_POST['town']?> ">      </td>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">Postcode</div>      </td>
      <td width="50%">
        <input type="text" name="postcode" maxlength="5" size="5"
               value="<?php echo $_POST['postcode']?>">      </td>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">Phone Number</div>      </td>
      <td width="50%">
        <input type="text" name="phone" size="12" maxlength="12"
          value="<?php echo $_POST['phone']?>">      </td>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">E-Mail Address</div>      </td>
      <td width="50%">
        <input type="text" name="email" maxlength="50"
          value="<?php echo $_POST['email']?>">      </td>
    </tr>
  </table>
  <table border="1" align="left" cellpadding="5">
      <tr>
           <td>Quantity</td>
           <td>Item Name</td>
           <td>Price Each</td>
           <td>Extended Price</td>
           <td></td>
           <td></td>
     </tr>
      <tr>
      <?php
       $sessid = session_id();
       $query = "SELECT * FROM temp WHERE sess = '$sessid'";
      $results = mysql_query($query)
          or die (mysql_query());
           while ($row = mysql_fetch_array($results)) {
               extract ($row);
               $prod = "SELECT * FROM stock WHERE
                    pro_id = '$pro_id'";
               $prod2 = mysql_query($prod);
               $prod3 = mysql_fetch_array($prod2);
               extract ($prod3);
               echo "<td>";
               echo $quantity;
               echo "</td>";
               
          
               echo "<td>";
               echo "<a href = 'getprod.php?pro_id=" .
                    $pro_id ."'>";
               echo $name;
               echo "</td></a>";
               echo "<td align='right'>";
               echo $sale_price;
               echo "</td>";
               echo "<td align='right'>";
               $extprice = number_format($sale_price * $quantity, 2);
               echo $extprice;
               echo "</td>";
               echo "<td>";
               echo "</td>";
               echo "</tr>";
               $total = $extprice + $total;
 
               }
?>
<tr>
<td colspan='4' align='right'>Your total before shipping is:</td>
<td align='right'> <?php echo number_format($total, 2) ?></td>
<td></td>
<td></td>
</tr>
</table>
<input type="hidden" name="total" value="<?php echo $total?>">
  <p>
    <input type="submit" name="Submit" value="FINAL ORDER ">
  </p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</form>
<?php
@include('footer.php');
 
?>
 
</BODY>
</HTML>
temp.php is where the sessions are stored

Code: Select all

<?php
session_id();
session_start();
include_once("config.php");
 
     
mysql_select_db ("site");
$quantity =$_POST['quantity'];
$pro_id = $_POST['pro_id'];
$sess =session_id();
 
$query = "INSERT INTO temp (sess, quantity, pro_id)
          VALUES ('$sess','$quantity','$pro_id')";
$results = mysql_query($query)
     or die(mysql_error());
 
include("cart.php");
?>
final.php is where all the details are passed to the database

Code: Select all

<?php
session_id();
session_start();
@include('header.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
 
include_once("config.php");
 
//varibles from display.php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$firstName = $_POST['firstName'];
$addy = $_POST['addy'];
$town = $_POST['town'];
$postcode = $_POST['postcode'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$total = $_POST['total'];
$sessid = session_id();
$today = date("Y-m-d");
 
//custome numbers
     $query = "SELECT * FROM customer WHERE
          (firstName = '$firstName' AND
          lastName = '$lastName' AND
          addy = '$addy' AND
          town = '$town')";
     $results = mysql_query($query)
          or (mysql_error());
     $rows = mysql_num_rows($results);
 
     if ($rows < 1) {
          //new id
          $query2 = "INSERT INTO customer (
          firstName, lastName, addy, town, postcode, phone, email)
           VALUES (
          '$firstName',
          '$lastName',
          '$addy',
          '$town',
          '$postcode',
          '$phone',
          '$email')";
          $insert = mysql_query($query2)
               or (mysql_error());
          $custid = mysql_insert_id();
     }
     //if we have the id then use same details
     if($custid) $custnum = $custid;
 
//find cart info from temp
     $query = "SELECT * from temp WHERE sess='$sessid'";
     $results = mysql_query($query)
          or (mysql_error());
 
     //order into database
     while ($row = mysql_fetch_array($results)) {
               extract ($row);
               $query4 = "INSERT INTO order ( quantity, pro_id)
                    VALUES (
                    '$quantity',
                    '$pro_id')";
               $insert4 = mysql_query($query4)
                    or (mysql_error());
            $order_id = mysql_insert_id();
 
     }
 
//remove from temp
     $query="DELETE FROM temp WHERE sess='$sessid'";
     $delete = mysql_query($query);
 
 
 
//details
?>
 
<HTML>
<HEAD>
<TITLE>Thank you for your order!</TITLE>
</HEAD>
<BODY>
<br>
<br>
 
Thank you for your order!<br><br>
Your order number is <?php echo $order_id ?>. Please print this page or retain
this number for your records.<br>
<br>
Here is a recap of your order:<br><br>
Order date: <?php echo $today ?><br>
<hr noshade width='250px' align='left'>
</BODY>
</HTML>
Cheers
webgrrl
Forum Newbie
Posts: 8
Joined: Mon Apr 13, 2009 4:27 pm

Re: Porblem with mysql update from php

Post by webgrrl »

I think your problem might be that you are not including the custId when you are inserting in the orders table, since I'm sure you have that feild as required so you can track which orders are tied to which customers. Try that and see if it works first and then if it doesn't I'll go back and look closer for you.
Post Reply