2012-04-17 145 views
1

基本上我一个审查表上的工作中,用户填写有关特定产品项目,并提交表单时,它调用process.php这验证表格,当一个人会发邮件给我提交没有错误。然后,它将用户返回到表单页面,并显示已提交的错误或者说它已成功提交,这一切都很好,但我现在需要做的是在表单填写正确时我仍然希望电子邮件和返回到表单的页面,但也插入数据到我的数据库。我的表单验证的伟大工程,直到我尝试在弹出一些代码插入到数据库,然后我得到这些错误......验证我的PHP形式提交到数据库

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/XZXZ/support/database.inc:16) in /home/XZXZ/public_html/Reviews/process.php on line 113 

Warning: Cannot modify header information - headers already sent by (output started at /home/XZXZ/support/database.inc:16) in /home/XZXZ/public_html/Reviews/process.php on line 117 

我只需要知道在哪里可以在弹出这个以避免这些错误仍然有一切正常工作。下面来看看在process.php文件:

<?php 

    if(isset($_POST)){ 

     //form validation vars 
     $formok = true; 
     $errors = array(); 

     //sumbission data 
     $ipaddress = $_SERVER['REMOTE_ADDR']; 
     $sub_date = date('d/m/Y'); 
     $sub_time = date('H:i:s'); 

     //form data 
     $sub_date = $_POST['sub_date']; 
     $sub_time = $_POST['sub_time']; 
     $review_title = $_POST['review_title']; 
     $rating = $_POST['rating']; 
     $pros = $_POST['pros']; 
     $cons = $_POST['cons']; 
     $best_uses = $_POST['best_uses']; 
     $comments = $_POST['comments']; 
     $upload = $_POST['upload']; 
     $recommend = $_POST['recommend']; 
     $reviewer_name = $_POST['reviewer_name']; 
     $reviewer_desc = $_POST['reviewer_desc']; 
     $reviewer_loc = $_POST['reviewer_loc']; 



     //form validation to go here.... 

    } 

     //validate review title is not empty 
    if(empty($review_title)){ 
     $formok = false; 
     $errors[] = "You have not entered a title for this review"; 
    } 

     //validate rating is selected 
    if (isset ($_POST['rating']) && ($_POST['rating'] == '')) { 
     $formok = FALSE; 
     $errors[] = "You have not selected a rating for the product"; 
    } 

     //validate pros is not empty 
    if(empty($pros)){ 
     $formok = false; 
     $errors[] = "You have not entered any pros"; 
    } 

     //validate cons is not empty 
    if(empty($cons)){ 
     $formok = false; 
     $errors[] = "You have not entered any cons"; 
    } 

     //validate name is not empty 
    if(empty($reviewer_name)){ 
     $formok = false; 
     $errors[] = "You have not entered your name"; 
    } 

     //validate desc is not empty 
    if(empty($reviewer_desc)){ 
     $formok = false; 
     $errors[] = "You have not entered your description"; 
    } 

     //validate location is not empty 
    if(empty($reviewer_loc)){ 
     $formok = false; 
     $errors[] = "You have not entered your location"; 
    } 

     //send email if all is ok 
    if($formok){ 

     $headers = "From: [email protected]" . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

     $emailbody = "<p>You have received a new product review pending approval from XZXZ.com</p> 
         <p><strong>Review Title: </strong> {$review_title} </p> 
         <p><strong>Rating: </strong> {rating} </p> 
         <p><strong>Pros: </strong> {$pros} </p> 
         <p><strong>Cons: </strong> {$cons} </p> 
         <p><strong>Best Uses: </strong> {$best_uses} </p> 
         <p><strong>Comments: </strong> {$comments} </p> 
         <p><strong>Upload: </strong> {$upload} </p> 
         <p><strong>Recommend: </strong> {$recommend} </p> 
         <p><strong>Name: </strong> {$reviewer_name} </p> 
         <p><strong>Description: </strong> {$reviewer_desc} </p> 
         <p><strong>Location: </strong> {$reviewer_loc} </p> 
         <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>"; 

     mail("[email protected]","New Pending Review",$emailbody,$headers); 

     //insert to database  

     require("/home/XZXZ/support/database.inc"); 

     $SQL="INSERT INTO 'XZXZ_rvs'.'reviews_prod' (sub_date, sub_time, review_title, rating, pros, cons, best_uses, comments, upload, recommend, reviewer_name, reviewer_desc, reviewer_loc) VALUES ('$_POST[$sub_date]','$_POST[$sub_time]','$_POST[$review_title]','$_POST[$rating]','$_POST[$pros]','$_POST[$cons]','$_POST[$best_uses]','$_POST[$comments]','$_POST[$upload]','$_POST[$recommend]','$_POST[$reviewer_name]','$_POST[$reviewer_desc]','$_POST[$reviewer_loc]')"; 

    } 
     //what we need to return back to our form 
    $returndata = array( 
     'posted_form_data' => array( 
      'review_title' => $review_title, 
      'rating' => $rating, 
      'pros' => $pros, 
      'cons' => $cons, 
      'best_uses' => $best_uses, 
      'comments' => $comments, 
      'upload' => $upload, 
      'recommend' => $recommend, 
      'reviewer_name' => $reviewer_name, 
      'reviewer_desc' => $reviewer_desc, 
      'reviewer_loc' => $reviewer_loc 
     ), 
     'form_ok' => $formok, 
     'errors' => $errors 
    ); 
    //if this is not an ajax request 
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){ 

    //set session variables 
    session_start(); 
    $_SESSION['cf_returndata'] = $returndata; 

    //redirect back to form 
    header('location: ' . $_SERVER['HTTP_REFERER']); 

} 
+0

