Solved

PHP Help Needed

Posted on 2011-03-18
4
193 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now