Jump to content

getting error message with mysql_affected_rows..not sure why


simcoweb

Recommended Posts

Here's my code:

 

<?php
// 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) or die(mysql_error());
  $aff_rows = mysql_affected_rows($results);
  if ($aff_rows == 1){
    include 'header.inc';
    show_results();
include 'footer.inc';
}
?>

 

I'm getting this error message when running this:

 

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\corbaley\process2.php on line 65

 

with line 65 being the $aff_rows = mysql_affected_rows($results)

 

It's gotta be something obvious but I need a new set of eyes to find it :)

Link to comment
Share on other sites

mysql_affected_rows() takes a LINK identifier - not a RESULT identifier. You may also need to add the link identifier to the

mysql_query() command

 

<?php

//Create a link when connecting to the database
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');


// 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')";

//Use the link identifier created above in the query and the affected rows functions
  $results = mysql_query($sql, $link) or die(mysql_error());
  $aff_rows = mysql_affected_rows($link);
  if ($aff_rows == 1){
    include 'header.inc';
    show_results();
include 'footer.inc';
}

?>

Link to comment
Share on other sites

Thanks for the post! That took care of that part. But, as with most coding efforts, now a new issue has arisen from its ashes!

 

Basically, using the same code i've posted already regarding the INSERT of the data, i'm attempting to then display the results, if successful, on the next page. What i'm getting, though, is a duplicate display of the code. In other words, it's displaying the results twice vertically.

 

Here's the code snippets in question. First, in the form processing script i'm using an if/else to summon a function that displays the form results in a table. Like so:

 

<?php
$results = mysql_query($sql, $link) or die(mysql_error());
  $aff_rows = mysql_affected_rows($link);
  if ($aff_rows == 1){
    include 'header.inc';
    show_results();
include 'footer.inc';
} else {
  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';
}
?>

 

The basics here is that IF the query was successful and IF at least one affected row proves that then run the code and display the contents of the show_results() function which is this 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>$fname</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";
}
}
?>

 

Now, 1st, the function code produces an error that the while loop is an invalid mysql query. 2nd, it still displays the HTML without the results but, as I mentioned, it repeats itself. It displays the text message, the table with the names of the fields, no results of those fields though....then displays it all again just below that.

 

Ideas anyone?

Link to comment
Share on other sites

OK< the 1st problem is due to variable scope. Variables you create outside a function are not available inside a function unless you call them as global variables. However, in this situation a better alternative would be to pass $result to the function:

 

In the first script

show_form_params($result);

 

In the 2nd script

function show_results($result) {

 

I don't see anything obvious that would cause the 2nd problem you describe. Try the above fix for #1 and see if that fixes #2 as well.

Link to comment
Share on other sites

$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')";

 

echo $sql; // Print this in mysql .  Is this giving result or error ..............

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.