Jump to content

Why is this function repeating when called?


simcoweb

Recommended Posts

I have this function set to call when a form is successfully submitted. Basically it's supposed to display a confirmation/thanks message and the results of their form input. The problem is:

 

1. it's repeating itself in the display (See below for example)

2. I'm getting this 'invalid query' message for the 'while' function

 

Here's the function code:

 

<?php
// function for showing form submission results
function show_results() {
  echo "<h3>Request Sent</h3><br />\n";
  echo "<font face='Verdana' size='2'>Your information has been sent. A representative will contact you shortly to discuss your current situation and your options based upon the information provided.<br />\n";
  echo "Thank you for submitting your contact request!<br />\n";
  echo "Cordially yours,<br />\n";
  echo "David Corbaley</font><p>";
  
          while ($row = mysql_fetch_array($results)){
    echo "<table width='90%' border='0' cellpadding='2' align='center'>
      <tr><td colspan='2'><font face='Verdana' size='2'>The information you submitted:</td></tr>
	  <tr><td>First name:</td><td>" . $row['first_name'] . "</td></tr>
	  <tr><td>Last name:</td><td>" . stripslashes($row['last_name']) . "</td></tr>
	  <tr><td>Email:</td><td>" . stripslashes($row['email']) . "</td></tr>
	  <tr><td>Daytime phone:</td><td>" . stripslashes($row['day_phone']) . "</td></tr>
	  <tr><td>Alternate phone:</td><td>" . stripslashes($row['alt_phone']) . "</td></tr>
	  <tr><td>Best time to call:</td><td>" . stripslashes($row['best_time']) . "</td></tr>
	  <tr><td>Address:</td><td>" . stripslashes($row['address']) . "</td></tr>
	  <tr><td>City:</td><td>" . stripslashes($row['city']) . "</td></tr>
	  <tr><td>State:</td><td>" . stripslashes($row['state']) . "</td></tr>
	  <tr><td>Zip:</td><td>" . stripslashes($row['zip']) . "</td></tr>
	  <tr><td>Type of mortgage:</td><td>" . stripslashes($row['type_mortgage']) . "</td></tr>
	  <tr><td>1st Mortgage holder:</td><td>" . stripslashes($row['first_company']) . "</td></tr>
	  <tr><td>Months behind:</td><td>" . stripslashes($row['months_behind']) . "</td></tr>
	  <tr><td>Back payments:</td><td>" . stripslashes($row['back_payments']) . "</td></tr>
	  <tr><td>NOD filed:</td><td>" . stripslashes($row['notice_of_foreclosure']) . "</td></tr>
	  <tr><td>Auction date set:</td><td>" . stripslashes($row['auction_date']) . "</td></tr>
	  <tr><td>Date of auction:</td><td>" . stripslashes($row['date_of_auction']) . "</td></tr>
	  <tr><td>2nd Mortgage holder:</td><td>" . stripslashes($row['second_company']) . "</td></tr>
	  <tr><td>Combined balances:</td><td>" . stripslashes($row['combined_balances']) . "</td></tr>
	  <tr><td>Combined payments:</td><td>" . stripslashes($row['combined_payments']) . "</td></tr>
	  <tr><td>Currently in bankruptcy:</td><td>" . $row['in_bankruptcy'] . "</td></tr>
	  <tr><td>Situation details:</td><td>" . stripslashes($row['situation']) . "</td></tr>
	  <tr><td>Keep or sell property:</td><td>" . $row['keep_property'] . "</td></tr>
	  <tr><td>Referred by:</td><td>" . $row['referred_by'] . "</td></tr>
	  </table>\n";
}
}
?>

 

Here's what it displays after submitting the form:

 

Request Sent

 

Your information has been sent. A representative will contact you shortly to discuss your current situation and your options based upon the information provided.

Thank you for submitting your contact request!

Cordially yours,

David Corbaley

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\corbaley\db_config.inc on line 143

 

Request Sent

 

Your information has been sent. A representative will contact you shortly to discuss your current situation and your options based upon the information provided.

Thank you for submitting your contact request!

Cordially yours,

David Corbaley

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\corbaley\db_config.inc on line 143

 

Line 143 is the 'while' statement. Notice how it repeats the output? That's a mystery. Plus, i'm fuzzy on the query issue. This function is contained in an 'include' file which the main script contains ( include 'db_config.inc'). Which, if i'm correct, would allow the function's 'while' loop to work off the query that's in the main script which posts the input into variables and inserts them into the database. That may be the problem as it may not allow me to handle the while loop in that fashion?

Link to comment
Share on other sites

