0

我做网上商店,当我需要把我的购物车放在邮件上时我卡住了,我不知道如何调用功能购物车来把它放在邮件上,或者我做错了函数来把它放在邮件上

function cart1(){
    foreach($_SESSION as $name => $value){
        if ($value>0){
            if (substr($name, 0,5)=='cart_'){
                $id =substr($name, 5, (strlen($name)-5)); 
                $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string($id));
                //racuna kolko kosta
                while($get_row = mysql_fetch_assoc($get)){
                    $sub = $get_row['price']*$value;
                    echo $get_row{'name'}.' x '.$value.' Kom'.' = '.number_format($sub,2). ' kn';
                }
            }
            //zbraje 1 i 2i ... proizvod
            echo "<br />";


            $total += $sub;
            //echo "<hr width='150'>";
        }
    }
    if ($total==0){
    echo "Cart is empty.";
    }
    else{
    echo 'Total: '.number_format($total, 2).' kn';
    }
}

还有邮件功能,我知道客户信息但我不知道如何调用功能

<?php
require 'cart.php';
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "aaaa@hotmail.com";
    $email_subject = "Your email subject line";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Molimo Vas da upisete sva polja koja su oznacena *.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['adress']) ||
        !isset($_POST['post']) ||
        !isset($_POST['city']) ||
        !isset($_POST['email']) ||
        !isset($_POST['tel']) ||
        !isset($_POST['comments']))  {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $name = $_POST['name']; // required
    $adress = $_POST['adress']; // required
    $post = $_POST['post']; // not required
    $city = $_POST['city']; // required
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $comments = $_POST['comments'];
    $cart1 .= cart1();


    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'U didnt write email..<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'Niste upisali Ime i Prezime.<br />';
  }
  if(!preg_match($string_exp,$oib)) {
    $error_message .= 'Niste upisali OIB.<br />';
  }
  if(!preg_match($string_exp,$adress)) {
    $error_message .= 'U didnt write adress.<br />';
  }
  if(!preg_match($string_exp,$post)) {
    $error_message .= 'U didnt write Post number.<br />';
  }
  if(!preg_match($string_exp,$city)) {
    $error_message .= 'U didnt write city.<br />';
  }
  if(!preg_match($string_exp,$tel)) {
    $error_message .= 'U didnt write Tel.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'U didnt write comment.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First name/lastname: ".clean_string($name)."\n";
    $email_message .= "Adres: ".clean_string($adress)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "Post: ".clean_string($post)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Tel: ".clean_string($tel)."\n";
    $email_message .= "Fax: ".clean_string($fax)."\n";
    $email_message .= "Comment: ".clean_string($comments)."\n";
    $email_message =  cart1();




// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Tenx to buying here!<br />
<?php
}
?>
4

1 回答 1

0

[1] cart1() is not returning any value. Instead of echo, create a variable to hold/concatenate the string being returned, use: return $str_out;

[2] Suggest that you use the new mysqli protocol for php (with prepared statements) http://php.net/manual/en/mysqli.prepare.php

[3] Suggest that you incorporate error handlers in your code to catch exceptions.

于 2013-03-06T01:39:40.117 回答