所以我有一个php post进程将用户输入放入csv。我添加了一个if / else语句,因此每次有用户提交时我的标题都不会被添加,但现在我的进程不会运行。我不确定我在哪里出错,因为我是PHP的新手。代码如下。
<?php
$headers = array('FirstName','LastName','Initials','Number','Department','Password');
$user = array($_POST['firstname'],$_POST['lastname'],$_POST['initials'],$_POST['number'],$_POST['department'],$_POST['password']);
$filename = 'newusers.csv';
$fp = fopen($filename,'a');
$op1 = fputcsv($fp,$headers);
$op2 = fputcsv($fp,$user); //pass $_POST and filehandle to fputcsv
fclose($fp);
if file_exists($filename) {
fputcsv($fp,$user);
}
else {
fputcsv($fp,$headers);
fputcsv($fp,$user);
}
if( $op1 && $op2 ) {
$redirect = "Location: //redirecthere//";
} else {
$redirect = "Location: //redirect here//";
}
header($redirect);
die();
?>
我认为我的问题在于我的重定向语句中的op1和op2变量,但我不确定。我也想知道我的其他陈述是否写错了。或者我在某处错过了分号?提前致谢
答案 0 :(得分:0)
您需要使用
if (file_exists($filename)) {
而不是
if file_exists($filename) {
答案 1 :(得分:0)
使用时如果必须始终在括号内有条件
if file_exists($filename)
应更改为if(file_exists($filename))
答案 2 :(得分:0)
我做了一些修改。尝试一下,它应该工作
$user = array($_POST['firstname'],$_POST['lastname'],$_POST['initials'],$_POST['number'],$_POST['department'],$_POST['password']);
$filename = 'newusers.csv';
if (file_exists($filename)) {
$fp = fopen($filename,'a');
$ok = fputcsv($fp,$user);
} else {
$fp = fopen($filename,'w');
$headers = array('FirstName','LastName','Initials','Number','Department','Password');
$ok = fputcsv($fp,$headers) && fputcsv($fp,$user);
}
fclose($fp);
if ( $ok ) {
$redirect = "Location: http://128.222.4.24/custom/owen/success.html";
} else {
$redirect = "Location: http://128.222.4.24/custom/owen/error.html";
}
header($redirect);
die();