function show_results() {
  global $results;
  echo "<h3>Request Sent</h3><br />\n";

 

OR

 

show_results($results); 
function show_results($results) {
  echo "<h3>Request Sent</h3><br />\n";

 

You either have to decalre $results inside the function, as global or pass it as a parameter due to scope.

Link to comment
Share on other sites

Here's the query code:

 

<?php
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
  mysql_select_db($dbname, $link) or die(mysql_error());
  // run our query to insert the record
  $sql = "INSERT INTO leads (first_name, last_name, email, day_phone, alt_phone, best_time, address, 
  			city, state, zip, type_mortgage, first_company, months_behind, back_payments, notice_of_foreclosure,
	  auction_date, date_of_auction, second_company, combined_balances, combined_payments, in_bankruptcy,
	  situation, keep_property, referred_by ) VALUES ('$fname', '$lname', '$email', '$dayphone', '$altphone',
	  '$besttime', '$address', '$city', '$state', '$zip', '$mortgage', '$first_mtg', '$months_behind',
	  '$back_payments', '$notice', '$auction', '$auction_date', '$second_mortgage', '$balances', '$payments',
	  '$bankruptcy', '$situation', '$keep_sell', '$referred')";
  $results = mysql_query($sql, $link) or die(mysql_error());
  $aff_rows = mysql_affected_rows($link);
?>

Link to comment
Share on other sites

Hey, thanks for your post! We were on the same brain wave with that solution as it dawned on me that the 'while' loop needed to extract data, not insert. So, in a step ahead of your post, I modified the code to this:

 

<?php
// function for showing form submission results
function show_results() {
  global $new_id;
  echo $new_id ."is your new ID";
  echo "<h3>Request Sent</h3><br />\n";
  echo "<font face='Verdana' size='2'>Your information has been sent. A representative will contact you shortly to discuss your current situation and your options based upon the information provided.<br />\n";
  echo "Thank you for submitting your contact request!<br />\n";
  echo "Cordially yours,<br />\n";
  echo "David Corbaley</font><p>";
  // run a query for this insert
  $query = mysql_query("SELECT * FROM leads WHERE id='$new_id'");
          while ($row = mysql_fetch_array($query)){
    echo "<table width='90%' border='0' cellpadding='2' align='center'>
      <tr><td colspan='2'><font face='Verdana' size='2'>The information you submitted:</td></tr>
	  <tr><td>First name:</td><td>" . $row['first_name'] . "</td></tr>
	  <tr><td>Last name:</td><td>" . stripslashes($row['last_name']) . "</td></tr>
	  <tr><td>Email:</td><td>" . stripslashes($row['email']) . "</td></tr>
	  <tr><td>Daytime phone:</td><td>" . stripslashes($row['day_phone']) . "</td></tr>
	  <tr><td>Alternate phone:</td><td>" . stripslashes($row['alt_phone']) . "</td></tr>
	  <tr><td>Best time to call:</td><td>" . stripslashes($row['best_time']) . "</td></tr>
	  <tr><td>Address:</td><td>" . stripslashes($row['address']) . "</td></tr>
	  <tr><td>City:</td><td>" . stripslashes($row['city']) . "</td></tr>
	  <tr><td>State:</td><td>" . stripslashes($row['state']) . "</td></tr>
	  <tr><td>Zip:</td><td>" . stripslashes($row['zip']) . "</td></tr>
	  <tr><td>Type of mortgage:</td><td>" . stripslashes($row['type_mortgage']) . "</td></tr>
	  <tr><td>1st Mortgage holder:</td><td>" . stripslashes($row['first_company']) . "</td></tr>
	  <tr><td>Months behind:</td><td>" . stripslashes($row['months_behind']) . "</td></tr>
	  <tr><td>Back payments:</td><td>" . stripslashes($row['back_payments']) . "</td></tr>
	  <tr><td>NOD filed:</td><td>" . stripslashes($row['notice_of_foreclosure']) . "</td></tr>
	  <tr><td>Auction date set:</td><td>" . stripslashes($row['auction_date']) . "</td></tr>
	  <tr><td>Date of auction:</td><td>" . stripslashes($row['date_of_auction']) . "</td></tr>
	  <tr><td>2nd Mortgage holder:</td><td>" . stripslashes($row['second_company']) . "</td></tr>
	  <tr><td>Combined balances:</td><td>" . stripslashes($row['combined_balances']) . "</td></tr>
	  <tr><td>Combined payments:</td><td>" . stripslashes($row['combined_payments']) . "</td></tr>
	  <tr><td>Currently in bankruptcy:</td><td>" . $row['in_bankruptcy'] . "</td></tr>
	  <tr><td>Situation details:</td><td>" . stripslashes($row['situation']) . "</td></tr>
	  <tr><td>Keep or sell property:</td><td>" . $row['keep_property'] . "</td></tr>
	  <tr><td>Referred by:</td><td>" . $row['referred_by'] . "</td></tr>
	  </table>\n";
}
}
?>

 

Whereas I added the global statement for the $new_id variable that was set during the INSERT query, created a SELECT query pulling the data from the $new_id reference (WHERE id='$new_id') and now the invalid query statement is gone.

 

BUT, I still get a complete repeat of the data being displayed. Check it out:

 

Request Sent

 

Your information has been sent. A representative will contact you shortly to discuss your current situation and your options based upon the information provided.

Thank you for submitting your contact request!

Cordially yours,

David Corbaley

 

The information you submitted:

First name: hello

Last name: dolly

Email: bozo@bozo.com

Daytime phone: 333-333-3333

Alternate phone:

Best time to call: anytime

Address: 3234 overhere

City: there

State: CA

Zip: 93333

Type of mortgage: conventional

1st Mortgage holder: sloop

Months behind: a bunch

Back payments: a lot

NOD filed: Yes

Auction date set: No

Date of auction: today

2nd Mortgage holder: bonzos

Combined balances: a lot

Combined payments: a whole lot

Currently in bankruptcy: No

Situation details: i'm broke

Keep or sell property: Keep

Referred by: TV ad

 

22is your new ID

Request Sent

 

Your information has been sent. A representative will contact you shortly to discuss your current situation and your options based upon the information provided.

Thank you for submitting your contact request!

Cordially yours,

David Corbaley

 

The information you submitted:

First name: hello

Last name: dolly

Email: bozo@bozo.com

Daytime phone: 333-333-3333

Alternate phone:

Best time to call: anytime

Address: 3234 overhere

City: there

State: CA

Zip: 93333

Type of mortgage: conventional

1st Mortgage holder: sloop

Months behind: a bunch

Back payments: a lot

NOD filed: Yes

Auction date set: No

Date of auction: today

2nd Mortgage holder: bonzos

Combined balances: a lot

Combined payments: a whole lot

Currently in bankruptcy: No

Situation details: i'm broke

Keep or sell property: Keep

Referred by: TV ad

Link to comment
Share on other sites

No problemo! :)

 

