2012-06-28 4 views
0

정말 고민 중입니다. viewdetails.php 페이지, addnew.php 페이지 및 php.php가 있습니다. php.php 페이지에는 두 페이지에 대한 내 쿼리가 있습니다. 먼저 viewdetails.php에 결과를 표시하는 쿼리가 있고 두 번째는 addnew.php 페이지에 결과를 표시하는 쿼리입니다. 내가 겪고있는 문제는 브라우저에서 addnew.php를 열면 dbandpassword.php에 대한 경로와 viewdetails.php에 대한 쿼리의 다른 항목에 대한 오류가 발생한다는 것입니다.동일한 파일에서 쿼리를 분리하는 방법

어떻게 이러한 모든 쿼리를 한 페이지 (php.php)에 보관하고 별도로 유지하여 오류가 발생하지 않습니까? 두 쿼리를 함께 결합해야합니까 아니면 일부를 함께 결합해야합니까? 코드를 정리해야한다는 것을 알고 있습니다. 지금 당장 걱정할 필요가 없습니다. 나는 또한 쿼리가 각각 자신의 파일 (php.php, php2.php)에 넣어도 개별적으로 작동한다는 것을 알고있다.

<?php 

ob_start(); 
require("../admin/dbandpassword.php"); 
ob_end_clean(); 

// FETCH LEAD INFORMATION - this is query for viewdetails.php 

$query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer/Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC"); 

$result = $mysqli->query($query) or die ("Error: ".mysqli_error($mysqli,$query)); 
while ($row = $result->fetch_array()) { 

$firstname = $row ['firstname'];  
$lastname = $row['lastname']; 
$ID = $row['ID']; 
$partner = $row['spousefirst']; 
$phonecell = $row['phonecell']; 
$email = $row['email']; 
$date = $row['date']; 
$contacttype = $row['contacttype']; 
$agentassigned = $row['agentassigned']; 
$leadstatus = $row['leadstatus']; 

echo' 
    <table> 
    <tbody>  
    <tr> 
     <td width="10"><input type="checkbox" name="" id="" value="'.$ID.'"></td> 
     <td><a href="/backend/leads/view/?ID='.$ID.'"><strong>'.$firstname.' '.$lastname.'</strong></a></td> 
     <td><a href="/backend/leads/view/?ID='. $ID.'">'.$partner.'</a></td> 
     <td>'.$phonecell.'</td> 
     <td><a href="mailto:'. $email.'">'.$email.'</a></td> 
     <td>'.date("M jS, g:i A", strtotime($date)).'</td> 
     <td>'.$contacttype.'</td> 
     <td>'.$agentassigned.'</td> 
     <td>'.$leadstatus.'</td> 
     <td><a href="/backend/contacts/notes.php?ID='.$ID.'">View </a>+</td> 
     <td><a href="/backend/contacts/todo.php?ID='.$ID.'">View </a>+</td> 
     <td><a href="/backend/contacts/deletesuccess.php?ID='.$ID.'">D</a></td> 
     </tr> 
     </tbody> 
     </table>'; 
     } 

