Solved

PHP Help Needed

Posted on 2011-03-18
4
199 Views
Last Modified: 2012-05-11
Can someone take a look at line 36 in this code?

This code pulls data from a MySQL database & displays it in a form. Then I can edit the data & click submit & it updates the MySQL database. It then, sends an email to the email address on line 35. Again, all of this works until I start editing line 36 to send the location, first name, & last name within the message of the email.

What is the code so I can get an email with the first name, last name & location?
<?php # edit_dqa.php

$page_title = 'Edit a Record';

$con = mysql_connect("localhost","uname","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("psrflow", $con);

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { 
	$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted.
	$id = $_POST['id'];
} else { // No valid ID, kill the script.
	echo '<h1 id="mainhead">Page Error</h1>
	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
	include ('./includes/footer.html'); 
	exit();
}
		
		$query = "UPDATE psrinfo SET fname='$fn', lname='$ln', location='$loc' WHERE fid=$id";
		$result = @mysql_query ($query); // Run the query.
				
	// Send Email
	switch ('location') {
	case 'location1':
	echo $EmailAddress = 'myemail@yahoo.com';
	break;
	
	}
	
$EmailAddress = 'myemail@yahoo.com';	
$Message = "Someone has edited the information below."; Location : {$_REQUEST['location']}<br>First Name : {$_REQUEST['fname']}<br>Last Name : {$_REQUEST['lname']}"
$Headers  = "MIME-Version: 1.0\n"; 
$Headers = "Content-type: text/html; charset=iso-8859-1\n"; 
$Subject  = "Please Change";
$Headers = "To: DQA <$EmailAddress>\n"; 
$Headers = "From: anotheremail@yahoo.com \n"; 

if (mail($EmailAddress, $Subject, $Message, $Headers)) {
						
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '//')) {
$url = substr ($url, 0, -1);
}
$url ='/flow/index.html'; 

header("Location: $url");

exit();
		
	} else { // Report the errors.
	
		echo '<h1 id="mainhead">Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
		foreach ($errors as $msg) { // Print each error.
			echo " - $msg<br />\n";
		}
		echo '</p><p>Please try again.</p><p><br /></p>';
		
	} // End of if (empty($errors)) IF.

} // End of submit conditional.

// Retrieve the user's information.
$query = "SELECT pacts, fname, lname, location WHERE fid = " . $_REQUEST['id'];
$result = @mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

	$row = mysql_fetch_array ($result, MYSQL_NUM);
		
?>	

<?php

echo '<form action="edit_dqa.php" method="post">
<fieldset><legend><h1> You are editing a record!</h1></legend>

<b>First Name:</b> <br><input type="text" name="fname" size="15" maxlength="30" value="'.$row[1].'" /><br />
<b>Last Name:</b> <br><input type="text" name="lname" size="15" maxlength="30" value="'.$row[2].'" /><br />
<b>Location: </b><br><input type="text" name="location" size="15" maxlength="30" value="'.$row[5].'" /><br>
br>
<br>
</fieldset>

<div align="left"><input type="submit" name="submit" value="Submit" /></div>

<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

} else { // Not a valid user ID.
	echo '<h1 id="mainhead">Page Error</h1>
	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close the database connection.

?>

Open in new window

0
Comment
Question by:wantabe2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:CombatGold1
ID: 35164791
You keep overwriting the existing $Headers line with new ones, which will result in the header only containing "From: anotheremail@yahoo.com \n";

To append text onto the end of a string, you need to use a dot before the equals sign:
$Headers = "First line\n";
$Headers .= "Add this line\n";
$Headers .= "Add another line\n";
$Headers = "Actually erase those 3 lines, just have this line instead\n";

Open in new window


Also line 36 looks invalid. I'm guessing you want it to be something like this:
$Message = "Someone has edited the information below. Location : ${_REQUEST['location']}<br>\nFirst Name : ${_REQUEST['fname']}<br>\nLast Name : ${_REQUEST['lname']}";

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
ID: 35164964
Thanks, I did what you advised & it works perfect!

Is there any way I can add some kind of confirmation such as "Your edited information has been submitted" on the user end after they click on the "submit" button? Thanks again!
0
 
LVL 3

Accepted Solution

by:
CombatGold1 earned 500 total points
ID: 35166259
Yes, working with this bit:
$query = "UPDATE psrinfo SET fname='$fn', lname='$ln', location='$loc' WHERE fid=$id";
$result = @mysql_query ($query); // Run the query.

Open in new window

After that result line, add:
if (mysql_affected_rows() == 1)
	echo '<p>Your information has been updated successfully.</p>';
else
	echo '<p class="error">There was a problem updating your information.</p>';

Open in new window

You should really open a new question for that though as that is a separate issue ;)



Also I would also suggest you do not put SQL queries in a variable. You are not erasing the variable after executing the query, so there is the chance of it being leaked. Instead I'd advise you to put the query directly into the function so the SQL string is not kept in memory, like this:
$result = @mysql_query("UPDATE psrinfo SET fname='$fn', lname='$ln', location='$loc' WHERE fid=$id");

Open in new window

0
 
LVL 15

Author Closing Comment

by:wantabe2
ID: 35166619
Thanks, still not working but I will open up a new post.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question