之前,你必须把会话开始请请请考虑将扩展'.php'您database.inc文件,如果有人在浏览器中导航到该文件,它将以计划文本形式输出,显示所有数据库连接参数! – Dale 2012-04-17 20:59:38

+1

就做到了,谢谢你的提示,我继承了这个项目,并通常与数据块只编码一切工作在同一个PHP文件时,而不是使用需要 – forevermetal02 2012-04-18 13:13:06

+0

@Dale,这未必是真实的。设置'.inc'文件很容易被处理为PHP(并且不向访问者显示源代码)。但是,如果您将某个服务器移动到不支持'.htaccess'的服务器(或.htaccess文件未被传输),则将该扩展名保留为默认值('.php')当使用FTP时,由于它被默认隐藏)。 – 0b10011 2012-04-18 15:55:40

回答

1

您必须添加脚本以前什么发送到包括空行或空格,浏览器。如果您关闭这个脚本之前包含你的PHP脚本文件(?>),和你的编辑器,FTP客户端或其他应用程序添加一个空行到文件末尾(这是常见的),这可能会发生事故。为防止这种情况发生,只需将PHP脚本保留为开放式(如果整个文件都是PHP,则不需要使用?>)。它也可能是一个流浪echo(或类似的,例如printprint_var等)引起的问题。

在你的警告,它看起来像这样在/home/XZXZ/support/database.inc发生的事情就行16

此外,在SAURABH为感动,你必须die();header("Location: ...");作为header()功能不会将用户重定向 - 它仅发送Location: ...作为头添加到浏览器中,并让它到浏览器完成剩下的工作 - 然而,服务器并不知道你正在重定向用户,所以它会继续运行脚本。因此,您必须终止脚本以防止执行任何其他代码。

+0

还要注意''session_start()'之前包含的文件的内容。 – 2012-04-17 20:29:20

0

你能不能也请分享一下你在/home/XZXZ/support/database.inc。我没有看到你正在执行查询。但看看这些错误,我觉得/home/XZXZ/support/database.inc正试图设置重定向。此外它的一个很好的做法,把一个die()重定向使用header

+0

Saurabh,这是database.inc文件... <? /************************************************ ********** *数据库连接信息* ********************************* *************************/ $ servernme =“XXXX”; $ userid =“XXXX”; $ passwd =“XXXX”; $ db \t =“XXXX”;连接到数据库 $ cid = @mysql_connect if(!$ cid){echo(“ERROR:”。mysql_error()。“\ n”); } ?> – forevermetal02 2012-04-17 20:49:14

+0

我觉得可能你无法连接数据库,因此它试图回显错误。检查主机名,用户名和密码。确保mysql正在运行。同时确保你选择了Db,这在代码中没有看到。 – 2012-04-17 21:50:26