<?php
  if ($aff_rows == 1){
    include 'header.inc';
    show_results();
include 'footer.inc';
} 
?>

 

Basically the premise is IF the INSERT query was successful then show the results of the function. If not, then there's another function that displays.

Link to comment
Share on other sites

Are doing elseif, or another if.

 

The reason being is if you are doing an if, it could executing both if statements, which would be the reason for the double results...is this all the code, or just a shortened version? If shortened we would need to see all the code...

Link to comment
Share on other sites

I was doing an 'if' and 'else' statement so if the 'if' returned false then the 'else' would prevail. I've since changed it to two separate 'ifs' while experimenting. Here's the entire block of code with the query, the 'if' statements dependent upon that query.

 

<?php
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
  mysql_select_db($dbname, $link) or die(mysql_error());
  // run our query to insert the record
  $sql = "INSERT INTO leads (first_name, last_name, email, day_phone, alt_phone, best_time, address, 
  			city, state, zip, type_mortgage, first_company, months_behind, back_payments, notice_of_foreclosure,
	  auction_date, date_of_auction, second_company, combined_balances, combined_payments, in_bankruptcy,
	  situation, keep_property, referred_by ) VALUES ('$fname', '$lname', '$email', '$dayphone', '$altphone',
	  '$besttime', '$address', '$city', '$state', '$zip', '$mortgage', '$first_mtg', '$months_behind',
	  '$back_payments', '$notice', '$auction', '$auction_date', '$second_mortgage', '$balances', '$payments',
	  '$bankruptcy', '$situation', '$keep_sell', '$referred')";
  $results = mysql_query($sql, $link) or die(mysql_error());
  $aff_rows = mysql_affected_rows($link);
  $new_id = mysql_insert_id();
  if ($aff_rows == 1){
    include 'header.inc';
    show_results();
include 'footer.inc';
} 
if ($aff_rows <> 1) {
  include 'header.inc';
  echo "<h3>Error</h3><br />There appears to be a problem submitting your data. Please review and correct it to try again.<br />\n";
  show_form_params();
  include 'footer.inc';
}
?>

 

A quick note, the second function in the second 'if' statement displays completely different info. The show_results(); function shows the data that was inserted in the INSERT query. The show_form_params(); shows the form with the input fields populated with their input. The dual display is of the show_results(); function.

Link to comment
Share on other sites

What gets me is that the first display does not show the new_id. So it seems as though you may have 2 references somewhere that is printing that result. I am not sure. Because if show_results was doubling up it would show that 22new id  portion, which it does not.

 

In the header.inc, you did not just copy/paste what is inside the show_results function for testing reasons did you?

Link to comment
Share on other sites

add 'LIMIT 1' to the end of your SELECT query.

 

EDIT:

Ok, found the problem! I was repeating the show_results(); function in the header.inc file! That issue is resolved!

 

well there ya go =). frost has a keen eye. altho, if after adding 'LIMIT 1' to your select query still displayed results twice, that would have been the next thing i would have assumed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.