将表单提交保存为CSV文件

时间:2014-02-03 20:32:13

标签: php

您好我的代码出现语法错误,我正在尝试将表单的结果保存到csv文件中。错误发生在$ savestring行...解析错误:语法错误,意外','在第24行的/home/blahblah/public_html/forms/ContactUs/includes/contact.php

我看不出自己做错了什么,有人能指出我的错误吗?

{
$customername = $_POST['CustomerName'];
$companyname = $_POST['CompanyName'];
$noemps = $_POST['NoEmps'];
$telephone = $_POST['TelephoneNo'];
$email = $_POST['EmailAddress'];
$addressline1 = $_POST['AddressLine1'];
$addressline2 = $_POST['AddressLine2'];
$addressline3 = $_POST['AddressLine3'];
$city = $_POST['City'];
$county = $_POST['County'];
$postcode = $_POST['PostCode'];
$enquiry = $_POST['Enquiry'];
$upload = $_POST['upload'];
$source = $_POST['Source'];
$quotecode = $_POST['QuoteCode'];


$fp = fopen(”ContactUs.csv”, “a”);
$savestring = $customername . “,” . $companyname . "," . $noemps . "," . $telephone .     "," . $email . ",“ . $addressline1 . "," . $addressline2 . "," . $addressline3 . "," . $city . "," . $county . "," . $postcode . "," . $enquiry . "," . $upload . "," . $source . "," .$qoutecode . "\n”;
fwrite($fp, $savestring);
fclose($fp);

3 个答案:

答案 0 :(得分:1)

问题是您正在尝试声明具有特殊字符()的字符串。
尝试改变这个:

$customername . “,” . $companyname

对此:

$customername . "," . $companyname

此外,还有三个:

1)”ContactUs.csv”, “a” => "ContactUs.csv", "a"

2)$email . ",“ . $addressline1 => $email . "," . $addressline1

3)" .$qoutecode . "\n” => " .$qoutecode . "\n"

因此,您的新代码将是:

{
$customername = $_POST['CustomerName'];
$companyname = $_POST['CompanyName'];
$noemps = $_POST['NoEmps'];
$telephone = $_POST['TelephoneNo'];
$email = $_POST['EmailAddress'];
$addressline1 = $_POST['AddressLine1'];
$addressline2 = $_POST['AddressLine2'];
$addressline3 = $_POST['AddressLine3'];
$city = $_POST['City'];
$county = $_POST['County'];
$postcode = $_POST['PostCode'];
$enquiry = $_POST['Enquiry'];
$upload = $_POST['upload'];
$source = $_POST['Source'];
$quotecode = $_POST['QuoteCode'];


$fp = fopen("ContactUs.csv", "a");
$savestring = $customername . "," . $companyname . "," . $noemps . "," . $telephone .     "," . $email . "," . $addressline1 . "," . $addressline2 . "," . $addressline3 . "," . $city . "," . $county . "," . $postcode . "," . $enquiry . "," . $upload . "," . $source . "," .$qoutecode . "\n";
fwrite($fp, $savestring);
fclose($fp);

答案 1 :(得分:0)

问题就在这里:

",“

你看到引号的微小差异吗?这导致第一个引用,即有效引用,保持开放。

作为旁注,你正在使用“引号”,所以默认情况下你告诉解释器解析变量的所有字符串,所以你所做的常量连接是没有意义的。只需将变量直接放入一个字符串中会很好。

答案 2 :(得分:-1)

尝试这种方式

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>
相关问题