我如何在同一个文件中分隔查询

时间:2012-06-28 00:17:05

标签: php mysqli

我真的很挣扎。我有一个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 个答案:

答案 0 :(得分:1)

您确实需要担心的不仅仅是分离查询,但它们似乎彼此独立(一个是INSERT,一个是SELECT,它们不是事务的一部分)。

这里真正的错误可能是你在页面上需要两次相同的文件,既不必要又有风险。删除第二个require语句。