// ADD LEAD - this would be for addnew.php page 

     if (isset($_POST['firstname'])) 
     { 

     require("../../admin/dcandpassword.php"); // NOTE THE DIFFERENT PATH THAN ABOVE BECAUSE addnew.php IS A FOLDER DEEPER 

     $ID = $_POST['ID']; 
     $date = mysqli_real_escape_string($con,$_POST['NOW()']); 
     $firstname = mysqli_real_escape_string($mysqli,$_POST['firstname']); 
     $lastname = mysqli_real_escape_string($mysqli,$_POST['lastname']); 
     $spousefirst = mysqli_real_escape_string($mysqli,$_POST['spousefirst']); 
     $spouselast = mysqli_real_escape_string($mysqli,$_POST['spouselast']); 
     $primarybday = mysqli_real_escape_string($mysqli,$_POST['primarybday']); 
     $spousebday = mysqli_real_escape_string($mysqli,$_POST['spousebday']); 
     $phonecell = mysqli_real_escape_string($mysqli,$_POST['phonecell']); 
     $phonehome = mysqli_real_escape_string($mysqli,$_POST['phonehome']); 
     $phoneoffice = mysqli_real_escape_string($mysqli,$_POST['phoneoffice']); 
     $spousecell = mysqli_real_escape_string($mysqli,$_POST['spousecell']); 
     $phoneother = mysqli_real_escape_string($mysqli,$_POST['phoneother']); 
     $email = mysqli_real_escape_string($mysqli,$_POST['email']); 
     $emailspouse = mysqli_real_escape_string($mysqli,$_POST['emailspouse']); 
     $emailother = mysqli_real_escape_string($mysqli,$_POST['emailother']); 
     $emailspouseother = mysqli_real_escape_string($mysqli,$_POST['emailspouseother']); 
     $address = mysqli_real_escape_string($mysqli,$_POST['address']); 
     $suite = mysqli_real_escape_string($mysqli,$_POST['suite']); 
     $city = mysqli_real_escape_string($mysqli,$_POST['city']); 
     $state = mysqli_real_escape_string($mysqli,$_POST['state']); 
     $zipcode = mysqli_real_escape_string($mysqli,$_POST['zipcode']); 
     $addressother = mysqli_real_escape_string($mysqli,$_POST['addressother']); 
     $suiteother = mysqli_real_escape_string($mysqli,$_POST['suiteother']); 
     $cityother = mysqli_real_escape_string($mysqli$_POST['cityother']); 
     $stateother = mysqli_real_escape_string($mysqli,$_POST['stateother']); 
     $zipcodeother = mysqli_real_escape_string($mysqli,$_POST['zipcodeother']); 
     $agentassigned = mysqli_real_escape_string($mysqli,$_POST['agentassigned']); 
     $contacttype = mysqli_real_escape_string($mysqli,$_POST['contacttype']); 
     $contactstatus = mysqli_real_escape_string($mysqli,$_POST['contactstatus']); 
     $leadstatus = mysqli_real_escape_string($mysqli,$_POST['leadstatus']); 
     $contactsource = mysqli_real_escape_string($mysqli,$_POST['contactsource']); 
     $timing = mysqli_real_escape_string($mysqli,$_POST['timing']); 
     $password = mysqli_real_escape_string($mysqli,$_POST['password']); 
     $subscribesearches = mysqli_real_escape_string($mysqli,$_POST['subscribesearches']); 
     $subscribedrips = mysqli_real_escape_string($mysqli,$_POST['subscribedrips']); 

     $query = ("INSERT INTO contacts (date, firstname, lastname, spousefirst, spouselast, primarybday, spousebday, phonecell, phonehome, phoneoffice, spousecell, phoneother, email, emailspouse, emailother, emailspouseother, address, suite, city, state, zipcode, addressother, suiteother, cityother, stateother, zipcodeother, agentassigned, contacttype, contactstatus, leadstatus, contactsource, timing, password, subscribesearches, subscribedrips) VALUES (NOW(), '$firstname', '$lastname', '$spousefirst', '$spouselast', '$primarybday', '$spousebday', '$phonecell', '$phonehome', '$phoneoffice', '$spousecell', '$phoneother', '$email', '$emailspouse', '$emailother', '$emailspouseother', '$address', '$suite', '$city', '$state', '$zipcode', '$addressother', '$suiteother', '$cityother', '$stateother', '$zipcodeother', '$agentassigned', '$contacttype', '$contactstatus', '$leadstatus', '$contactsource', '$timing', '$password', '$subscribesearches', '$subscribedrips')"); 

     mysqli_query($mysqli,$query) or die ("Error: ".mysqli_error($mysqli)); 

     header("location: http://www.mydomain.com/backend/leads/edit/?ID=".mysqli_insert_id($mysqli)); 

    exit; 
} 

답변

1

당신은 정말 그냥 쿼리를 분리보다 더에 대해 걱정할 필요가 없지만, 그들이 하나가 INSERT 인 서로 (그리고 하나 SELECT입니다 독립적으로 표시, 그들은 트랜잭션의 일부가 아닌).

여기 실제 오류는 불필요하고 위험한 페이지에서 동일한 파일을 두 번 필요로하는 것일 수 있습니다. 두 번째 require 문을 삭제하십시오.

+0

좋아요, 잘 배우고 있습니다. 오류가 사라지도록 두 번째 require 문을 제거했습니다. addnew.php를 열면 SELECT 쿼리의 '

'이 표시됩니다. 어떻게 그 일을 막을 수 있습니까? 또 내가 무엇을 걱정해야합니까? 감사! – Josh

+0

[테이블의] 에코 코드 위에'if (! isset ($ _ POST [ 'firstname']))'같은 것을하십시오. (echo 문을 if 블록처럼 감싼다.) 양식이 제출되면 firstname이 있으므로 테이블을 숨 깁니다. 이러한 액션을 여러 개의 PHP 파일로 분리해야합니다. 비즈니스 로직과 뷰 코드를 분리해야합니다. [관심의 분리] (http://en.wikipedia.org/wiki/Separation_of_concerns) – Lusitanian

+0

데이빗 고마워. 나는 이것을 이해하려고 노력했다. 비즈니스 로직과 별도의 뷰 코드를 사용한다고해서 새로운 페이지 table.php를 생성 한 다음 쿼리에 포함을해야한다는 의미인가? 다른 모든 HTML이있는 원본 페이지 viewdetails.php에 테이블을 넣을 수 있습니까? 나는 이것을 시도했지만 오류가 발생합니다. 도와 주셔서 다시 한번 감사 드리며, 저는이 사실을 이해하려고 노력하고 있습니다. 많은 포럼은 이해하기가 어렵습니다. – Josh

관련 문제