I’m really struggling with this. I have a viewdetails.php page, addnew.php page and a php.php. The php.php page has my query for both pages. First I have the query that displays results on viewdetails.php and second is the query that displays results on addnew.php page. The problem I’m having is when I open addnew.php in a browser it gives me errors regarding the path to dbandpassword.php and for other items in the query for viewdetails.php.
How do I keep all these queries in one page (php.php) and keep them separate so I don’t get errors? Should I be combining the two queries together or maybe just some of it together? I know the code needs to be cleaned up, I’m not worried about that right now. I also know that the queries both work individually if put in their own file (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;
}
You really do need to worry about more than just separating the queries, but they appear to be independent of one another (one is an INSERT and one is a SELECT, and they aren’t part of a transaction).
The real error here is probably that you’re requiring the same file twice on a page which is both needless and risky. Delete the second require statement.