写入 CSV 时出错 - 警告:fopen() [function.fopen]:文件名不能为空

发布于 2024-12-14 01:47:22 字数 2032 浏览 5 评论 0原文

在我的网站上,用户填写表格进行注册,然后该信息将被添加到数据库中。我也试图将信息写入 CSV 文件。到目前为止,我的代码给了我这个错误:

警告:fopen()[function.fopen]:

第122行的/home/content/m/a/t/matronryan/html/sendmail3.php中的文件名不能为空 第122行: $fp = fopen($filename, "w");

我不知道问题出在哪里。这是代码:

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$stilldonate = $_POST['stilldonate']; // not required
$phone = $_POST['phone'];
$bid = $_POST['bid']; // required
$item = $_POST['item'];
$address1 = $_POST['address1']; // required
$address2 = $_POST['address2']; 
$city = $_POST['city'];
$county = $_POST['county']; // required
$postcode = $_POST['postcode']; // required
$updated = $_POST['updated']; 

include('db_functions.php');
    connect_to_db();
    $query="insert into auctionusers (firstname, lastname, address1, address2, city, county, postcode, email, phone, bid, stilldonate, item, updated) values ('$first_name', '$last_name', '$address1', '$address2', '$city', '$county', '$postcode', '$email_from', '$phone', '$bid', '$stilldonate', '$item', '$updated')";
    $result=mysql_query($query);


$filename == 'auctionusers.csv';
$fp = fopen($filename, "w");

$res = mysql_query("SELECT * FROM auctionusers");

// fetch a row and write the column names out to the file
$row = mysql_fetch_assoc($res);
$line = "";
$comma = "";
foreach($row as $name => $value) {
    $line .= $comma . '"' . str_replace('"', '""', $name) . '"';
    $comma = ",";
}
$line .= "\n";
fputs($fp, $line);

// remove the result pointer back to the start
mysql_data_seek($res, 0);

// and loop through the actual data
while($row = mysql_fetch_assoc($res)) {

    $line = "";
    $comma = "";
    foreach($row as $value) {
        $line .= $comma . '"' . str_replace('"', '""', $value) . '"';
        $comma = ",";
    }
    $line .= "\n";
    fputs($fp, $line);

}

fclose($fp);

mysql_close();


header('location: index2.php?op=Thank You&id=bid');

}
?>

任何人都可以看到出了什么问题吗?

On my site users fill out a form to register, this info then gets added to a database. I'm trying to get the info to be written to a CSV file also. The code I have so far gives me this error:

Warning: fopen() [function.fopen]: Filename cannot be empty in /home/content/m/a/t/matronryan/html/sendmail3.php on line 122

Line 122: $fp = fopen($filename, "w");

I can't work out what the problem is. Here's the code:

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$stilldonate = $_POST['stilldonate']; // not required
$phone = $_POST['phone'];
$bid = $_POST['bid']; // required
$item = $_POST['item'];
$address1 = $_POST['address1']; // required
$address2 = $_POST['address2']; 
$city = $_POST['city'];
$county = $_POST['county']; // required
$postcode = $_POST['postcode']; // required
$updated = $_POST['updated']; 

include('db_functions.php');
    connect_to_db();
    $query="insert into auctionusers (firstname, lastname, address1, address2, city, county, postcode, email, phone, bid, stilldonate, item, updated) values ('$first_name', '$last_name', '$address1', '$address2', '$city', '$county', '$postcode', '$email_from', '$phone', '$bid', '$stilldonate', '$item', '$updated')";
    $result=mysql_query($query);


$filename == 'auctionusers.csv';
$fp = fopen($filename, "w");

$res = mysql_query("SELECT * FROM auctionusers");

// fetch a row and write the column names out to the file
$row = mysql_fetch_assoc($res);
$line = "";
$comma = "";
foreach($row as $name => $value) {
    $line .= $comma . '"' . str_replace('"', '""', $name) . '"';
    $comma = ",";
}
$line .= "\n";
fputs($fp, $line);

// remove the result pointer back to the start
mysql_data_seek($res, 0);

// and loop through the actual data
while($row = mysql_fetch_assoc($res)) {

    $line = "";
    $comma = "";
    foreach($row as $value) {
        $line .= $comma . '"' . str_replace('"', '""', $value) . '"';
        $comma = ",";
    }
    $line .= "\n";
    fputs($fp, $line);

}

fclose($fp);

mysql_close();


header('location: index2.php?op=Thank You&id=bid');

}
?>

Can Anyone see whats wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不放开 2024-12-21 01:47:22

更改

$filename == 'auctionusers.csv';

为:

$filename = 'auctionusers.csv';

Double equal 对变量执行布尔运算。您一定错误地添加了一个额外的等号。

Change:

$filename == 'auctionusers.csv';

To:

$filename = 'auctionusers.csv';

Double equal performs a boolean operation upon the variable. You must've added an extra equals sign by mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文